|
|
|
@ -25,11 +25,11 @@ def init_app(app): |
|
|
|
|
|
|
|
|
|
schemaMigrations = {} |
|
|
|
|
schemaMigrationsPath = join(app.root_path, 'schema_migrations') |
|
|
|
|
print("loading schema migration scripts from {}".format(schemaMigrationsPath)) |
|
|
|
|
app.logger.info("loading schema migration scripts from {}".format(schemaMigrationsPath)) |
|
|
|
|
for filename in listdir(schemaMigrationsPath): |
|
|
|
|
result = re.search(r"^\d+_(up|down)", filename) |
|
|
|
|
if not result: |
|
|
|
|
print(f"schemaVersion {filename} must match ^\d+_(up|down). exiting.") |
|
|
|
|
app.logger.error(f"schemaVersion {filename} must match ^\\d+_(up|down). exiting.") |
|
|
|
|
exit(1) |
|
|
|
|
key = result.group() |
|
|
|
|
with open(join(schemaMigrationsPath, filename), 'rb') as file: |
|
|
|
@ -54,12 +54,12 @@ def init_app(app): |
|
|
|
|
hasSchemaVersionTable = True |
|
|
|
|
|
|
|
|
|
if hasSchemaVersionTable == False: |
|
|
|
|
print("no table named schemaversion found in the {} schema. running migration 01_up".format(app.config['DATABASE_SCHEMA'])) |
|
|
|
|
app.logger.info("no table named schemaversion found in the {} schema. running migration 01_up".format(app.config['DATABASE_SCHEMA'])) |
|
|
|
|
try: |
|
|
|
|
cursor.execute(schemaMigrations["01_up"]) |
|
|
|
|
connection.commit() |
|
|
|
|
except: |
|
|
|
|
print("unable to create the schemaversion table because: {}".format(my_exec_info_message(sys.exc_info()))) |
|
|
|
|
app.logger.error("unable to create the schemaversion table because: {}".format(my_exec_info_message(sys.exc_info()))) |
|
|
|
|
exit(1) |
|
|
|
|
actionWasTaken = True |
|
|
|
|
|
|
|
|
@ -67,24 +67,24 @@ def init_app(app): |
|
|
|
|
schemaVersion = cursor.fetchall()[0][0] |
|
|
|
|
|
|
|
|
|
if schemaVersion > desiredSchemaVersion: |
|
|
|
|
print("schemaVersion ({}) > desiredSchemaVersion ({}). schema downgrades are not supported yet. exiting.".format( |
|
|
|
|
app.logger.critical("schemaVersion ({}) > desiredSchemaVersion ({}). schema downgrades are not supported yet. exiting.".format( |
|
|
|
|
schemaVersion, desiredSchemaVersion |
|
|
|
|
)) |
|
|
|
|
exit(1) |
|
|
|
|
|
|
|
|
|
while schemaVersion < desiredSchemaVersion: |
|
|
|
|
migrationKey = "%02d_up" % (schemaVersion+1) |
|
|
|
|
print("schemaVersion ({}) < desiredSchemaVersion ({}). running migration {}".format( |
|
|
|
|
app.logger.info("schemaVersion ({}) < desiredSchemaVersion ({}). running migration {}".format( |
|
|
|
|
schemaVersion, desiredSchemaVersion, migrationKey |
|
|
|
|
)) |
|
|
|
|
try: |
|
|
|
|
cursor.execute(schemaMigrations[migrationKey]) |
|
|
|
|
connection.commit() |
|
|
|
|
except KeyError: |
|
|
|
|
print("missing schema migration script: {}_xyz.sql".format(migrationKey)) |
|
|
|
|
app.logger.critical("missing schema migration script: {}_xyz.sql".format(migrationKey)) |
|
|
|
|
exit(1) |
|
|
|
|
except: |
|
|
|
|
print("unable to execute the schema migration {} because: {}".format(migrationKey, my_exec_info_message(sys.exc_info()))) |
|
|
|
|
app.logger.critical("unable to execute the schema migration {} because: {}".format(migrationKey, my_exec_info_message(sys.exc_info()))) |
|
|
|
|
exit(1) |
|
|
|
|
actionWasTaken = True |
|
|
|
|
|
|
|
|
@ -93,7 +93,7 @@ def init_app(app): |
|
|
|
|
versionFromDatabase = cursor.fetchall()[0][0] |
|
|
|
|
|
|
|
|
|
if schemaVersion != versionFromDatabase: |
|
|
|
|
print("incorrect schema version value \"{}\" after running migration {}, expected \"{}\". exiting.".format( |
|
|
|
|
app.logger.critical("incorrect schema version value \"{}\" after running migration {}, expected \"{}\". exiting.".format( |
|
|
|
|
versionFromDatabase, |
|
|
|
|
migrationKey, |
|
|
|
|
schemaVersion |
|
|
|
@ -104,7 +104,7 @@ def init_app(app): |
|
|
|
|
|
|
|
|
|
app.config['PSYCOPG2_CONNECTION_POOL'].putconn(connection) |
|
|
|
|
|
|
|
|
|
print("{} current schemaVersion: \"{}\"".format( |
|
|
|
|
app.logger.info("{} current schemaVersion: \"{}\"".format( |
|
|
|
|
("schema migration completed." if actionWasTaken else "schema is already up to date. "), schemaVersion |
|
|
|
|
)) |
|
|
|
|
|
|
|
|
|