diff --git a/scheduler/app/database/init_db.py b/scheduler/app/database/init_db.py index 91d0f9b..2a8a053 100755 --- a/scheduler/app/database/init_db.py +++ b/scheduler/app/database/init_db.py @@ -22,6 +22,8 @@ DB_HOST = os.getenv("DB_HOST", "seatable-mysql") DB_PORT = int(os.getenv("DB_PORT", "3306")) DATABASE_NAME = os.getenv("DATABASE_NAME", "scheduler") +# All upgrade statements start from 2.2.4, but here it is not actually 2.2.4—it's just a default value. +CURRENT_VERSION = os.getenv("VERSION", "2.2.4") def wait_for_mysql(): @@ -57,4 +59,18 @@ def wait_for_mysql(): ) os.system(sql) +# Write the current version number directly during the initial deployment. +sql = ( + 'mysql -h %s -u%s -p%s %s -e "INSERT INTO version_history (version, update_at) ' + "SELECT '%s', NOW() WHERE NOT EXISTS (SELECT 1 FROM version_history LIMIT 1);\"" + % ( + shlex.quote(DB_HOST), + shlex.quote(db_user), + shlex.quote(db_passwd), + DATABASE_NAME, + CURRENT_VERSION, + ) +) +os.system(sql) + print("Initalization of database successful") diff --git a/scheduler/app/upgrade/6.2.0.sql b/scheduler/app/upgrade/6.2.0.sql index 496e840..86a0c23 100644 --- a/scheduler/app/upgrade/6.2.0.sql +++ b/scheduler/app/upgrade/6.2.0.sql @@ -1,9 +1,9 @@ ALTER TABLE script_log MODIFY started_at DATETIME(6); -ALTER TABLE script_log ADD COLUMN state VARCHAR(10); -ALTER TABLE script_log ADD KEY state_h3u8i9o1_key (state); +ALTER TABLE script_log ADD COLUMN IF NOT EXISTS state VARCHAR(10); +ALTER TABLE script_log ADD INDEX IF NOT EXISTS state_h3u8i9o1_key (state); -ALTER TABLE script_log ADD COLUMN created_at DATETIME(6); -ALTER TABLE script_log ADD KEY created_at_h3u7y9o4_key (created_at); +ALTER TABLE script_log ADD COLUMN IF NOT EXISTS created_at DATETIME(6); +ALTER TABLE script_log ADD INDEX IF NOT EXISTS created_at_h3u7y9o4_key (created_at); UPDATE script_log SET created_at=started_at;