File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed
postgresql-migrations/20251122003044_add_chat_instance_remotejid_unique Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change 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" );
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments