fix(migration): add deduplication step before creating index

This commit is contained in:
Jader Santos 2025-11-21 22:09:15 -03:00
parent 377993e4b0
commit 1e036ba3ae

View File

@ -1,2 +1,16 @@
-- AlterTable
-- 1. Cleanup: Remove duplicate chats, keeping the most recently updated one
DELETE FROM "Chat"
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY "instanceId", "remoteJid"
ORDER BY "updatedAt" DESC
) as row_num
FROM "Chat"
) t
WHERE t.row_num > 1
);
-- 2. Create the unique index (Constraint)
CREATE UNIQUE INDEX "Chat_instanceId_remoteJid_key" ON "Chat"("instanceId", "remoteJid");