This commit is contained in:
2026-01-11 22:34:04 +09:00
parent 5160aacc53
commit be2ad31d2c
4 changed files with 112 additions and 4 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