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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
Generated
+4
-3
@@ -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
|
||||
|
||||
|
||||
+4
-3
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user