0
0
Fork 0

Fix duplicate featured tags (#19403)

* Fix duplicate featured tags

* Add unique tag name validator

* Fix error message
This commit is contained in:
Takeshi Umeda 2022-10-22 21:30:59 +09:00 committed by GitHub
parent 1d34eff63f
commit 53e86747e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View file

@ -0,0 +1,19 @@
class AddIndexFeaturedTagsOnAccountIdAndTagId < ActiveRecord::Migration[6.1]
disable_ddl_transaction!
def up
duplicates = FeaturedTag.connection.select_all('SELECT string_agg(id::text, \',\') AS ids FROM featured_tags GROUP BY account_id, tag_id HAVING count(*) > 1').to_ary
duplicates.each do |row|
FeaturedTag.where(id: row['ids'].split(',')[0...-1]).destroy_all
end
add_index :featured_tags, [:account_id, :tag_id], unique: true, algorithm: :concurrently
remove_index :featured_tags, [:account_id], algorithm: :concurrently
end
def down
add_index :featured_tags, [:account_id], algorithm: :concurrently
remove_index :featured_tags, [:account_id, :tag_id], unique: true, algorithm: :concurrently
end
end