Commit 3d4cfad4 authored by Mayra Cabrera's avatar Mayra Cabrera

Ensure orphaned rows are deleted before adding the foreign key

parent e9657acf
......@@ -9,12 +9,28 @@ class EnsureForeignKeysOnClustersApplications < ActiveRecord::Migration
disable_ddl_transaction!
def up
existing = Clusters::Cluster
.joins(:application_ingress)
.where('clusters.id = clusters_applications_ingress.cluster_id')
Clusters::Applications::Ingress.where('NOT EXISTS (?)', existing).in_batches do |batch|
batch.delete_all
end
unless foreign_keys_for(:clusters_applications_ingress, :cluster_id).any?
add_concurrent_foreign_key :clusters_applications_ingress, :clusters,
column: :cluster_id,
on_delete: :cascade
end
existing = Clusters::Cluster
.joins(:application_prometheus)
.where('clusters.id = clusters_applications_prometheus.cluster_id')
Clusters::Applications::Ingress.where('NOT EXISTS (?)', existing).in_batches do |batch|
batch.delete_all
end
unless foreign_keys_for(:clusters_applications_prometheus, :cluster_id).any?
add_concurrent_foreign_key :clusters_applications_prometheus, :clusters,
column: :cluster_id,
......
......@@ -2028,8 +2028,8 @@ ActiveRecord::Schema.define(version: 20180307012445) do
add_foreign_key "cluster_providers_gcp", "clusters", on_delete: :cascade
add_foreign_key "clusters", "users", on_delete: :nullify
add_foreign_key "clusters_applications_helm", "clusters", on_delete: :cascade
add_foreign_key "clusters_applications_ingress", "clusters", on_delete: :cascade
add_foreign_key "clusters_applications_prometheus", "clusters", on_delete: :cascade
add_foreign_key "clusters_applications_ingress", "clusters", name: "fk_753a7b41c1", on_delete: :cascade
add_foreign_key "clusters_applications_prometheus", "clusters", name: "fk_557e773639", on_delete: :cascade
add_foreign_key "clusters_applications_runners", "ci_runners", column: "runner_id", name: "fk_02de2ded36", on_delete: :nullify
add_foreign_key "clusters_applications_runners", "clusters", on_delete: :cascade
add_foreign_key "container_repositories", "projects"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment