BigW Consortium Gitlab

20170622135728_add_unique_constraint_to_ci_variables.rb 921 Bytes
Newer Older
1 2 3 4 5 6 7 8
class AddUniqueConstraintToCiVariables < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
9 10
    unless this_index_exists?
      add_concurrent_index(:ci_variables, columns, name: index_name, unique: true)
11 12 13 14
    end
  end

  def down
15 16 17 18 19 20
    if this_index_exists?
      if Gitlab::Database.mysql? && !index_exists?(:ci_variables, :project_id)
        # Need to add this index for MySQL project_id foreign key constraint
        add_concurrent_index(:ci_variables, :project_id)
      end

21
      remove_concurrent_index(:ci_variables, columns, name: index_name)
22 23 24
    end
  end

25 26 27
  private

  def this_index_exists?
28
    index_exists?(:ci_variables, columns, name: index_name)
29 30
  end

31 32 33
  def columns
    @columns ||= [:project_id, :key, :environment_scope]
  end
34 35 36 37

  def index_name
    'index_ci_variables_on_project_id_and_key_and_environment_scope'
  end
38
end