From dc8e1d093bc4ad02d91c5e49125d7d164d4fa4a2 Mon Sep 17 00:00:00 2001 From: Ivo Oskamp Date: Tue, 6 Jan 2026 16:23:49 +0100 Subject: [PATCH] Auto-commit local changes before build (2026-01-06 16:23:49) --- .last-branch | 2 +- .../src/backend/app/migrations.py | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/.last-branch b/.last-branch index 1598c06..c8b2ccf 100644 --- a/.last-branch +++ b/.last-branch @@ -1 +1 @@ -v20260106-19-missed-run-detection-threshold +v20260106-20-fix-customer-delete-fk diff --git a/containers/backupchecks/src/backend/app/migrations.py b/containers/backupchecks/src/backend/app/migrations.py index 589d718..b431a75 100644 --- a/containers/backupchecks/src/backend/app/migrations.py +++ b/containers/backupchecks/src/backend/app/migrations.py @@ -1226,6 +1226,37 @@ def migrate_object_persistence_tables() -> None: ''' ) ) + + # Ensure existing installations also have ON DELETE CASCADE for customer_object_id. + # Older schemas may have created the FK without cascade, blocking customer deletes. + conn.execute( + text( + ''' + DO $$ + BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.table_constraints tc + WHERE tc.table_name = 'job_object_links' + AND tc.constraint_type = 'FOREIGN KEY' + AND tc.constraint_name = 'job_object_links_customer_object_id_fkey' + ) THEN + ALTER TABLE job_object_links + DROP CONSTRAINT job_object_links_customer_object_id_fkey; + END IF; + + ALTER TABLE job_object_links + ADD CONSTRAINT job_object_links_customer_object_id_fkey + FOREIGN KEY (customer_object_id) + REFERENCES customer_objects(id) + ON DELETE CASCADE; + EXCEPTION + WHEN duplicate_object THEN + NULL; + END $$; + ''' + ) + ) conn.execute( text( 'CREATE INDEX IF NOT EXISTS idx_job_object_links_job_id ON job_object_links (job_id)' @@ -1253,6 +1284,36 @@ def migrate_object_persistence_tables() -> None: ''' ) ) + + # Ensure existing installations also have ON DELETE CASCADE for customer_object_id. + conn.execute( + text( + ''' + DO $$ + BEGIN + IF EXISTS ( + SELECT 1 + FROM information_schema.table_constraints tc + WHERE tc.table_name = 'run_object_links' + AND tc.constraint_type = 'FOREIGN KEY' + AND tc.constraint_name = 'run_object_links_customer_object_id_fkey' + ) THEN + ALTER TABLE run_object_links + DROP CONSTRAINT run_object_links_customer_object_id_fkey; + END IF; + + ALTER TABLE run_object_links + ADD CONSTRAINT run_object_links_customer_object_id_fkey + FOREIGN KEY (customer_object_id) + REFERENCES customer_objects(id) + ON DELETE CASCADE; + EXCEPTION + WHEN duplicate_object THEN + NULL; + END $$; + ''' + ) + ) conn.execute( text( 'CREATE INDEX IF NOT EXISTS idx_run_object_links_run_id ON run_object_links (run_id)' -- 2.45.2