コミットを比較

...

2 コミット

作成者 SHA1 メッセージ 日付
みてるぞ 474e0695ba #281 2026-03-10 22:35:08 +09:00
みてるぞ 1e788de9a0 #281 テストまだ通ってないので要確認 2026-03-09 21:06:58 +09:00
9個のファイルの変更267行の追加24行の削除
+9 -4
ファイルの表示
@@ -152,17 +152,22 @@ class Tag < ApplicationRecord
st.post_tags.find_each do |pt| st.post_tags.find_each do |pt|
if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id) if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id)
pt.discard_by!(nil) pt.discard_by!(nil)
end
# discard 後の update! は禁止なので DB を直に更新 # discard 後の update! は禁止なので DB を直に更新
pt.update_columns(tag_id: target_tag.id, updated_at: Time.current) pt.update_columns(tag_id: target_tag.id, updated_at: Time.current)
else
pt.update!(tag: target_tag)
end
end end
tag_name = st.tag_name tag_name = st.tag_name
nico_flg = st.nico?
st.destroy! st.destroy!
tag_name.reload tag_name.reload
tag_name.update!(canonical: target_tag.tag_name)
if nico_flg
tag_name.destroy!
else
tag_name.update_columns(canonical_id: target_tag.tag_name&.id,
updated_at: Time.current)
end
end end
# 投稿件数を再集計 # 投稿件数を再集計
+11
ファイルの表示
@@ -10,6 +10,7 @@ class TagName < ApplicationRecord
validate :canonical_must_be_canonical validate :canonical_must_be_canonical
validate :alias_name_must_not_have_prefix validate :alias_name_must_not_have_prefix
validate :canonical_must_not_be_present_with_tag_or_wiki_page validate :canonical_must_not_be_present_with_tag_or_wiki_page
validate :name_must_be_sanitised
def self.canonicalise names def self.canonicalise names
names = Array(names).map { |n| n.to_s.strip }.reject(&:blank?) names = Array(names).map { |n| n.to_s.strip }.reject(&:blank?)
@@ -22,6 +23,10 @@ class TagName < ApplicationRecord
private private
def sanitise_name
self.name = TagNameSanitisationRule.sanitise(name)
end
def canonical_must_be_canonical def canonical_must_be_canonical
if canonical&.canonical_id? if canonical&.canonical_id?
errors.add :canonical, 'canonical は実体を示す必要があります.' errors.add :canonical, 'canonical は実体を示す必要があります.'
@@ -39,4 +44,10 @@ class TagName < ApplicationRecord
errors.add :canonical, 'タグもしくは Wiki の参照がある名前はエーリアスになれません.' errors.add :canonical, 'タグもしくは Wiki の参照がある名前はエーリアスになれません.'
end end
end end
def name_must_be_sanitised
if name != TagNameSanitisationRule.sanitise(name)
errors.add :name, '名前に使用できない文字が含まれてゐます.'
end
end
end end
+55
ファイルの表示
@@ -0,0 +1,55 @@
class TagNameSanitisationRule < ApplicationRecord
include Discard::Model
self.primary_key = :priority
default_scope -> { kept }
validates :source_pattern, presence: true, uniqueness: true
validate :source_pattern_must_be_regexp
class << self
def sanitise(name) =
rules.reduce(name.dup) { |name, (pattern, replacement)| name.gsub(pattern, replacement) }
def apply!
TagName.find_each do |tn|
name = sanitise(tn.name)
next if name == tn.name
TagName.transaction do
existing_tn = TagName.find_by(name:)
if existing_tn
existing_tn = existing_tn.canonical || existing_tn
next if existing_tn.id == tn.id
if existing_tn.tag
Tag.merge_tags!(existing_tn.tag, tn.tag) if tn.tag
elsif tn.tag
tn.tag.update_columns(tag_name_id: existing_tn.id, updated_at: Time.current)
end
tn.destroy!
next
end
# TagName 側の自動サニタイズを回避
tn.update_columns(name:, updated_at: Time.current)
end
end
end
private
def rules = kept.order(:priority).map { |r| [Regexp.new(r.source_pattern), r.replacement] }
end
private
def source_pattern_must_be_regexp
Regexp.new(source_pattern)
rescue RegexpError
errors.add :source_pattern, '変な正規表現だね〜(笑)'
end
end
+31
ファイルの表示
@@ -0,0 +1,31 @@
class CreateTagNameSanitisationRules < ActiveRecord::Migration[8.0]
def up
create_table :tag_name_sanitisation_rules, id: :integer, primary_key: :priority do |t|
t.string :source_pattern, null: false
t.string :replacement, null: false
t.timestamps
t.datetime :discarded_at
t.index :source_pattern, unique: true
t.index :discarded_at
end
now = ActiveRecord::Base.connection.quote(Time.current)
execute <<~SQL
INSERT INTO
tag_name_sanitisation_rules(priority, source_pattern, replacement, created_at, updated_at)
VALUES
(10, '\\\\*', '_', #{ now }, #{ now })
, (20, '\\\\?', '_', #{ now }, #{ now })
, (25, '\\\\/', '_', #{ now }, #{ now })
, (30, '_+', '_', #{ now }, #{ now })
, (40, '_$', '', #{ now }, #{ now })
, (45, '^([^:]+\\\\:)?_', '\\\\1', #{ now }, #{ now })
, (50, '^([^:]+\\\\:)?$', '\\\\1null', #{ now }, #{ now })
;
SQL
end
def down
drop_table :tag_name_sanitisation_rules
end
end
生成ファイル
+11 -1
ファイルの表示
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do ActiveRecord::Schema[8.0].define(version: 2026_03_09_123200) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -127,6 +127,16 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
t.index ["tag_id"], name: "index_tag_implications_on_tag_id" t.index ["tag_id"], name: "index_tag_implications_on_tag_id"
end end
create_table "tag_name_sanitisation_rules", primary_key: "priority", id: :integer, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "source_pattern", null: false
t.string "replacement", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "discarded_at"
t.index ["discarded_at"], name: "index_tag_name_sanitisation_rules_on_discarded_at"
t.index ["source_pattern"], name: "index_tag_name_sanitisation_rules_on_source_pattern", unique: true
end
create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.bigint "canonical_id" t.bigint "canonical_id"
+1 -1
ファイルの表示
@@ -108,7 +108,7 @@ namespace :nico do
desired_non_nico_tag_ids = [] desired_non_nico_tag_ids = []
datum['tags'].each do |raw| datum['tags'].each do |raw|
name = "nico:#{ raw }" name = TagNameSanitisationRule.sanitise("nico:#{ raw }")
tag = Tag.find_or_create_by_tag_name!(name, category: :nico) tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
desired_nico_tag_based_ids << tag.id desired_nico_tag_based_ids << tag.id
+11 -1
ファイルの表示
@@ -1,12 +1,22 @@
FactoryBot.define do FactoryBot.define do
factory :tag do factory :tag do
transient do
name { nil }
end
category { :general } category { :general }
post_count { 0 } post_count { 0 }
association :tag_name association :tag_name
after(:build) do |tag, evaluator|
tag.name = evaluator.name if evaluator.name.present?
end
trait :nico do trait :nico do
category { :nico } category { :nico }
tag_name { association(:tag_name, name: "nico:#{ SecureRandom.hex(4) }") } transient do
name { "nico:#{ SecureRandom.hex(4) }" }
end
end end
end end
end end
+100
ファイルの表示
@@ -0,0 +1,100 @@
require 'rails_helper'
RSpec.describe TagNameSanitisationRule, type: :model do
describe '.sanitise' do
before do
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
described_class.create!(priority: 20, source_pattern: 'ABC', replacement: 'abc')
end
it 'applies sanitisation rules sequentially in priority order' do
expect(described_class.sanitise('A_B_C')).to eq('abc')
end
it 'does not fail when a rule does not match' do
expect { described_class.sanitise('xyz') }.not_to raise_error
expect(described_class.sanitise('xyz')).to eq('xyz')
end
end
describe 'validations' do
it 'is invalid when source_pattern is not a valid regexp' do
rule = described_class.new(priority: 10, source_pattern: '[', replacement: '')
expect(rule).to be_invalid
expect(rule.errors[:source_pattern]).to be_present
end
end
describe '.apply!' do
before do
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
end
context 'when no conflicting tag_name exists' do
let!(:tag_name) do
TagName.create!(name: 'tmp').tap do |tn|
tn.update_columns(name: 'foo_bar', updated_at: Time.current)
end
end
it 'renames the tag_name' do
described_class.apply!
expect(tag_name.reload.name).to eq('foobar')
end
end
context 'when a conflicting canonical tag_name exists' do
let!(:existing) { TagName.create!(name: 'foobar') }
let!(:source) do
TagName.create!(name: 'tmp').tap do |tn|
tn.update_columns(name: 'foo_bar', updated_at: Time.current)
end
end
it 'deletes the source tag_name' do
described_class.apply!
expect(TagName.exists?(source.id)).to be(false)
expect(existing.reload.name).to eq('foobar')
end
end
context 'when the source tag_name has a tag and the existing one has no tag' do
let!(:existing) { TagName.create!(name: 'foobar') }
let!(:source_tag) { create(:tag, name: 'tmp', category: :general) }
let!(:source_tag_name_id) { source_tag.tag_name_id }
before do
source_tag.tag_name.update_columns(name: 'foo_bar', updated_at: Time.current)
end
it 'moves the tag to the existing tag_name' do
described_class.apply!
expect(source_tag.reload.tag_name_id).to eq(existing.id)
expect(TagName.exists?(source_tag_name_id)).to be(false)
end
end
context 'when both source and existing tag_names have tags' do
let!(:existing_tn) { TagName.create!(name: 'foobar') }
let!(:existing_tag) { Tag.create!(tag_name: existing_tn, category: :general) }
let!(:source_tn) { TagName.create!(name: 'tmp') }
let!(:source_tag) { Tag.create!(tag_name: source_tn, category: :general) }
let!(:source_tag_name_id) { source_tn.id }
before do
source_tn.update_columns(name: 'foo_bar', updated_at: Time.current)
end
it 'merges the source tag into the existing tag and deletes the source tag_name' do
expect(TagName.find_by(name: 'foobar')&.tag&.id).to eq(existing_tag.id)
expect(TagName.find_by(name: 'foo_bar')&.tag&.id).to eq(source_tag.id)
described_class.apply!
expect(Tag.exists?(source_tag.id)).to be(false)
expect(TagName.exists?(source_tag.tag_name_id)).to be(false)
end
end
end
end
+35 -14
ファイルの表示
@@ -2,10 +2,13 @@ require 'rails_helper'
RSpec.describe Tag, type: :model do RSpec.describe Tag, type: :model do
describe '.merge_tags!' do describe '.merge_tags!' do
let!(:target_tag) { create(:tag) } let!(:target_tag) { create(:tag, category: :general) }
let!(:source_tag) { create(:tag) } let!(:source_tag) { create(:tag, category: :general) }
let!(:source_tag_name) { source_tag.tag_name }
let!(:post_record) { Post.create!(url: 'https://example.com/posts/1', title: 'test post') } let!(:post_record) do
Post.create!(url: 'https://example.com/posts/1', title: 'test post')
end
context 'when merging a simple source tag' do context 'when merging a simple source tag' do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
@@ -15,7 +18,8 @@ RSpec.describe Tag, type: :model do
expect(source_post_tag.reload.tag_id).to eq(target_tag.id) expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
expect(Tag.exists?(source_tag.id)).to be(false) expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end end
end end
@@ -31,8 +35,10 @@ RSpec.describe Tag, type: :model do
expect(active.count).to eq(1) expect(active.count).to eq(1)
expect(discarded_source.discarded_at).to be_present expect(discarded_source.discarded_at).to be_present
expect(discarded_source.tag_id).to eq(target_tag.id)
expect(Tag.exists?(source_tag.id)).to be(false) expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end end
end end
@@ -45,26 +51,41 @@ RSpec.describe Tag, type: :model do
expect(Tag.exists?(target_tag.id)).to be(true) expect(Tag.exists?(target_tag.id)).to be(true)
expect(Tag.exists?(source_tag.id)).to be(false) expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_post_tag.reload.tag_id).to eq(target_tag.id) expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
expect(target_tag.reload.post_count).to eq(1)
end end
end end
context 'when aliasing the source tag_name is invalid' do context 'when aliasing the source tag_name would be invalid under normal validation' do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
let!(:wiki_page) do let!(:wiki_page) do
WikiPage.create!( WikiPage.create!(
tag_name: source_tag.tag_name, tag_name: source_tag_name,
created_user: create_admin_user!, created_user: create_admin_user!,
updated_user: create_admin_user!) updated_user: create_admin_user!
)
end end
it 'rolls back the transaction' do it 'still merges by bypassing validations' do
expect {
described_class.merge_tags!(target_tag, [source_tag]) described_class.merge_tags!(target_tag, [source_tag])
}.to raise_error(ActiveRecord::RecordInvalid)
expect(Tag.exists?(source_tag.id)).to be(true) expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_post_tag.reload.tag_id).to eq(source_tag.id) expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
expect(source_tag.tag_name.reload.canonical_id).to be_nil expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(source_tag_name.reload).not_to be_valid
end
end
context 'when merging a nico source tag' do
let!(:target_tag) { create(:tag, category: :nico, name: 'nico:foo') }
let!(:source_tag) { create(:tag, category: :nico, name: 'nico:bar') }
let!(:source_tag_name_id) { source_tag.tag_name_id }
it 'deletes the source tag_name instead of aliasing it' do
described_class.merge_tags!(target_tag, [source_tag])
expect(Tag.exists?(source_tag.id)).to be(false)
expect(TagName.exists?(source_tag_name_id)).to be(false)
expect(target_tag.reload.post_count).to eq(0)
end end
end end
end end