20170508190732_add_foreign_key_to_ci_variables.rb 518 Bytes
Newer Older
1
class AddForeignKeyToCiVariables < ActiveRecord::Migration[4.2]
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    execute <<~SQL
      DELETE FROM ci_variables
      WHERE NOT EXISTS (
        SELECT true
        FROM projects
        WHERE projects.id = ci_variables.project_id
      )
    SQL

    add_concurrent_foreign_key(:ci_variables, :projects, column: :project_id)
  end

  def down
    remove_foreign_key(:ci_variables, column: :project_id)
  end
end