diff --git a/backend/db/migrate/20260921000000_delete_inactive_records_from_post_tags.rb b/backend/db/migrate/20260921000000_delete_inactive_records_from_post_tags.rb new file mode 100644 index 0000000..a1a5d58 --- /dev/null +++ b/backend/db/migrate/20260921000000_delete_inactive_records_from_post_tags.rb @@ -0,0 +1,53 @@ +class DeleteInactiveRecordsFromPostTags < ActiveRecord::Migration[8.0] + def up + execute <<~SQL + DELETE + FROM + post_tags + WHERE + discarded_at IS NOT NULL + SQL + + remove_index :post_tags, [:tag_id, :discarded_at] + remove_index :post_tags, [:post_id, :discarded_at] + remove_index :post_tags, name: 'idx_post_tags_active_unique' + remove_index :post_tags, :discarded_at + + remove_foreign_key :post_tags, column: :deleted_user_id + remove_index :post_tags, :deleted_user_id + + remove_column :post_tags, :active_unique_key + remove_column :post_tags, :is_active + remove_column :post_tags, :discarded_at + remove_column :post_tags, :deleted_user_id + remove_column :post_tags, :updated_at + + execute <<~SQL + ALTER TABLE + post_tags + MODIFY COLUMN + id BIGINT NOT NULL + SQL + + execute <<~SQL + ALTER TABLE + post_tags + DROP PRIMARY KEY + SQL + + remove_column :post_tags, :id + + execute <<~SQL + ALTER TABLE + post_tags + ADD PRIMARY KEY + (post_id, tag_id) + SQL + + remove_index :post_tags, :post_id + end + + def down + raise ActiveRecord::IrreversibleMigration, '戻せません.' + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 5ebbcc5..aa1b497 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_07_27_123600) do +ActiveRecord::Schema[8.0].define(version: 2026_09_21_000000) 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 @@ -311,23 +311,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do t.check_constraint "`begin_ms` >= 0", name: "chk_post_tag_sections_begin_ms_natural" end - create_table "post_tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + create_table "post_tags", primary_key: ["post_id", "tag_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "post_id", null: false t.bigint "tag_id", null: false t.bigint "created_user_id" - t.bigint "deleted_user_id" t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.datetime "discarded_at" - t.virtual "is_active", type: :boolean, as: "(`discarded_at` is null)", stored: true - t.virtual "active_unique_key", type: :string, as: "(case when (`discarded_at` is null) then concat(`post_id`,_utf8mb4':',`tag_id`) else NULL end)", stored: true - t.index ["active_unique_key"], name: "idx_post_tags_active_unique", unique: true t.index ["created_user_id"], name: "index_post_tags_on_created_user_id" - t.index ["deleted_user_id"], name: "index_post_tags_on_deleted_user_id" - t.index ["discarded_at"], name: "index_post_tags_on_discarded_at" - t.index ["post_id", "discarded_at"], name: "index_post_tags_on_post_id_and_discarded_at" - t.index ["post_id"], name: "index_post_tags_on_post_id" - t.index ["tag_id", "discarded_at"], name: "index_post_tags_on_tag_id_and_discarded_at" t.index ["tag_id"], name: "index_post_tags_on_tag_id" end @@ -395,19 +384,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do t.index ["user_id"], name: "index_settings_on_user_id", unique: true end - create_table "wiki_assets", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| - t.bigint "wiki_page_id", null: false - t.integer "no", null: false - t.string "alt_text" - t.binary "sha256", limit: 32, null: false - t.bigint "created_by_user_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["created_by_user_id"], name: "index_wiki_assets_on_created_by_user_id" - t.index ["wiki_page_id", "no"], name: "index_wiki_assets_on_wiki_page_id_and_no", unique: true - t.index ["wiki_page_id", "sha256"], name: "index_wiki_assets_on_wiki_page_id_and_sha256", unique: true - end - create_table "tag_implications", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "tag_id", null: false t.bigint "parent_tag_id", null: false @@ -616,6 +592,19 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do t.index ["banned_at"], name: "index_users_on_banned_at" end + create_table "wiki_assets", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| + t.bigint "wiki_page_id", null: false + t.integer "no", null: false + t.string "alt_text" + t.binary "sha256", limit: 32, null: false + t.bigint "created_by_user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["created_by_user_id"], name: "index_wiki_assets_on_created_by_user_id" + t.index ["wiki_page_id", "no"], name: "index_wiki_assets_on_wiki_page_id_and_no", unique: true + t.index ["wiki_page_id", "sha256"], name: "index_wiki_assets_on_wiki_page_id_and_sha256", unique: true + end + create_table "wiki_lines", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "sha256", limit: 64, null: false t.text "body", null: false @@ -729,7 +718,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do add_foreign_key "post_tags", "posts" add_foreign_key "post_tags", "tags" add_foreign_key "post_tags", "users", column: "created_user_id" - add_foreign_key "post_tags", "users", column: "deleted_user_id" add_foreign_key "post_versions", "posts" add_foreign_key "post_versions", "users", column: "created_by_user_id" add_foreign_key "posts", "users", column: "uploaded_user_id"