- cursor.execute("SELECT relname from pg_class where relname = 'plc_db_version'")
- rows = cursor.fetchall()
- if not rows:
- print "Warning: current db has no version. Unable to validate config file."
- else:
- cursor.execute("SELECT version FROM plc_db_version")
- rows = cursor.fetchall()
- if not rows or not rows[0]:
- print "Warning: current db has no version. Unable to validate config file."
- elif rows[0][0] == db_version_new:
- print "Status: Versions are the same. No upgrade necessary."
- sys.exit()
- elif not rows[0][0] == db_version_previous:
- print "Stauts: DB_VERSION_PREVIOUS in config file (%s) does not" \
- " match current db version %d" % (upgrade_config_file, rows[0][0])
- sys.exit()
- else:
- print "STATUS: attempting upgrade from %d to %d" % \
- (db_version_previous, db_version_new)
-
- # check db encoding
- sql = " SELECT pg_catalog.pg_encoding_to_char(d.encoding)" \
- " FROM pg_catalog.pg_database d " \
- " WHERE d.datname = '%s' " % config['PLC_DB_NAME']
- cursor.execute(sql)
- rows = cursor.fetchall()
- if rows[0][0] not in ['UTF8', 'UNICODE']:
- print "WARNING: db encoding is not utf8. Attempting to encode"
- db.close()
- # generate db dump
- dump_file = '%s/dump.sql' % (temp_dir)
- dump_file_encoded = dump_file + ".utf8"
- dump_cmd = 'pg_dump -i %s -U postgres -f %s > /dev/null 2>&1' % \
- (config['PLC_DB_NAME'], dump_file)
- if os.system(dump_cmd):
- print "ERROR: during db dump. Exiting."
- sys.exit(1)
- # encode dump to utf8
- print "Status: encoding database dump"
- encode_utf8(dump_file, dump_file_encoded)
- # archive original db
- archive_db(config['PLC_DB_NAME'], config['PLC_DB_NAME']+'_sqlascii_archived')
- # create a utf8 database and upload encoded data
- recreate_cmd = 'createdb -U postgres -E UTF8 %s > /dev/null; ' \
- 'psql -a -U %s %s < %s > /dev/null 2>&1;' % \
- (config['PLC_DB_NAME'], config['PLC_DB_USER'], \
- config['PLC_DB_NAME'], dump_file_encoded)
- print "Status: recreating database as utf8"
- if os.system(recreate_cmd):
- print "Error: database encoding failed. Aborting"
- sys.exit(1)
-
- os.remove(dump_file_encoded)
- os.remove(dump_file)
+ cursor.execute("SELECT relname from pg_class where relname = 'plc_db_version'")
+ rows = cursor.fetchall()
+ if not rows:
+ print("Warning: current db has no version. Unable to validate config file.")
+ else:
+ cursor.execute("SELECT version FROM plc_db_version")
+ rows = cursor.fetchall()
+ if not rows or not rows[0]:
+ print("Warning: current db has no version. Unable to validate config file.")
+ elif rows[0][0] == db_version_new:
+ print("Status: Versions are the same. No upgrade necessary.")
+ sys.exit()
+ elif not rows[0][0] == db_version_previous:
+ print("Stauts: DB_VERSION_PREVIOUS in config file (%s) does not" \
+ " match current db version %d" % (upgrade_config_file, rows[0][0]))
+ sys.exit()
+ else:
+ print("STATUS: attempting upgrade from %d to %d" % \
+ (db_version_previous, db_version_new))
+
+ # check db encoding
+ sql = " SELECT pg_catalog.pg_encoding_to_char(d.encoding)" \
+ " FROM pg_catalog.pg_database d " \
+ " WHERE d.datname = '%s' " % config['PLC_DB_NAME']
+ cursor.execute(sql)
+ rows = cursor.fetchall()
+ if rows[0][0] not in ['UTF8', 'UNICODE']:
+ print("WARNING: db encoding is not utf8. Attempting to encode")
+ db.close()
+ # generate db dump
+ dump_file = '%s/dump.sql' % (temp_dir)
+ dump_file_encoded = dump_file + ".utf8"
+ dump_cmd = 'pg_dump -i %s -U postgres -f %s > /dev/null 2>&1' % \
+ (config['PLC_DB_NAME'], dump_file)
+ if os.system(dump_cmd):
+ print("ERROR: during db dump. Exiting.")
+ sys.exit(1)
+ # encode dump to utf8
+ print("Status: encoding database dump")
+ encode_utf8(dump_file, dump_file_encoded)
+ # archive original db
+ archive_db(config['PLC_DB_NAME'], config['PLC_DB_NAME']+'_sqlascii_archived')
+ # create a utf8 database and upload encoded data
+ recreate_cmd = 'createdb -U postgres -E UTF8 %s > /dev/null; ' \
+ 'psql -a -U %s %s < %s > /dev/null 2>&1;' % \
+ (config['PLC_DB_NAME'], config['PLC_DB_USER'], \
+ config['PLC_DB_NAME'], dump_file_encoded)
+ print("Status: recreating database as utf8")
+ if os.system(recreate_cmd):
+ print("Error: database encoding failed. Aborting")
+ sys.exit(1)
+
+ os.remove(dump_file_encoded)
+ os.remove(dump_file)