From a7a61fdd64875b4a4fd870daa2a821b9da55e946 Mon Sep 17 00:00:00 2001 From: Ivo Oskamp Date: Mon, 19 Jan 2026 15:40:00 +0100 Subject: [PATCH] Auto-commit local changes before build (2026-01-19 15:40:00) --- .last-branch | 2 +- .../src/backend/app/migrations.py | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.last-branch b/.last-branch index f37801d..71635d1 100644 --- a/.last-branch +++ b/.last-branch @@ -1 +1 @@ -v20260119-14-fix-routes-runchecks-syntax +v20260119-15-fix-migrations-autotask-phase2 diff --git a/containers/backupchecks/src/backend/app/migrations.py b/containers/backupchecks/src/backend/app/migrations.py index 69fa355..8dfca71 100644 --- a/containers/backupchecks/src/backend/app/migrations.py +++ b/containers/backupchecks/src/backend/app/migrations.py @@ -43,6 +43,30 @@ def _column_exists_on_conn(conn, table_name: str, column_name: str) -> bool: return result.first() is not None +def _get_table_columns(conn, table_name: str) -> set[str]: + """Return a set of column names for the given table using the provided connection. + + This helper is designed for use inside engine.begin() blocks so that any + errors are properly rolled back before the connection is returned to the pool. + + If the table does not exist (or cannot be inspected), an empty set is returned. + """ + try: + result = conn.execute( + text( + """ + SELECT column_name + FROM information_schema.columns + WHERE table_name = :table + """ + ), + {"table": table_name}, + ) + return {row[0] for row in result.fetchall()} + except Exception: + return set() + + def migrate_add_username_to_users() -> None: """Ensure users.username column exists and is NOT NULL and UNIQUE. @@ -925,9 +949,10 @@ def migrate_job_runs_autotask_ticket_fields() -> None: return try: - with engine.connect() as conn: + with engine.begin() as conn: cols = _get_table_columns(conn, table) if not cols: + print("[migrations] job_runs table not found; skipping migrate_job_runs_autotask_ticket_fields.") return if "autotask_ticket_id" not in cols: @@ -957,12 +982,12 @@ def migrate_job_runs_autotask_ticket_fields() -> None: ) except Exception as exc: print( - f"[migrations] Could not add FK job_runs.autotask_ticket_created_by_user_id -> users.id (continuing): {exc}" + f"[migrations] Could not add FK job_runs_autotask_ticket_created_by_user_id -> users.id (continuing): {exc}" ) conn.execute(text('CREATE INDEX IF NOT EXISTS idx_job_runs_autotask_ticket_id ON "job_runs" (autotask_ticket_id)')) except Exception as exc: - print(f"[migrations] job_runs table not found; skipping migrate_job_runs_autotask_ticket_fields: {exc}") + print(f"[migrations] migrate_job_runs_autotask_ticket_fields failed (continuing): {exc}") return print("[migrations] migrate_job_runs_autotask_ticket_fields completed.") @@ -1269,9 +1294,10 @@ def migrate_tickets_resolved_origin() -> None: return try: - with engine.connect() as conn: + with engine.begin() as conn: cols = _get_table_columns(conn, table) if not cols: + print("[migrations] tickets table not found; skipping migrate_tickets_resolved_origin.") return if "resolved_origin" not in cols: print("[migrations] Adding tickets.resolved_origin column...")