Commit 043920bb authored by Peter Leitzen's avatar Peter Leitzen

Rebuild foreign keys for MySQL to fix broken master

MySQL requires to drop FK for time of re-adding index
parent 3f02e805
# 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