Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(migrator): delete task_run_log before task_run to respect FK constraint (#18625)

p0nyda81441d6527
diff --git a/backend/migrator/migration/3.14/0010##remove_pipeline.sql b/backend/migrator/migration/3.14/0010##remove_pipeline.sql
index 819af8743a..80d83061bc 100644
--- a/backend/migrator/migration/3.14/0010##remove_pipeline.sql
+++ b/backend/migrator/migration/3.14/0010##remove_pipeline.sql
@@ -7,7 +7,12 @@ WHERE task.pipeline_id = pipeline.id;
-- Remove any orphaned tasks that don't belong to a plan (i.e. not linked to a pipeline).
-- This is required to satisfy the NOT NULL constraint on plan_id.
-DELETE FROM task_run WHERE task_id IN (SELECT id FROM task WHERE plan_id IS NULL);
+DELETE FROM task_run_log USING task_run, task
+WHERE task_run_log.task_run_id = task_run.id AND task_run.task_id = task.id AND task.plan_id IS NULL;
+
+DELETE FROM task_run USING task
+WHERE task_run.task_id = task.id AND task.plan_id IS NULL;
+
DELETE FROM task WHERE plan_id IS NULL;
ALTER TABLE task ALTER COLUMN plan_id SET NOT NULL;