diff --git a/sbin/upgrade_database.py b/sbin/upgrade_database.py new file mode 100755 index 0000000..f6db36c --- /dev/null +++ b/sbin/upgrade_database.py @@ -0,0 +1,77 @@ +#!/usr/bin/python3 + +import sys +import os +# Because Python 🙄 +bin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../bin")) +sys.path.insert(1, bin_path) +import configuration + +print( +""" +\u001b[31;4mYou are about to upgrade the database to the latest schema in /etc/db/schema-template.sql.\u001b[0m + +\u001b[33mThis will cause all existing surveys to be wiped out and re-created. +Data which is captured by the database itself, e.g., event data will be exported¹ +and re-imported. The rest should be brought back in at the next bin/runner.sh +run. + +¹ As of this version, not all data will be exported. The following information +will be lost: + + - Sequence NTBA status + - Shot NTBA status + - Shot NTBP status + - Raw sequence remarks + - Raw shot remarks + - Preplot point remarks + \u001b[33;1m- All real-time data will also be lost\u001b[33m + +\u001b[0m + +If this is what you want to do, press ENTER, else press Ctrl+C to cancel. +""") + +input() + +dbname=os.environ["PGDATABASE"] if "PGDATABASE" in os.environ else "dougal" +dbuser=os.environ["PGUSER"] if "PGUSER" in os.environ else "postgres" +dbtemplate=os.path.abspath(os.path.join(bin_path, "..", "etc", "db", "database-template.sql")) + +system_exports = os.path.join(bin_path, "system_exports.py") +create_survey = os.path.join(bin_path, "create_survey.sh") +runner = "RUNNER_NOEXPORT=true RUNNER_IMPORT=true " + os.path.join(bin_path, "runner.sh") + +print(system_exports) +#os.system(system_exports) + +cmd = f"psql -U {dbuser} -c 'DROP DATABASE {dbname};'" +print(cmd) +res = os.system(cmd) +if res != 0: + print("Cannot proceed. Upgrade aborted.") + exit(res) + +cmd = f"psql -U {dbuser} <{dbtemplate}" +print(cmd) +os.system(cmd) + +#for survey in configuration.surveys(): + #schema = survey["schema"] + #cmd = f"psql dougal -c 'DROP schema {schema} CASCADE;'" + #print(cmd) + +for survey in configuration.surveys(): + id = survey["id"].lower() + name = survey["name"] + epsg = survey["epsg"] + schema = survey["schema"] + + cmd = f"{create_survey} \"{id}\" \"{name}\" {epsg}" + print(cmd) + os.system(cmd) + +print(runner) +os.system(runner) + +print("Done")