← Back to team overview

credativ team mailing list archive

[Merge] lp:~therp-nl/openupgrade-server/7.0_lp1265463_use_password into lp:openupgrade-server

 

Ronald Portier (Therp) has proposed merging lp:~therp-nl/openupgrade-server/7.0_lp1265463_use_password into lp:openupgrade-server.

Requested reviews:
  OpenUpgrade Committers (openupgrade-committers)
Related bugs:
  Bug #1265463 in OpenUpgrade Addons: "Can not upgrade database with password only with ident authentication"
  https://bugs.launchpad.net/openupgrade-addons/+bug/1265463

For more details, see:
https://code.launchpad.net/~therp-nl/openupgrade-server/7.0_lp1265463_use_password/+merge/200301

Migrate script will use all database connection info found in the config file that is already a required parameter.
-- 
https://code.launchpad.net/~therp-nl/openupgrade-server/7.0_lp1265463_use_password/+merge/200301
Your team OpenUpgrade Committers is requested to review the proposed merge of lp:~therp-nl/openupgrade-server/7.0_lp1265463_use_password into lp:openupgrade-server.
=== modified file 'scripts/migrate.py'
--- scripts/migrate.py	2013-05-09 13:49:52 +0000
+++ scripts/migrate.py	2014-01-02 13:07:09 +0000
@@ -94,13 +94,25 @@
 
 config.read(options.config)
 
-db_user=config.get('options', 'db_user')
+conn_parms = {}
+for p in ('host', 'port', 'user', 'password'):                             
+    cfg = config.get('options', 'db_' + p)                                          
+    if cfg:                                                                
+        conn_parms[p] = cfg
+
+if not 'user' in conn_parms:
+    print 'No user found in configuration'
+    sys.exit()
+db_user = conn_parms['user']
+
 db_name=options.database or config.get('options', 'db_name')
 
 if not db_name or db_name=='' or db_name.isspace() or db_name.lower()=='false':
   parser.print_help()
   sys.exit()
 
+conn_parms['database'] = db_name
+
 if options.inplace:
   db=db_name
 else:
@@ -185,18 +197,36 @@
 if not options.inplace:
     print('copying database %(db_name)s to %(db)s...' % {'db_name': db_name, 
                                                          'db': db})
-    conn=psycopg2.connect(database=db_name, user=db_user)
+    conn = psycopg2.connect(**conn_parms)
     conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
     cur=conn.cursor()
     cur.execute('drop database if exists "%(db)s"' % {'db': db})
     cur.execute('create database "%(db)s"' % {'db': db})
     cur.close()
-    os.system(('pg_dump --format=custom --user=%(user)s %(db_name)s | pg_restore '+
-    '--dbname=%(db)s') % {
-        'user': db_user, 
-        'db_name': db_name, 
-        'db': db,
-        })
+
+    os.environ['PGUSER'] = db_user             
+    if ('host' in conn_parms and conn_parms['host']
+    and not os.environ.get('PGHOST')):
+        os.environ['PGHOST'] = conn_parms['host']             
+
+    if ('port' in conn_parms and conn_parms['port']
+    and not os.environ.get('PGPORT')):
+        os.environ['PGPORT'] = conn_parms['port']             
+
+    password_set = False
+    if ('password' in conn_parms and conn_parms['password']
+    and not os.environ.get('PGPASSWORD')):
+        os.environ['PGPASSWORD'] = conn_parms['password']             
+        password_set = True
+
+    os.system(
+        ('pg_dump --format=custom --no-password %(db_name)s ' +
+         '| pg_restore --no-password --dbname=%(db)s') %
+        {'db_name': db_name, 'db': db}
+    )
+
+    if password_set:
+        del os.environ['PGPASSWORD'] 
 
 for version in options.migrations.split(','):
   print 'running migration for '+version


Follow ups