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

20230706011541_add_user_id_foreign_key_to_ml_experiments.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 77059d89d300980707946deae2a0b0f2db11cd2d (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
# frozen_string_literal: true

class AddUserIdForeignKeyToMlExperiments < Gitlab::Database::Migration[2.1]
  disable_ddl_transaction!

  NEW_FK_NAME = 'fk_ml_experiments_on_user_id'
  OLD_FK_NAME = 'fk_rails_1fbc5e001f'

  def up
    add_concurrent_foreign_key(:ml_experiments, :users, column: :user_id, on_delete: :nullify,
      name: NEW_FK_NAME, validate: true)

    with_lock_retries do
      remove_foreign_key_if_exists(:ml_experiments, name: OLD_FK_NAME)
    end
  end

  def down
    unless foreign_key_exists?(:ml_experiments, :users, name: OLD_FK_NAME)
      with_lock_retries do
        execute(<<~SQL.squish)
          ALTER TABLE ml_experiments ADD CONSTRAINT #{OLD_FK_NAME} FOREIGN KEY (user_id) REFERENCES users (id)
        SQL
      end
    end

    with_lock_retries do
      remove_foreign_key_if_exists(:ml_experiments, name: NEW_FK_NAME)
    end
  end
end