From 5840987162008fbceadf1c8e354586f1826f9483 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Mon, 9 Nov 2026 00:02:32 +0900 Subject: [PATCH] #445 --- ...add_linked_tag_ids_to_nico_tag_versions.rb | 5 +++ ...igrate_linked_tags_in_nico_tag_versions.rb | 42 +++++++++++++++++++ ...move_linked_tags_from_nico_tag_versions.rb | 6 +++ backend/db/schema.rb | 5 +-- 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 backend/db/migrate/20261108000000_add_linked_tag_ids_to_nico_tag_versions.rb create mode 100644 backend/db/migrate/20261108010000_migrate_linked_tags_in_nico_tag_versions.rb create mode 100644 backend/db/migrate/20261108020000_remove_linked_tags_from_nico_tag_versions.rb diff --git a/backend/db/migrate/20261108000000_add_linked_tag_ids_to_nico_tag_versions.rb b/backend/db/migrate/20261108000000_add_linked_tag_ids_to_nico_tag_versions.rb new file mode 100644 index 0000000..ac44b0e --- /dev/null +++ b/backend/db/migrate/20261108000000_add_linked_tag_ids_to_nico_tag_versions.rb @@ -0,0 +1,5 @@ +class AddLinkedTagIdsToNicoTagVersions < ActiveRecord::Migration[8.0] + def change + add_column :nico_tag_versions, :linked_tag_ids, :text + end +end diff --git a/backend/db/migrate/20261108010000_migrate_linked_tags_in_nico_tag_versions.rb b/backend/db/migrate/20261108010000_migrate_linked_tags_in_nico_tag_versions.rb new file mode 100644 index 0000000..2e1e8d4 --- /dev/null +++ b/backend/db/migrate/20261108010000_migrate_linked_tags_in_nico_tag_versions.rb @@ -0,0 +1,42 @@ +class MigrateLinkedTagsInNicoTagVersions < ActiveRecord::Migration[8.0] + class MigrationTagVersion < ActiveRecord::Base + self.table_name = 'tag_versions' + end + + class MigrationNicoTagVersion < ActiveRecord::Base + self.table_name = 'nico_tag_versions' + end + + def up + # 除票外部タグの恢復はないので除票履歴があれば全消しでよいはず. + execute <<~SQL + DELETE + t1 + FROM + nico_tag_versions t1 + INNER JOIN + nico_tag_versions t2 + ON + t1.tag_id = t2.tag_id + AND t2.event_type = 'discard' + SQL + + MigrationNicoTagVersion.find_each do |ntv| + linked_tag_ids = ntv.linked_tags.split.map { |name| + # 厳密には一意ぢゃないけどめんだぅだからこれでいいや. + MigrationTagVersion + .order(created_at: :desc, id: :desc) + .find_by("names ->> '$.ja.name' = ?", name) + &.tag_id + }.compact.uniq.sort.join(' ') + + ntv.update_columns(linked_tag_ids:) + end + + change_column_null :nico_tag_versions, :linked_tag_ids, false + end + + def down + raise ActiveRecord::IrreversibleMigration, '戻せません.' + end +end diff --git a/backend/db/migrate/20261108020000_remove_linked_tags_from_nico_tag_versions.rb b/backend/db/migrate/20261108020000_remove_linked_tags_from_nico_tag_versions.rb new file mode 100644 index 0000000..ebb4d40 --- /dev/null +++ b/backend/db/migrate/20261108020000_remove_linked_tags_from_nico_tag_versions.rb @@ -0,0 +1,6 @@ +class RemoveLinkedTagsFromNicoTagVersions < ActiveRecord::Migration[8.0] + def change + remove_column :nico_tag_versions, :linked_tags + remove_column :nico_tag_versions, :name + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index bf72236..723fe34 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_10_15_010000) do +ActiveRecord::Schema[8.0].define(version: 2026_11_08_020000) 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 @@ -301,10 +301,9 @@ ActiveRecord::Schema[8.0].define(version: 2026_10_15_010000) do t.bigint "tag_id", null: false t.integer "version_no", null: false t.string "event_type", null: false - t.string "name", null: false - t.text "linked_tags", null: false t.datetime "created_at", null: false t.bigint "created_by_user_id" + t.text "linked_tag_ids", null: false t.index ["created_at"], name: "index_nico_tag_versions_on_created_at" t.index ["created_by_user_id", "created_at"], name: "index_nico_tag_versions_on_created_by_user_id_and_created_at", order: { created_at: :desc } t.index ["tag_id", "created_at"], name: "index_nico_tag_versions_on_tag_id_and_created_at", order: { created_at: :desc }