From 5e2815932655af7085322388095984e082c1199e Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 11 Mar 2026 12:43:08 +0900 Subject: [PATCH] #287 --- backend/app/models/tag.rb | 6 +++++- .../20260311123100_add_discarded_at_to_tags.rb | 6 ++++++ backend/db/schema.rb | 14 +++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 backend/db/migrate/20260311123100_add_discarded_at_to_tags.rb diff --git a/backend/app/models/tag.rb b/backend/app/models/tag.rb index eaf51da..d083dc6 100644 --- a/backend/app/models/tag.rb +++ b/backend/app/models/tag.rb @@ -1,8 +1,12 @@ class Tag < ApplicationRecord + include Discard::Model + class NicoTagNormalisationError < ArgumentError ; end + default_scope -> { kept } + has_many :post_tags, inverse_of: :tag has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag' @@ -160,7 +164,7 @@ class Tag < ApplicationRecord end tag_name = st.tag_name - st.destroy! + st.discard! tag_name.reload tag_name.update!(canonical: target_tag.tag_name) end diff --git a/backend/db/migrate/20260311123100_add_discarded_at_to_tags.rb b/backend/db/migrate/20260311123100_add_discarded_at_to_tags.rb new file mode 100644 index 0000000..4bd5c5d --- /dev/null +++ b/backend/db/migrate/20260311123100_add_discarded_at_to_tags.rb @@ -0,0 +1,6 @@ +class AddDiscardedAtToTags < ActiveRecord::Migration[8.0] + def change + add_column :tags, :discarded_at, :datetime + add_index :tags, :discarded_at + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 8077fac..b6ffbd3 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # 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_11_123100) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "name", 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" 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| t.string "name", null: false t.bigint "canonical_id" @@ -150,6 +160,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "post_count", default: 0, null: false + t.datetime "discarded_at" + t.index ["discarded_at"], name: "index_tags_on_discarded_at" t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true end