20221021055441_add_index_featured_tags_on_account_id_and_tag_id.rb 785 B

123456789101112131415161718192021
  1. # frozen_string_literal: true
  2. class AddIndexFeaturedTagsOnAccountIdAndTagId < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. def up
  5. 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
  6. duplicates.each do |row|
  7. FeaturedTag.where(id: row['ids'].split(',')[0...-1]).destroy_all
  8. end
  9. add_index :featured_tags, [:account_id, :tag_id], unique: true, algorithm: :concurrently
  10. remove_index :featured_tags, [:account_id], algorithm: :concurrently
  11. end
  12. def down
  13. add_index :featured_tags, [:account_id], algorithm: :concurrently
  14. remove_index :featured_tags, [:account_id, :tag_id], unique: true, algorithm: :concurrently
  15. end
  16. end