Commit fa0068e4 authored by Stan Hu's avatar Stan Hu

Merge branch...

Merge branch '10138-cannot-drop-index-index_prometheus_alerts_on_project_id_and_prometheus_metric_id-needed-in-a-foreign-key-constraint' into 'master'

Resolve "Cannot drop index 'index_prometheus_alerts_on_project_id_and_prometheus_metric_id': needed in a foreign key constraint"

Closes #10138

See merge request gitlab-org/gitlab-ee!9792
parents c745f7cf 043920bb
# frozen_string_literal: true
class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration
class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration[5.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
......@@ -9,6 +9,7 @@ class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration
disable_ddl_transaction!
def up
rebuild_foreign_key do
# Before we create the new index we need to remove it to deal with possible
# failures from previous migration.
#
......@@ -20,10 +21,12 @@ class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration
remove_concurrent_index :prometheus_alerts, old_columns
end
end
def down
delete_duplicate_alerts!
rebuild_foreign_key do
# Before we create the new index we need to remove it to deal with possible
# failures from previous migration.
#
......@@ -35,6 +38,7 @@ class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration
remove_concurrent_index :prometheus_alerts, new_columns,
name: INDEX_METRIC_ENVIRONMENT_NAME
end
end
private
......@@ -58,6 +62,23 @@ class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration
end
end
# MySQL requires to drop FK for time of re-adding index
def rebuild_foreign_key
if Gitlab::Database.mysql?
remove_foreign_key_without_error :prometheus_alerts, :prometheus_metrics
remove_foreign_key_without_error :prometheus_alerts, :projects
end
yield
if Gitlab::Database.mysql?
add_concurrent_foreign_key :prometheus_alerts, :prometheus_metrics,
column: :prometheus_metric_id, on_delete: :cascade
add_concurrent_foreign_key :prometheus_alerts, :projects,
column: :project_id, on_delete: :cascade
end
end
def new_columns
[:project_id, :prometheus_metric_id, :environment_id]
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment