summaryrefslogtreecommitdiff
path: root/db/migrate/20210622135221_add_foreign_key_for_environment_id_to_environments.rb
blob: 03991cea41c939a05b7ec46ce675b88243912895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

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

  disable_ddl_transaction!

  def up
    # `validate: false` option is passed here, because validating the existing rows fails by the orphaned deployments,
    # which will be cleaned up in https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64588.
    # The validation runs for only new records or updates, so that we can at least
    # stop creating orphaned rows.
    add_concurrent_foreign_key :deployments, :environments, column: :environment_id, on_delete: :cascade, validate: false
  end

  def down
    with_lock_retries do
      remove_foreign_key_if_exists :deployments, :environments
    end
  end
end