Merge lp:~therp-nl/openupgrade-server/7.0-migrate_script_with_template into lp:openupgrade-server

Proposed by Stefan Rijnhart (Opener)
Status: Merged
Merged at revision: 4646
Proposed branch: lp:~therp-nl/openupgrade-server/7.0-migrate_script_with_template
Merge into: lp:openupgrade-server
Diff against target: 69 lines (+33/-26)
1 file modified
scripts/migrate.py (+33/-26)
To merge this branch: bzr merge lp:~therp-nl/openupgrade-server/7.0-migrate_script_with_template
Reviewer Review Type Date Requested Status
Holger Brunn (Therp) code review Approve
Pedro Manuel Baeza code review Approve
Review via email: mp+210619@code.launchpad.net

Commit message

[RFR] Copy database within the database server

Description of the change

This change copies the database within the database server instead of performing the pg_dump/restore round trip. Apart from a code simplification, this change also allows the script to continue in this environment that I am currently working in, in which the pg utils mysteriously ignore the PGUSER/PASSWORD environment variables.

To post a comment you must log in.
Revision history for this message
Holger Brunn (Therp) (hbrunn) wrote :

This will fail miserably if someone uses the database (an open cursor is enough) to be migrated.

But indeed, the template way is faster and nicer. How about trying that first, catch the exception if the db is in use and then do it with pg_dump?

review: Needs Fixing
4647. By Stefan Rijnhart (Opener)

[RVT] Revert previous fix

4648. By Stefan Rijnhart (Opener)

[FIX] Attempt to copy database server-internally but keep fallback
       on external method

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Thanks for the suggestion! Now committed.

Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

I haven't tried, but seems OK, and the fallback part is the same as previously, so at least this part will work.

Regards.

review: Approve (code review)
Revision history for this message
Holger Brunn (Therp) (hbrunn) :
review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'scripts/migrate.py'
2--- scripts/migrate.py 2014-01-20 21:56:01 +0000
3+++ scripts/migrate.py 2014-03-14 09:29:30 +0000
4@@ -201,32 +201,39 @@
5 conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
6 cur=conn.cursor()
7 cur.execute('drop database if exists "%(db)s"' % {'db': db})
8- cur.execute('create database "%(db)s"' % {'db': db})
9- cur.close()
10-
11- os.environ['PGUSER'] = db_user
12- if ('host' in conn_parms and conn_parms['host']
13- and not os.environ.get('PGHOST')):
14- os.environ['PGHOST'] = conn_parms['host']
15-
16- if ('port' in conn_parms and conn_parms['port']
17- and not os.environ.get('PGPORT')):
18- os.environ['PGPORT'] = conn_parms['port']
19-
20- password_set = False
21- if ('password' in conn_parms and conn_parms['password']
22- and not os.environ.get('PGPASSWORD')):
23- os.environ['PGPASSWORD'] = conn_parms['password']
24- password_set = True
25-
26- os.system(
27- ('pg_dump --format=custom --no-password %(db_name)s ' +
28- '| pg_restore --no-password --dbname=%(db)s') %
29- {'db_name': db_name, 'db': db}
30- )
31-
32- if password_set:
33- del os.environ['PGPASSWORD']
34+ try:
35+ print "Copying the database using 'with template'"
36+ cur.execute('create database "%(db)s" with template "%(db_name)s"' % {
37+ 'db': db, 'db_name': db_name})
38+ cur.close()
39+ except psycopg2.OperationalError:
40+ print "Failed, fallback on creating empty database + loading a dump"
41+ cur.execute('create database "%(db)s"' % {'db': db})
42+ cur.close()
43+
44+ os.environ['PGUSER'] = db_user
45+ if ('host' in conn_parms and conn_parms['host']
46+ and not os.environ.get('PGHOST')):
47+ os.environ['PGHOST'] = conn_parms['host']
48+
49+ if ('port' in conn_parms and conn_parms['port']
50+ and not os.environ.get('PGPORT')):
51+ os.environ['PGPORT'] = conn_parms['port']
52+
53+ password_set = False
54+ if ('password' in conn_parms and conn_parms['password']
55+ and not os.environ.get('PGPASSWORD')):
56+ os.environ['PGPASSWORD'] = conn_parms['password']
57+ password_set = True
58+
59+ os.system(
60+ ('pg_dump --format=custom --no-password %(db_name)s ' +
61+ '| pg_restore --no-password --dbname=%(db)s') %
62+ {'db_name': db_name, 'db': db}
63+ )
64+
65+ if password_set:
66+ del os.environ['PGPASSWORD']
67
68 for version in options.migrations.split(','):
69 print 'running migration for '+version

Subscribers

People subscribed via source and target branches