From be2ad31d2c3a43220b88aa5512d60fe6d1b095b9 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sun, 11 Jan 2026 22:34:04 +0900 Subject: [PATCH] #215 --- .../20260111180600_create_tag_names.rb | 10 +++++ .../20260111192600_add_tag_name_to_tags.rb | 42 +++++++++++++++++++ ...260111204400_add_tag_name_to_wiki_pages.rb | 42 +++++++++++++++++++ backend/db/schema.rb | 22 ++++++++-- 4 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 backend/db/migrate/20260111180600_create_tag_names.rb create mode 100644 backend/db/migrate/20260111192600_add_tag_name_to_tags.rb create mode 100644 backend/db/migrate/20260111204400_add_tag_name_to_wiki_pages.rb diff --git a/backend/db/migrate/20260111180600_create_tag_names.rb b/backend/db/migrate/20260111180600_create_tag_names.rb new file mode 100644 index 0000000..5b53b36 --- /dev/null +++ b/backend/db/migrate/20260111180600_create_tag_names.rb @@ -0,0 +1,10 @@ +class CreateTagNames < ActiveRecord::Migration[7.0] + def change + create_table :tag_names do |t| + t.string :name, limit: 255, null: false + t.references :canonical, null: true, foreign_key: { to_table: :tag_names }, index: true + t.timestamps + end + add_index :tag_names, :name, unique: true + end +end diff --git a/backend/db/migrate/20260111192600_add_tag_name_to_tags.rb b/backend/db/migrate/20260111192600_add_tag_name_to_tags.rb new file mode 100644 index 0000000..c1e46fd --- /dev/null +++ b/backend/db/migrate/20260111192600_add_tag_name_to_tags.rb @@ -0,0 +1,42 @@ +class AddTagNameToTags < ActiveRecord::Migration[7.0] + class Tag < ApplicationRecord + self.table_name = 'tags' + end + + class TagName < ApplicationRecord + self.table_name = 'tag_names' + end + + def up + add_reference :tags, :tag_name, null: true, foreign_key: true, index: false, after: :id + add_index :tags, :tag_name_id, unique: true + + Tag.find_each do |tag| + name = tag.read_attribute(:name) + tn = TagName.find_or_create_by!(name:) do |r| + r.canonical_id = nil + end + tag.update_columns(tag_name_id: tn.id) + end + + change_column_null :tags, :tag_name_id, false + + remove_column :tags, :name + end + + def down + add_column :tags, :name, :string, limit: 255, null: true, after: :id + + Tag.find_each do |tag| + tag_name_id = tag.read_attribute(:tag_name_id) + name = TagName.find(tag_name_id).read_attribute(:name) + tag.update_columns(name:) + end + + change_column_null :tags, :name, false + + remove_foreign_key :tags, column: :tag_name_id + remove_index :tags, :tag_name_id + remove_column :tags, :tag_name_id + end +end diff --git a/backend/db/migrate/20260111204400_add_tag_name_to_wiki_pages.rb b/backend/db/migrate/20260111204400_add_tag_name_to_wiki_pages.rb new file mode 100644 index 0000000..4ced731 --- /dev/null +++ b/backend/db/migrate/20260111204400_add_tag_name_to_wiki_pages.rb @@ -0,0 +1,42 @@ +class AddTagNameToWikiPages < ActiveRecord::Migration[7.0] + class WikiPage < ApplicationRecord + self.table_name = 'wiki_pages' + end + + class TagName < ApplicationRecord + self.table_name = 'tag_names' + end + + def up + add_reference :wiki_pages, :tag_name, null: true, foreign_key: true, index: false, after: :id + add_index :wiki_pages, :tag_name_id, unique: true + + WikiPage.find_each do |page| + name = page.read_attribute(:title) + tn = TagName.find_or_create_by!(name:) do |r| + r.canonical_id = nil + end + page.update_columns(tag_name_id: tn.id) + end + + change_column_null :wiki_pages, :tag_name_id, false + + remove_column :wiki_pages, :title + end + + def down + add_column :wiki_pages, :title, :string, limit: 255, null: true, after: :id + + WikiPage.find_each do |page| + tag_name_id = page.read_attribute(:tag_name_id) + title = TagName.find(tag_name_id).read_attribute(:name) + page.update_columns(title:) + end + + change_column_null :wiki_pages, :title, false + + remove_foreign_key :wiki_pages, column: :tag_name_id + remove_index :wiki_pages, :tag_name_id + remove_column :wiki_pages, :tag_name_id + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 697bd8f..b6fbc98 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_12_30_143400) do +ActiveRecord::Schema[8.0].define(version: 2026_01_11_204400) 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 @@ -84,7 +84,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_30_143400) do end create_table "posts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| - t.string "title", null: false + t.string "title" t.string "url", limit: 2000, null: false t.string "thumbnail_base", limit: 2000 t.bigint "parent_id" @@ -124,6 +124,15 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_30_143400) do t.index ["tag_id"], name: "index_tag_implications_on_tag_id" end + create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.string "name", null: false + t.bigint "canonical_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["canonical_id"], name: "index_tag_names_on_canonical_id" + t.index ["name"], name: "index_tag_names_on_name", unique: true + end + create_table "tag_similarities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "tag_id", null: false t.bigint "target_tag_id", null: false @@ -133,11 +142,12 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_30_143400) do end create_table "tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| - t.string "name", null: false + t.bigint "tag_name_id", null: false t.string "category", default: "general", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "post_count", default: 0, null: false + t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true end create_table "user_ips", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| @@ -176,12 +186,13 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_30_143400) do end create_table "wiki_pages", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| - t.string "title", null: false + t.bigint "tag_name_id", null: false t.bigint "created_user_id", null: false t.bigint "updated_user_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id" + t.index ["tag_name_id"], name: "index_wiki_pages_on_tag_name_id", unique: true t.index ["updated_user_id"], name: "index_wiki_pages_on_updated_user_id" end @@ -231,12 +242,15 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_30_143400) do add_foreign_key "tag_aliases", "tags" add_foreign_key "tag_implications", "tags" add_foreign_key "tag_implications", "tags", column: "parent_tag_id" + add_foreign_key "tag_names", "tag_names", column: "canonical_id" add_foreign_key "tag_similarities", "tags" add_foreign_key "tag_similarities", "tags", column: "target_tag_id" + add_foreign_key "tags", "tag_names" add_foreign_key "user_ips", "ip_addresses" add_foreign_key "user_ips", "users" add_foreign_key "user_post_views", "posts" add_foreign_key "user_post_views", "users" + add_foreign_key "wiki_pages", "tag_names" add_foreign_key "wiki_pages", "users", column: "created_user_id" add_foreign_key "wiki_pages", "users", column: "updated_user_id" add_foreign_key "wiki_revision_lines", "wiki_lines"