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,31 +9,35 @@ class AllowPrometheusAlertsPerEnvironment < ActiveRecord::Migration
disable_ddl_transaction!
def up
# Before we create the new index we need to remove it to deal with possible
# failures from previous migration.
#
# See also https://gitlab.com/gitlab-org/gitlab-ce/issues/58164
remove_concurrent_index :prometheus_alerts, INDEX_METRIC_ENVIRONMENT_NAME
rebuild_foreign_key do
# Before we create the new index we need to remove it to deal with possible
# failures from previous migration.
#
# See also https://gitlab.com/gitlab-org/gitlab-ce/issues/58164
remove_concurrent_index :prometheus_alerts, INDEX_METRIC_ENVIRONMENT_NAME
add_concurrent_index :prometheus_alerts, new_columns,
name: INDEX_METRIC_ENVIRONMENT_NAME, unique: true
add_concurrent_index :prometheus_alerts, new_columns,
name: INDEX_METRIC_ENVIRONMENT_NAME, unique: true
remove_concurrent_index :prometheus_alerts, old_columns
remove_concurrent_index :prometheus_alerts, old_columns
end
end
def down
delete_duplicate_alerts!
# Before we create the new index we need to remove it to deal with possible
# failures from previous migration.
#
# See also https://gitlab.com/gitlab-org/gitlab-ce/issues/58164
remove_concurrent_index :prometheus_alerts, old_columns
rebuild_foreign_key do
# Before we create the new index we need to remove it to deal with possible
# failures from previous migration.
#
# See also https://gitlab.com/gitlab-org/gitlab-ce/issues/58164
remove_concurrent_index :prometheus_alerts, old_columns
add_concurrent_index :prometheus_alerts, old_columns, unique: true
add_concurrent_index :prometheus_alerts, old_columns, unique: true
remove_concurrent_index :prometheus_alerts, new_columns,
name: INDEX_METRIC_ENVIRONMENT_NAME
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