FIX: add unique index for 'vote_count' topic custom fields.

It shouldn't have duplicates else it breaks the code.
This commit is contained in:
Vinoth Kannan 2019-06-25 21:17:49 +05:30
parent a43a9bdf6b
commit f552e24953
1 changed files with 13 additions and 0 deletions

View File

@ -0,0 +1,13 @@
class AddVoteCountIndexToPostCustomFields < ActiveRecord::Migration[5.2]
def change
execute <<~SQL
DELETE FROM topic_custom_fields f
WHERE name = 'vote_count' AND id > (
SELECT MIN(f2.id) FROM topic_custom_fields f2
WHERE f2.topic_id = f.topic_id AND f2.name = f.name
)
SQL
add_index :topic_custom_fields, :topic_id, unique: true, where: "name = 'vote_count'"
end
end