DEV: Fix problem check tracker unique index not respecting NULLs (#29169)
By default, when checking uniqueness on a tuple for the purposes of enforcing a unique index, PostgreSQL considers NULLs to be distinct values. Because of this we could incorrectly have multiple entries with { identifier: "rails_env", target: nil } created due to race conditions. This would then cause errors at runtime.
This commit is contained in:
parent
ca06518140
commit
45c9316d7d
|
@ -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
|
Loading…
Reference in New Issue