diff options
Diffstat (limited to 'db/migrate')
-rw-r--r-- | db/migrate/20221021055441_add_index_featured_tags_on_account_id_and_tag_id.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/db/migrate/20221021055441_add_index_featured_tags_on_account_id_and_tag_id.rb b/db/migrate/20221021055441_add_index_featured_tags_on_account_id_and_tag_id.rb new file mode 100644 index 000000000..74d7673f7 --- /dev/null +++ b/db/migrate/20221021055441_add_index_featured_tags_on_account_id_and_tag_id.rb @@ -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 |