Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: rename query_history project column before adding project FK (#19635)
p0ny3097bd38c24e
diff --git a/backend/migrator/migration/3.17/0007##fix_project_fk_references.sql b/backend/migrator/migration/3.17/0007##fix_project_fk_references.sqlindex 3e1113b3e3..fed048ac11 100644--- a/backend/migrator/migration/3.17/0007##fix_project_fk_references.sql+++ b/backend/migrator/migration/3.17/0007##fix_project_fk_references.sql@@ -5,17 +5,18 @@ DO $$DECLAREt text;BEGIN+ -- Rename query_history.project_id to project before adding the FK.+ IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'query_history' AND column_name = 'project_id') THEN+ ALTER TABLE query_history RENAME COLUMN project_id TO project;+ END IF;+-- Add direct project FK to tables that only had composite FKs.FOREACH t IN ARRAY ARRAY['plan_check_run', 'plan_webhook_delivery', 'task_run', 'task_run_log', 'issue_comment', 'query_history'] LOOP- IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = t || '_project_fkey') THEN+ IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = t AND column_name = 'project')+ AND NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = t || '_project_fkey') THENEXECUTE format('ALTER TABLE %I ADD CONSTRAINT %I FOREIGN KEY (project) REFERENCES project(resource_id)', t, t || '_project_fkey');END IF;END LOOP;-- -- Rename query_history.project_id to project.- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'query_history' AND column_name = 'project_id') THEN- ALTER TABLE query_history RENAME COLUMN project_id TO project;- END IF;END $$;-- Recreate the index with the new column name.