Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20180302152117_ensure_foreign_keys_on_clusters_applications.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d660c7cfd2d472fe9faa5d7d23cb865c025abc19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class EnsureForeignKeysOnClustersApplications < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  # rubocop:disable Cop/InBatches
  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,
        on_delete: :cascade
    end
  end

  def down
    if foreign_keys_for(:clusters_applications_ingress, :cluster_id).any?
      remove_foreign_key :clusters_applications_ingress, column: :cluster_id
    end

    if foreign_keys_for(:clusters_applications_prometheus, :cluster_id).any?
      remove_foreign_key :clusters_applications_prometheus, column: :cluster_id
    end
  end
end