feat: タグ名を別管理に変更(#215) (#219)

Merge branch 'main' into feature/215

#215 ニコニコ同期テスト

#215 テスト・ケース追加

#215 テスト・ケース追加

#215 テスト・ケース追加

#215 テスト・ケース追加

Merge remote-tracking branch 'origin/main' into feature/215

Merge branch 'main' into feature/215

#215

#215

Merge remote-tracking branch 'origin/main' into feature/215

#215

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #219
This commit was merged in pull request #219.
This commit is contained in:
2026-01-15 12:40:41 +09:00
parent 74141f2a84
commit fa2030f9a5
34 changed files with 1268 additions and 119 deletions
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,9 @@
class DropTagAliases < ActiveRecord::Migration[7.0]
def up
drop_table :tag_aliases
end
def down
raise ActiveRecord::IrreversibleMigration, '戻せません.'
end
end