Remove duplicate reactions

This commit is contained in:
Karina Kwiatek 2023-12-20 10:28:49 +00:00
parent 52e17d0f68
commit 563926895f
2 changed files with 22 additions and 1 deletions

View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
class RemoveDuplicateReactions < ActiveRecord::Migration[7.0]
def up
execute <<~SQUIRREL
DELETE FROM reactions
WHERE id IN (
SELECT id FROM (
SELECT id, row_number() over (PARTITION BY parent_type, parent_id, user_id ORDER BY id) AS row_number FROM reactions
)s WHERE row_number >= 2
)
SQUIRREL
add_index :reactions, %i[parent_type parent_id user_id], unique: true
end
def down
remove_index :reactions, %i[parent_type parent_id user_id]
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_12_09_212629) do
ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -149,6 +149,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_09_212629) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["parent_id", "parent_type"], name: "index_reactions_on_parent_id_and_parent_type"
t.index ["parent_type", "parent_id", "user_id"], name: "index_reactions_on_parent_type_and_parent_id_and_user_id", unique: true
t.index ["user_id", "created_at"], name: "index_reactions_on_user_id_and_created_at"
end