Skip to content

Commit 1e036ba

Browse files
committed
fix(migration): add deduplication step before creating index
1 parent 377993e commit 1e036ba

File tree

1 file changed

+15
-1
lines changed
  • prisma/postgresql-migrations/20251122003044_add_chat_instance_remotejid_unique

1 file changed

+15
-1
lines changed
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
-- AlterTable
1+
-- 1. Cleanup: Remove duplicate chats, keeping the most recently updated one
2+
DELETE FROM "Chat"
3+
WHERE id IN (
4+
SELECT id FROM (
5+
SELECT id,
6+
ROW_NUMBER() OVER (
7+
PARTITION BY "instanceId", "remoteJid"
8+
ORDER BY "updatedAt" DESC
9+
) as row_num
10+
FROM "Chat"
11+
) t
12+
WHERE t.row_num > 1
13+
);
14+
15+
-- 2. Create the unique index (Constraint)
216
CREATE UNIQUE INDEX "Chat_instanceId_remoteJid_key" ON "Chat"("instanceId", "remoteJid");

0 commit comments

Comments
 (0)