このコミットが含まれているのは:
2026-10-04 13:45:23 +09:00
コミット 11eedc242d
9個のファイルの変更、343行の追加、30行の削除
+63
ファイルの表示
@@ -0,0 +1,63 @@
class CreateTagDescriptions < ActiveRecord::Migration[8.0]
def change
create_table :tag_description_versions do |t|
t.references :tag, null: false, foreign_key: false
t.string :language_code, limit: 16, null: false
t.integer :version_no, null: false
t.string :event_type, null: false
t.text :body, null: false
t.string :source_language_code, limit: 16
t.integer :source_version_no
t.references :created_by_user
t.datetime :created_at, null: false
t.index [:tag_id, :language_code, :version_no], unique: true
t.foreign_key :tag_description_versions,
column: [:tag_id, :source_language_code, :source_version_no],
primary_key: [:tag_id, :language_code, :version_no]
t.check_constraint 'version_no > 0',
name: 'chk_tag_description_versions_version_no_positive'
t.check_constraint "event_type IN ('create', 'update', 'discard', 'restore')",
name: 'tag_description_versions_event_type_valid'
t.check_constraint ('(source_language_code IS NULL AND source_version_no IS NULL) OR ' +
'(source_language_code IS NOT NULL AND source_version_no IS NOT NULL)'),
name: 'chk_tag_description_versions_source_pair'
t.check_constraint 'source_language_code IS NULL OR source_language_code <> language_code',
name: 'chk_tag_description_versions_source_language_code_different'
end
create_table :tag_descriptions do |t|
t.references :tag, null: false, foreign_key: true
t.string :language_code, limit: 16, null: false, index: true
t.integer :version_no, null: false
t.text :body, null: false
t.string :source_language_code, limit: 16
t.integer :source_version_no
t.references :created_by_user, foreign_key: { to_table: :users }
t.references :updated_by_user, foreign_key: { to_table: :users }
t.timestamps
t.foreign_key :languages, column: :language_code, primary_key: :code
t.foreign_key :tag_description_versions,
column: [:tag_id, :source_language_code, :source_version_no],
primary_key: [:tag_id, :language_code, :version_no]
t.index [:tag_id, :language_code], unique: true
t.index [:source_language_code, :source_version_no]
t.check_constraint 'version_no > 0',
name: 'chk_tag_descriptions_version_no_positive'
t.check_constraint ('(source_language_code IS NULL AND source_version_no IS NULL) OR ' +
'(source_language_code IS NOT NULL AND source_version_no IS NOT NULL)'),
name: 'chk_tag_descriptions_source_pair'
t.check_constraint 'source_language_code IS NULL OR source_language_code <> language_code',
name: 'chk_tag_descriptions_source_language_code_different'
end
end
end
+104
ファイルの表示
@@ -0,0 +1,104 @@
class MigrateTagDescriptionsFromWikiPages < ActiveRecord::Migration[8.0]
class WikiPage < ActiveRecord::Base
self.table_name = 'wiki_pages'
end
class WikiRevision < ActiveRecord::Base
self.table_name = 'wiki_revisions'
end
class WikiRevisionLine < ActiveRecord::Base
self.table_name = 'wiki_revision_lines'
end
class TagName < ActiveRecord::Base
self.table_name = 'tag_names'
end
class TagDescription < ActiveRecord::Base
self.table_name = 'tag_descriptions'
end
class TagDescriptionVersion < ActiveRecord::Base
self.table_name = 'tag_description_versions'
end
def up
WikiPage.find_each do |page|
tag_name = TagName.find_by(id: page.tag_name_id, language_code: 'ja')
next unless tag_name&.tag_id
revisions = WikiRevision.where(wiki_page_id: page.id).order(:id).to_a
revisions.each_with_index do |revision, index|
version_no = index + 1
tag_id = tag_name[:tag_id]
unless tag_id
raise (
'tag_id is nil: ' +
"wiki_page_id=#{ page.id }, " +
"tag_name_id=#{ tag_name.id }, " +
"tag_name=#{ tag_name.attributes.inspect}")
end
revision_id = revision.id
say (
"wiki_page_id=#{ page.id }, " +
"tag_name_id=#{ tag_name.id }, " +
"tag_id=#{ tag_id }, " +
"revision_id=#{ revision_id }")
version = TagDescriptionVersion.new(
id: revision_id,
tag_id:,
language_code: 'ja',
version_no:,
event_type: version_no == 1 ? 'create' : 'update',
body: revision_body(revision_id),
source_language_code: nil,
source_version_no: nil,
created_by_user_id: revision.created_user_id,
created_at: revision.created_at)
say version.attributes.inspect
version.save!
end
next if page.discarded_at.present?
next if revisions.empty?
TagDescription.create!(
id: page.id,
tag_id: tag_name.tag_id,
language_code: 'ja',
version_no: revisions.length,
body: page.body,
source_language_code: nil,
source_version_no: nil,
created_by_user_id: page.created_user_id,
updated_by_user_id: page.updated_user_id,
created_at: page.created_at,
updated_at: page.updated_at)
end
end
def down
raise ActiveRecord::IrreversibleMigration, '戻せません.'
end
private
def revision_body revision_id
WikiRevisionLine
.joins('INNER JOIN wiki_lines ' +
'ON wiki_lines.id = wiki_revision_lines.wiki_line_id')
.where(wiki_revision_id: revision_id)
.order(:position)
.pluck('wiki_lines.body')
.join("\n")
end
end