summaryrefslogtreecommitdiff
path: root/db/migrate/20201005092709_remove_compliance_frameworks_group_id_fk.rb
blob: dcbe48c03f963c8a1db0bff7cd968e213c2f97db (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
# frozen_string_literal: true

class RemoveComplianceFrameworksGroupIdFk < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  INDEX_NAME = 'index_compliance_management_frameworks_on_group_id_and_name'

  class TmpComplianceFramework < ActiveRecord::Base
    self.table_name = 'compliance_management_frameworks'

    include EachBatch
  end

  disable_ddl_transaction!

  def up
    TmpComplianceFramework.each_batch(of: 100) do |query|
      query.update_all('namespace_id = group_id') # Copy data in case we rolled back before...
    end

    change_column_null(:compliance_management_frameworks, :group_id, true)

    remove_foreign_key_if_exists(:compliance_management_frameworks, :namespaces, column: :group_id)
    remove_concurrent_index_by_name(:compliance_management_frameworks, INDEX_NAME)
  end

  def down
    # This is just to make the rollback possible
    TmpComplianceFramework.each_batch(of: 100) do |query|
      query.update_all('group_id = namespace_id') # The group_id column is not in used at all
    end

    change_column_null(:compliance_management_frameworks, :group_id, false)

    add_concurrent_foreign_key(:compliance_management_frameworks, :namespaces, column: :group_id, on_delete: :cascade)
    add_concurrent_index(:compliance_management_frameworks, [:group_id, :name], unique: true, name: INDEX_NAME)
  end
end