Fix collections allowing multiple occurrences of the same user (#38636)
This commit is contained in:
1 parent
7b343c9567
commit
06a8379dce
3 files changed
+44
-2
No files matched your search
@@ -0,0 +1,41 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddIndexToCollectionItemsAccountIdCollectionId < ActiveRecord::Migration[8.1]
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
add_index_to_table
|
||||
remove_index :collection_items, [:account_id]
|
||||
end
|
||||
|
||||
def down
|
||||
add_index :collection_items, [:account_id]
|
||||
remove_index_from_table
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def add_index_to_table
|
||||
add_index :collection_items, [:account_id, :collection_id], unique: true, algorithm: :concurrently
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
remove_index_from_table
|
||||
deduplicate_records
|
||||
retry
|
||||
end
|
||||
|
||||
def remove_index_from_table
|
||||
remove_index :collection_items, [:account_id, :collection_id]
|
||||
end
|
||||
|
||||
def deduplicate_records
|
||||
safety_assured do
|
||||
execute <<~SQL.squish
|
||||
DELETE FROM collection_items
|
||||
WHERE id NOT IN (
|
||||
SELECT DISTINCT ON(account_id, collection_id) id FROM collection_items
|
||||
ORDER BY account_id, collection_id, id ASC
|
||||
)
|
||||
SQL
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user