summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db/post_migrate/20170526190708_create_foreign_keys_for_pipeline_stages.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/db/post_migrate/20170526190708_create_foreign_keys_for_pipeline_stages.rb b/db/post_migrate/20170526190708_create_foreign_keys_for_pipeline_stages.rb
index 40060d12d74..8a01de1fce6 100644
--- a/db/post_migrate/20170526190708_create_foreign_keys_for_pipeline_stages.rb
+++ b/db/post_migrate/20170526190708_create_foreign_keys_for_pipeline_stages.rb
@@ -6,6 +6,22 @@ class CreateForeignKeysForPipelineStages < ActiveRecord::Migration
disable_ddl_transaction!
def up
+ execute <<~SQL
+ DELETE FROM ci_stages
+ WHERE NOT EXISTS (
+ SELECT true FROM projects
+ WHERE projects.id = ci_stages.project_id
+ )
+ SQL
+
+ execute <<~SQL
+ DELETE FROM ci_builds
+ WHERE NOT EXISTS (
+ SELECT true FROM ci_stages
+ WHERE ci_stages.id = ci_builds.stage_id
+ )
+ SQL
+
add_concurrent_foreign_key :ci_stages, :projects, column: :project_id, on_delete: :cascade
add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
end