diff options
author | Mayra Cabrera <mcabrera@gitlab.com> | 2018-03-08 00:44:33 +0300 |
---|---|---|
committer | Mayra Cabrera <mcabrera@gitlab.com> | 2018-03-08 00:45:50 +0300 |
commit | 3d4cfad43793d7055acdceec95477879b64e666b (patch) | |
tree | a06c779afe606ee82cc5888f96bfd1b37aab626e /db | |
parent | e9657acfc3433ea98a48d90312160e2e9cc8b173 (diff) |
Ensure orphaned rows are deleted before adding the foreign key
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20180302152117_ensure_foreign_keys_on_clusters_applications.rb | 16 | ||||
-rw-r--r-- | db/schema.rb | 4 |
2 files changed, 18 insertions, 2 deletions
diff --git a/db/migrate/20180302152117_ensure_foreign_keys_on_clusters_applications.rb b/db/migrate/20180302152117_ensure_foreign_keys_on_clusters_applications.rb index 4c2c242c01a..8298979e96a 100644 --- a/db/migrate/20180302152117_ensure_foreign_keys_on_clusters_applications.rb +++ b/db/migrate/20180302152117_ensure_foreign_keys_on_clusters_applications.rb @@ -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, diff --git a/db/schema.rb b/db/schema.rb index c5b3b21c55e..30e6006afec 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" |