mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 10:53:44 +08:00
deec2cf578
Previously our index was non unique, causing situations where a user could have multiple drafts stored in the table for the same exact entity. This does not properly reflect reality and needed to change as in certain cases duplicate drafts could be created causing internal data inconsistency
28 lines
609 B
Ruby
28 lines
609 B
Ruby
# frozen_string_literal: true
|
|
|
|
class AddUniqueIndexToDrafts < ActiveRecord::Migration[6.0]
|
|
def up
|
|
|
|
execute <<~SQL
|
|
DELETE FROM drafts d1
|
|
USING (
|
|
SELECT MAX(id) as id, draft_key, user_id
|
|
FROM drafts
|
|
GROUP BY draft_key, user_id
|
|
HAVING COUNT(*) > 1
|
|
) d2
|
|
WHERE
|
|
d1.draft_key = d2.draft_key AND
|
|
d1.user_id = d2.user_id AND
|
|
d1.id <> d2.id
|
|
SQL
|
|
|
|
remove_index :drafts, [:user_id, :draft_key]
|
|
add_index :drafts, [:user_id, :draft_key], unique: true
|
|
end
|
|
|
|
def down
|
|
raise ActiveRecord::IrreversibleMigration
|
|
end
|
|
end
|