Skip to content

Commit cea1fa0

Browse files
Merge pull request #2247 from msantosjader/fix/postgres-chat-constraint
fix(prisma): add unique constraint to Chat model in Postgres
2 parents 38be0b4 + 1e036ba commit cea1fa0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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)
16+
CREATE UNIQUE INDEX "Chat_instanceId_remoteJid_key" ON "Chat"("instanceId", "remoteJid");

prisma/postgresql-schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ model Chat {
132132
instanceId String
133133
unreadMessages Int @default(0)
134134
135+
@@unique([instanceId, remoteJid])
135136
@@index([instanceId])
136137
@@index([remoteJid])
137138
}

0 commit comments

Comments
 (0)