summaryrefslogtreecommitdiff
path: root/db/migrate/20220301003502_add_security_orchestration_policy_configuration_namespace_index.rb
blob: de6b36faa6588a045daf5f7f27e7b5dfc82f2b22 (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
# frozen_string_literal: true

class AddSecurityOrchestrationPolicyConfigurationNamespaceIndex < Gitlab::Database::Migration[1.0]
  disable_ddl_transaction!
  INDEX_NAME = 'partial_index_sop_configs_on_namespace_id'

  def up
    add_concurrent_index :security_orchestration_policy_configurations, :namespace_id, unique: true, name: INDEX_NAME, where: 'namespace_id IS NOT NULL'
    add_concurrent_foreign_key :security_orchestration_policy_configurations, :namespaces, column: :namespace_id, on_delete: :cascade, reverse_lock_order: true

    add_check_constraint :security_orchestration_policy_configurations,
      "((project_id IS NULL) != (namespace_id IS NULL))",
      :cop_configs_project_or_namespace_existence
  end

  def down
    exec_query 'DELETE FROM security_orchestration_policy_configurations WHERE namespace_id IS NOT NULL'

    remove_check_constraint :security_orchestration_policy_configurations, :cop_configs_project_or_namespace_existence

    with_lock_retries do
      remove_foreign_key_if_exists :security_orchestration_policy_configurations, column: :namespace_id
    end

    remove_concurrent_index_by_name :security_orchestration_policy_configurations, INDEX_NAME
  end
end