summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20200706035141_adjust_unique_index_alert_management_alerts.rb35
-rw-r--r--db/post_migrate/20200710102846_drop_index_ruby_objects_in_details_on_audit_events.rb18
-rw-r--r--db/structure.sql8
3 files changed, 57 insertions, 4 deletions
diff --git a/db/migrate/20200706035141_adjust_unique_index_alert_management_alerts.rb b/db/migrate/20200706035141_adjust_unique_index_alert_management_alerts.rb
new file mode 100644
index 00000000000..0a6e614be9a
--- /dev/null
+++ b/db/migrate/20200706035141_adjust_unique_index_alert_management_alerts.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+class AdjustUniqueIndexAlertManagementAlerts < ActiveRecord::Migration[6.0]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+ INDEX_NAME = 'index_alert_management_alerts_on_project_id_and_fingerprint'
+ NEW_INDEX_NAME = 'index_partial_am_alerts_on_project_id_and_fingerprint'
+ RESOLVED_STATUS = 2
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index(:alert_management_alerts, %w(project_id fingerprint), where: "status <> #{RESOLVED_STATUS}", name: NEW_INDEX_NAME, unique: true, using: :btree)
+ remove_concurrent_index_by_name :alert_management_alerts, INDEX_NAME
+ end
+
+ def down
+ # Nullify duplicate fingerprints, except for the newest of each match (project_id, fingerprint).
+ query = <<-SQL
+ UPDATE alert_management_alerts am
+ SET fingerprint = NULL
+ WHERE am.created_at <>
+ (SELECT MAX(created_at)
+ FROM alert_management_alerts am2
+ WHERE am.fingerprint = am2.fingerprint AND am.project_id = am2.project_id)
+ AND am.fingerprint IS NOT NULL;
+ SQL
+
+ execute(query)
+
+ remove_concurrent_index_by_name :alert_management_alerts, NEW_INDEX_NAME
+ add_concurrent_index(:alert_management_alerts, %w(project_id fingerprint), name: INDEX_NAME, unique: true, using: :btree)
+ end
+end
diff --git a/db/post_migrate/20200710102846_drop_index_ruby_objects_in_details_on_audit_events.rb b/db/post_migrate/20200710102846_drop_index_ruby_objects_in_details_on_audit_events.rb
new file mode 100644
index 00000000000..6869938466a
--- /dev/null
+++ b/db/post_migrate/20200710102846_drop_index_ruby_objects_in_details_on_audit_events.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class DropIndexRubyObjectsInDetailsOnAuditEvents < ActiveRecord::Migration[6.0]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+ INDEX_NAME = 'index_audit_events_on_ruby_object_in_details'
+
+ disable_ddl_transaction!
+
+ def up
+ remove_concurrent_index_by_name(:audit_events, INDEX_NAME)
+ end
+
+ def down
+ add_concurrent_index(:audit_events, :id, where: "details ~~ '%ruby/object%'", name: INDEX_NAME)
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index 03f5b95313e..5519f7150ca 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -18505,8 +18505,6 @@ CREATE INDEX index_alert_management_alerts_on_environment_id ON public.alert_man
CREATE INDEX index_alert_management_alerts_on_issue_id ON public.alert_management_alerts USING btree (issue_id);
-CREATE UNIQUE INDEX index_alert_management_alerts_on_project_id_and_fingerprint ON public.alert_management_alerts USING btree (project_id, fingerprint);
-
CREATE UNIQUE INDEX index_alert_management_alerts_on_project_id_and_iid ON public.alert_management_alerts USING btree (project_id, iid);
CREATE INDEX index_alert_management_alerts_on_prometheus_alert_id ON public.alert_management_alerts USING btree (prometheus_alert_id) WHERE (prometheus_alert_id IS NOT NULL);
@@ -18609,8 +18607,6 @@ CREATE INDEX index_approvers_on_user_id ON public.approvers USING btree (user_id
CREATE INDEX index_audit_events_on_entity_id_entity_type_id_desc_author_id ON public.audit_events USING btree (entity_id, entity_type, id DESC, author_id);
-CREATE INDEX index_audit_events_on_ruby_object_in_details ON public.audit_events USING btree (id) WHERE (details ~~ '%ruby/object%'::text);
-
CREATE INDEX index_award_emoji_on_awardable_type_and_awardable_id ON public.award_emoji USING btree (awardable_type, awardable_id);
CREATE INDEX index_award_emoji_on_user_id_and_name ON public.award_emoji USING btree (user_id, name);
@@ -19755,6 +19751,8 @@ CREATE INDEX index_pages_domains_on_verified_at_and_enabled_until ON public.page
CREATE INDEX index_pages_domains_on_wildcard ON public.pages_domains USING btree (wildcard);
+CREATE UNIQUE INDEX index_partial_am_alerts_on_project_id_and_fingerprint ON public.alert_management_alerts USING btree (project_id, fingerprint) WHERE (status <> 2);
+
CREATE UNIQUE INDEX index_partitioned_foreign_keys_unique_index ON public.partitioned_foreign_keys USING btree (to_table, from_table, from_column);
CREATE INDEX index_pat_on_user_id_and_expires_at ON public.personal_access_tokens USING btree (user_id, expires_at);
@@ -23641,10 +23639,12 @@ COPY "schema_migrations" (version) FROM STDIN;
20200704143633
20200704161600
20200706005325
+20200706035141
20200706154619
20200706170536
20200707071941
20200707094341
20200707095849
+20200710102846
\.