From 575d63dcbabe07a7b74e0e581ca4813fda2cb499 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 10 Dec 2025 12:46:40 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#174=20=E5=AE=8C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/models/tag_implication.rb | 2 +- .../20251210123200_add_unique_index_to_tag_implications.rb | 7 +++++++ backend/db/schema.rb | 3 ++- frontend/src/components/TagDetailSidebar.tsx | 4 +++- 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb diff --git a/backend/app/models/tag_implication.rb b/backend/app/models/tag_implication.rb index 68f4083..a629764 100644 --- a/backend/app/models/tag_implication.rb +++ b/backend/app/models/tag_implication.rb @@ -2,7 +2,7 @@ class TagImplication < ApplicationRecord belongs_to :tag, class_name: 'Tag' belongs_to :parent_tag, class_name: 'Tag' - validates :tag_id, presence: true + validates :tag_id, presence: true, uniqueness: { scope: :parent_tag_id } validates :parent_tag_id, presence: true validate :parent_tag_mustnt_be_itself diff --git a/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb b/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb new file mode 100644 index 0000000..0118f6c --- /dev/null +++ b/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb @@ -0,0 +1,7 @@ +class AddUniqueIndexToTagImplications < ActiveRecord::Migration[8.0] + def change + add_index :tag_implications, [:tag_id, :parent_tag_id], + unique: true, + name: 'index_tag_implications_on_tag_id_and_parent_tag_id' + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 19ec4f6..6a26dfd 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_10_09_222200) do +ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -120,6 +120,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_10_09_222200) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["parent_tag_id"], name: "index_tag_implications_on_parent_tag_id" + t.index ["tag_id", "parent_tag_id"], name: "index_tag_implications_on_tag_id_and_parent_tag_id", unique: true t.index ["tag_id"], name: "index_tag_implications_on_tag_id" end diff --git a/frontend/src/components/TagDetailSidebar.tsx b/frontend/src/components/TagDetailSidebar.tsx index fcbfa97..6fdcbad 100644 --- a/frontend/src/components/TagDetailSidebar.tsx +++ b/frontend/src/components/TagDetailSidebar.tsx @@ -27,7 +27,9 @@ const renderTagTree = ( ) return [self, - ...(tag.children?.flatMap (child => renderTagTree (child, nestLevel + 1, key)) ?? [])] + ...(tag.children + ?.sort ((a, b) => a.name < b.name ? -1 : 1) + .flatMap (child => renderTagTree (child, nestLevel + 2, key)) ?? [])] } -- 2.34.1 From 8b4b26065ce251df6c7bc41faedb59e81f306b27 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 10 Dec 2025 20:01:14 +0900 Subject: [PATCH 2/2] =?UTF-8?q?#174=20=E3=82=A8=E3=83=A9=E3=83=BC=E5=9B=9E?= =?UTF-8?q?=E9=81=BF=E3=81=A8=E8=AA=A4=E8=A8=98=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00_add_unique_index_to_tag_implications.rb | 22 ++++++++++++++++++- frontend/src/components/TagDetailSidebar.tsx | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb b/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb index 0118f6c..681c1b5 100644 --- a/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb +++ b/backend/db/migrate/20251210123200_add_unique_index_to_tag_implications.rb @@ -1,7 +1,27 @@ class AddUniqueIndexToTagImplications < ActiveRecord::Migration[8.0] - def change + def up + execute <<~SQL + DELETE + ti1 + FROM + tag_implications ti1 + INNER JOIN + tag_implications ti2 + ON + ti1.tag_id = ti2.tag_id + AND ti1.parent_tag_id = ti2.parent_tag_id + AND ti1.id > ti2.id + ; + SQL + add_index :tag_implications, [:tag_id, :parent_tag_id], unique: true, name: 'index_tag_implications_on_tag_id_and_parent_tag_id' end + + def down + # NOTE: 重複削除は復元されなぃ. + remove_index :tag_implications, + name: 'index_tag_implications_on_tag_id_and_parent_tag_id' + end end diff --git a/frontend/src/components/TagDetailSidebar.tsx b/frontend/src/components/TagDetailSidebar.tsx index 6fdcbad..da2c3b8 100644 --- a/frontend/src/components/TagDetailSidebar.tsx +++ b/frontend/src/components/TagDetailSidebar.tsx @@ -29,7 +29,7 @@ const renderTagTree = ( return [self, ...(tag.children ?.sort ((a, b) => a.name < b.name ? -1 : 1) - .flatMap (child => renderTagTree (child, nestLevel + 2, key)) ?? [])] + .flatMap (child => renderTagTree (child, nestLevel + 1, key)) ?? [])] } -- 2.34.1