From 53e86747e49dea5e4314bc3fedef4d69ba8f338e Mon Sep 17 00:00:00 2001
From: Takeshi Umeda <noel.yoshiba@gmail.com>
Date: Sat, 22 Oct 2022 21:30:59 +0900
Subject: [PATCH] Fix duplicate featured tags (#19403)

* Fix duplicate featured tags

* Add unique tag name validator

* Fix error message
---
 app/models/featured_tag.rb                    |  1 +
 ..._featured_tags_on_account_id_and_tag_id.rb | 19 +++++++++++++++++++
 db/schema.rb                                  |  4 ++--
 3 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 db/migrate/20221021055441_add_index_featured_tags_on_account_id_and_tag_id.rb

diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb
index 4a8d7224a9..ec234a6fd9 100644
--- a/app/models/featured_tag.rb
+++ b/app/models/featured_tag.rb
@@ -60,5 +60,6 @@ class FeaturedTag < ApplicationRecord
   def validate_tag_name
     errors.add(:name, :blank) if @name.blank?
     errors.add(:name, :invalid) unless @name.match?(/\A(#{Tag::HASHTAG_NAME_RE})\z/i)
+    errors.add(:name, :taken) if FeaturedTag.by_name(@name).where(account_id: account_id).exists?
   end
 end
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 0000000000..74d7673f7d
--- /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
diff --git a/db/schema.rb b/db/schema.rb
index 3972f777ad..ed00d9e742 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2022_10_12_181003) do
+ActiveRecord::Schema.define(version: 2022_10_21_055441) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -442,7 +442,7 @@ ActiveRecord::Schema.define(version: 2022_10_12_181003) do
     t.datetime "last_status_at"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
-    t.index ["account_id"], name: "index_featured_tags_on_account_id"
+    t.index ["account_id", "tag_id"], name: "index_featured_tags_on_account_id_and_tag_id", unique: true
     t.index ["tag_id"], name: "index_featured_tags_on_tag_id"
   end
 
-- 
GitLab