このコミットが含まれているのは:
2026-10-12 20:10:04 +09:00
コミット 2e98ed02e3
41個のファイルの変更、624行の追加、244行の削除
+56
ファイルの表示
@@ -0,0 +1,56 @@
require 'digest'
class SharedResourceIdAllocator
SPACES = {
'tags' => ['tags', 'external_tags'],
'wiki' => ['tag_descriptions', 'tag_groups'] }.freeze
LOCK_TIMEOUT = 10
def self.assign! record
space, tables = SPACES.find { |_name, names| names.include?(record.class.table_name) }
raise ArgumentError, 'Unknown shared ID resource' unless tables
connection = record.class.connection
transaction = record.class.current_transaction
raise ActiveRecord::ActiveRecordError, 'Shared ID requires a transaction' unless transaction.open?
database = connection.pool.db_config.database
name = "btrc:ids:#{ Digest::SHA256.hexdigest(database)[0, 16] }:#{ space }"
quoted_name = connection.quote(name)
acquired = record.class.uncached do
connection.select_value("SELECT GET_LOCK(#{ quoted_name }, #{ LOCK_TIMEOUT })")
end
unless acquired.to_i == 1
raise ActiveRecord::LockWaitTimeout, "Shared ID lock unavailable: #{ space }"
end
release = lambda do
result = record.class.uncached do
connection.select_value("SELECT RELEASE_LOCK(#{ quoted_name })")
end
unless result.to_i == 1
raise ActiveRecord::ActiveRecordError, "Shared ID lock release failed: #{ space }"
end
end
begin
transaction.after_commit(&release)
transaction.after_rollback(&release)
rescue StandardError
release.call
raise
end
return if record.id.present?
# Locking reads see committed maxima even after earlier repeatable reads.
maxima = record.class.uncached do
tables.map do |table|
quoted_table = connection.quote_table_name(table)
connection.select_value(
"SELECT id FROM #{ quoted_table } ORDER BY id DESC LIMIT 1 FOR UPDATE").to_i
end
end
record.id = maxima.max + 1
end
end
+22 -4
ファイルの表示
@@ -8,9 +8,9 @@ class TagDescriptionOperation
tag_id: tag.id, language_code: LANGUAGE_CODE)
page = Wiki::CompatibilityWriter.create!(
tag_name: tag.tag_name(LANGUAGE_CODE), body:, created_by_user:, message:)
tag_name: compatibility_tag_name(tag), body:, created_by_user:, message:)
revision = page.wiki_revisions.order(id: :desc).first
record_version!(tag:, revision:, body:, user: created_by_user,
record_version!(tag:, revision:, body: revision.body, user: created_by_user,
description: nil)
page
end
@@ -22,7 +22,20 @@ class TagDescriptionOperation
tag.lock!
description = TagDescription.lock.find_by!(
tag_id: tag.id, language_code: LANGUAGE_CODE)
page = WikiPage.lock.find(description.id)
tag_name = compatibility_tag_name(tag)
page = WikiPage.lock.find_by(tag_name_id: tag_name.id)
unless page
versions = TagDescriptionVersion.where(tag_id: tag.id, language_code: LANGUAGE_CODE)
if base_revision_id.present? && versions.maximum(:id) != base_revision_id.to_i
raise Wiki::Commit::Conflict, '競合が発生してゐます.'
end
page = Wiki::CompatibilityWriter.create!(
tag_name:, body:, created_by_user: updated_by_user, message:)
revision = page.current_revision
record_version!(tag:, revision:, body: revision.body,
user: updated_by_user, description:)
next revision
end
revision = Wiki::CompatibilityWriter.update!(
page:, body:, updated_by_user:, message:,
base_revision_id:)
@@ -34,6 +47,11 @@ class TagDescriptionOperation
end
end
def self.compatibility_tag_name tag
tag.tag_name(LANGUAGE_CODE) || TagName.find_by!(id: tag.tag_name_id,
language_code: LANGUAGE_CODE)
end
def self.record_version! tag:, revision:, body:, user:, description:
version_no = description ? description.version_no + 1 : 1
TagDescriptionVersion.create!(
@@ -44,7 +62,7 @@ class TagDescriptionOperation
description.update!(body:, version_no:, updated_by_user_id: user.id)
else
TagDescription.create!(
id: revision.wiki_page_id, tag_id: tag.id,
tag_id: tag.id,
language_code: LANGUAGE_CODE, version_no:, body:,
created_by_user_id: user.id, updated_by_user_id: user.id)
end
+1 -1
ファイルの表示
@@ -349,7 +349,7 @@ class TagGroupOperation
tag_name.primary_flg = true
tag_name.auto_generated = false
tag_name.save!
Wiki::Commit.create_legacy_content!(tag_name:, body:, created_by_user: actor)
Wiki::CompatibilityWriter.create!(tag_name:, body:, created_by_user: actor)
end
nil
end
+9 -5
ファイルの表示
@@ -8,12 +8,13 @@ module Wiki
end
def self.create_content! tag_name:, body:, created_by_user:, message: nil
if tag_name.tag && tag_name.language_code == 'ja'
if tag_name.tag && tag_name.language_code == 'ja' && !tag_name.name.include?(':')
return TagDescriptionOperation.create!(
tag: tag_name.tag, body:, created_by_user:, message:)
end
create_legacy_content!(tag_name:, body:, created_by_user:, message:)
tag_name.errors.add(:base, :invalid)
raise ActiveRecord::RecordInvalid, tag_name
end
def self.create_legacy_content! tag_name:, body:, created_by_user:, message: nil
@@ -42,14 +43,17 @@ module Wiki
end
def self.content! page:, body:, created_user:, message: nil, base_revision_id: nil
description = TagDescription.find_by(id: page.id)
if description
description = TagDescription.find_by(tag_id: page.tag_name.tag_id, language_code: 'ja')
description ||= TagDescription.joins(:tag).find_by(
tags: { tag_name_id: page.tag_name_id }, language_code: 'ja')
if description && !page.title.include?(':')
return TagDescriptionOperation.update!(
tag: description.tag, body:, updated_by_user: created_user,
message:, base_revision_id:)
end
legacy_content!(page:, body:, created_user:, message:, base_revision_id:)
page.errors.add(:base, :invalid)
raise ActiveRecord::RecordInvalid, page
end
def self.legacy_content! page:, body:, created_user:, message: nil,
+13 -3
ファイルの表示
@@ -1,8 +1,16 @@
module Wiki
class CompatibilityWriter
def self.create! tag_name:, body:, created_by_user:, message: nil
Commit.create_legacy_content!(
tag_name:, body:, created_by_user:, message:)
ApplicationRecord.transaction do
tag_name.lock!
page = WikiPage.lock.find_by(tag_name_id: tag_name.id)
if page
update!(page:, body:, updated_by_user: created_by_user, message:)
page
else
Commit.create_legacy_content!(tag_name:, body:, created_by_user:, message:)
end
end
end
def self.update! page:, body:, updated_by_user:, message: nil,
@@ -23,7 +31,9 @@ module Wiki
end
end
return if Commit.normalise_body(page.body) == normalised
if Commit.normalise_body(page.body) == normalised && page.wiki_revisions.exists?
return
end
Commit.legacy_content!(
page:, body: normalised, created_user: updated_by_user, message:,