Commit 4fd3314c authored by Kamil Trzciński's avatar Kamil Trzciński

Fix prometheus_metric_id non-unique migration

parent 10593253
...@@ -2270,7 +2270,8 @@ ActiveRecord::Schema.define(version: 20180831164910) do ...@@ -2270,7 +2270,8 @@ ActiveRecord::Schema.define(version: 20180831164910) do
end end
add_index "prometheus_alerts", ["environment_id"], name: "index_prometheus_alerts_on_environment_id", using: :btree add_index "prometheus_alerts", ["environment_id"], name: "index_prometheus_alerts_on_environment_id", using: :btree
add_index "prometheus_alerts", ["prometheus_metric_id"], name: "index_prometheus_alerts_on_prometheus_metric_id", unique: true, using: :btree add_index "prometheus_alerts", ["project_id", "prometheus_metric_id"], name: "index_prometheus_alerts_on_project_id_and_prometheus_metric_id", unique: true, using: :btree
add_index "prometheus_alerts", ["prometheus_metric_id"], name: "index_prometheus_alerts_on_prometheus_metric_id_non_unique", using: :btree
create_table "prometheus_metrics", force: :cascade do |t| create_table "prometheus_metrics", force: :cascade do |t|
t.integer "project_id" t.integer "project_id"
......
...@@ -5,13 +5,37 @@ class AllowManyPrometheusAlerts < ActiveRecord::Migration ...@@ -5,13 +5,37 @@ class AllowManyPrometheusAlerts < ActiveRecord::Migration
disable_ddl_transaction! disable_ddl_transaction!
# We mutate `:prometheus_metric_id` into non unique one,
# and convert it into project+prometheus_metric unique
def up def up
add_concurrent_index :prometheus_alerts, [:project_id, :prometheus_metric_id], unique: true rebuild_foreign_key do
remove_concurrent_index :prometheus_alerts, :prometheus_metric_id, unique: true add_concurrent_index :prometheus_alerts, [:project_id, :prometheus_metric_id], unique: true
add_concurrent_index :prometheus_alerts, :prometheus_metric_id, name: :index_prometheus_alerts_on_prometheus_metric_id_non_unique
remove_concurrent_index :prometheus_alerts, :prometheus_metric_id, unique: true
end
end end
def down def down
add_concurrent_index :prometheus_alerts, :prometheus_metric_id, unique: true rebuild_foreign_key do
remove_concurrent_index :prometheus_alerts, [:project_id, :prometheus_metric_id], unique: true add_concurrent_index :prometheus_alerts, :prometheus_metric_id, unique: true
remove_concurrent_index :prometheus_alerts, [:project_id, :prometheus_metric_id], unique: true
remove_concurrent_index :prometheus_alerts, :prometheus_metric_id, name: :index_prometheus_alerts_on_prometheus_metric_id_non_unique
end
end
private
# 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
end
yield
if Gitlab::Database.mysql?
add_concurrent_foreign_key :prometheus_alerts, :prometheus_metrics,
column: :prometheus_metric_id, on_delete: :cascade
end
end end
end 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