From 49d83f11bd428cb87405526f038ba8fc9d1afdbd Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 13:41:50 +0100 Subject: [PATCH] lifecycle: migrate: ensure template schema exists before migrating (cherry-pick #8952) (#9022) lifecycle: migrate: ensure template schema exists before migrating (#8952) Co-authored-by: Marc 'risson' Schmitt --- lifecycle/migrate.py | 1 + lifecycle/system_migrations/template_schema.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 lifecycle/system_migrations/template_schema.py diff --git a/lifecycle/migrate.py b/lifecycle/migrate.py index 489ffa909a..c713bad296 100755 --- a/lifecycle/migrate.py +++ b/lifecycle/migrate.py @@ -64,6 +64,7 @@ def release_lock(cursor: Cursor): """Release database lock""" if not LOCKED: return + LOGGER.info("releasing database lock") cursor.execute("SELECT pg_advisory_unlock(%s)", (ADV_LOCK_UID,)) diff --git a/lifecycle/system_migrations/template_schema.py b/lifecycle/system_migrations/template_schema.py new file mode 100644 index 0000000000..9ab53c0605 --- /dev/null +++ b/lifecycle/system_migrations/template_schema.py @@ -0,0 +1,12 @@ +from lifecycle.migrate import BaseMigration + + +class Migration(BaseMigration): + def needs_migration(self) -> bool: + self.cur.execute( + "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'template';" + ) + return not bool(self.cur.rowcount) + + def run(self): + self.cur.execute("CREATE SCHEMA IF NOT EXISTS template; COMMIT;")