From 1570c16f67c3f93bd223945e4b85fbb1694b533a Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Wed, 21 Jan 2026 11:26:03 -0500 Subject: [PATCH] Make Reaction polymorphic Add migration to replace comment_id with polymorphic reactable association (reactable_type, reactable_id), enabling reactions on both comments and cards. Co-Authored-By: Claude Opus 4.5 --- ...260121155752_make_reactions_polymorphic.rb | 27 +++++++++++++++++++ db/schema.rb | 7 ++--- db/schema_sqlite.rb | 7 ++--- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20260121155752_make_reactions_polymorphic.rb diff --git a/db/migrate/20260121155752_make_reactions_polymorphic.rb b/db/migrate/20260121155752_make_reactions_polymorphic.rb new file mode 100644 index 000000000..6c3d9cac4 --- /dev/null +++ b/db/migrate/20260121155752_make_reactions_polymorphic.rb @@ -0,0 +1,27 @@ +class MakeReactionsPolymorphic < ActiveRecord::Migration[8.0] + def change + add_column :reactions, :reactable_type, :string + add_column :reactions, :reactable_id, :uuid + + reversible do |dir| + dir.up do + execute <<~SQL + UPDATE reactions SET reactable_type = 'Comment', reactable_id = comment_id + SQL + end + + dir.down do + execute <<~SQL + UPDATE reactions SET comment_id = reactable_id WHERE reactable_type = 'Comment' + SQL + end + end + + change_column_null :reactions, :reactable_type, false + change_column_null :reactions, :reactable_id, false + + remove_column :reactions, :comment_id, :uuid + + add_index :reactions, [:reactable_type, :reactable_id] + end +end diff --git a/db/schema.rb b/db/schema.rb index 86b9f2937..fc83e3f58 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.2].define(version: 2025_12_24_092315) do +ActiveRecord::Schema[8.2].define(version: 2026_01_21_155752) do create_table "accesses", id: :uuid, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.datetime "accessed_at" t.uuid "account_id", null: false @@ -412,13 +412,14 @@ ActiveRecord::Schema[8.2].define(version: 2025_12_24_092315) do create_table "reactions", id: :uuid, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.uuid "account_id", null: false - t.uuid "comment_id", null: false t.string "content", limit: 16, null: false t.datetime "created_at", null: false + t.uuid "reactable_id", null: false + t.string "reactable_type", null: false t.uuid "reacter_id", null: false t.datetime "updated_at", null: false t.index ["account_id"], name: "index_reactions_on_account_id" - t.index ["comment_id"], name: "index_reactions_on_comment_id" + t.index ["reactable_type", "reactable_id"], name: "index_reactions_on_reactable_type_and_reactable_id" t.index ["reacter_id"], name: "index_reactions_on_reacter_id" end diff --git a/db/schema_sqlite.rb b/db/schema_sqlite.rb index b76f99865..817e37204 100644 --- a/db/schema_sqlite.rb +++ b/db/schema_sqlite.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.2].define(version: 2025_12_24_092315) do +ActiveRecord::Schema[8.2].define(version: 2026_01_21_155752) do create_table "accesses", id: :uuid, force: :cascade do |t| t.datetime "accessed_at" t.uuid "account_id", null: false @@ -412,13 +412,14 @@ ActiveRecord::Schema[8.2].define(version: 2025_12_24_092315) do create_table "reactions", id: :uuid, force: :cascade do |t| t.uuid "account_id", null: false - t.uuid "comment_id", null: false t.string "content", limit: 16, null: false t.datetime "created_at", null: false + t.uuid "reactable_id", null: false + t.string "reactable_type", limit: 255, null: false t.uuid "reacter_id", null: false t.datetime "updated_at", null: false t.index ["account_id"], name: "index_reactions_on_account_id" - t.index ["comment_id"], name: "index_reactions_on_comment_id" + t.index ["reactable_type", "reactable_id"], name: "index_reactions_on_reactable_type_and_reactable_id" t.index ["reacter_id"], name: "index_reactions_on_reacter_id" end