A platform for community discussion. Free, open, simple.
DEV: Fix problem check tracker unique index not respecting NULLs (#29169)
Ted Johansson45c9316d7dc5
diff --git a/db/migrate/20241011054348_fix_unique_constraint_on_problem_check_trackers.rb b/db/migrate/20241011054348_fix_unique_constraint_on_problem_check_trackers.rbnew file mode 100644index 00000000000..c032c7cda2b--- /dev/null+++ b/db/migrate/20241011054348_fix_unique_constraint_on_problem_check_trackers.rb@@ -0,0 +1,30 @@+# frozen_string_literal: true++class FixUniqueConstraintOnProblemCheckTrackers < ActiveRecord::Migration[7.1]+ def up+ remove_index :problem_check_trackers, %i[identifier target], if_exists: true++ # Remove any existing duplicates that might have slipped by to prevent+ # the creation of the new unique index from failing.+ #+ execute(<<~SQL)+ DELETE FROM problem_check_trackers+ WHERE id IN(+ SELECT pct1.id+ FROM problem_check_trackers pct1+ JOIN problem_check_trackers pct2+ ON pct1.identifier = pct2.identifier+ AND pct1.target IS NULL+ AND pct2.target IS NULL+ WHERE pct1.id > pct2.id+ )+ SQL++ add_index :problem_check_trackers, %i[identifier target], unique: true, nulls_not_distinct: true+ end++ def down+ remove_index :problem_check_trackers, %i[identifier target], if_exists: true+ add_index :problem_check_trackers, %i[identifier target], unique: true+ end+end