migration.sql 1.3 KB

123456789101112131415161718192021222324
  1. -- Read tracking for inbound messages. A null "readAt" on an inbound row is an
  2. -- unread message: the sidebar counts those and the inbox marks a thread's
  3. -- rows read when it is opened. Outbound rows never get a value.
  4. --
  5. -- Everything already in the tables is stamped as read, so the count starts
  6. -- from zero rather than from every message the workshop has ever received.
  7. -- One transaction: Prisma does not wrap a migration in one, and a half-run
  8. -- here would leave some channels counting history and others not.
  9. BEGIN;
  10. ALTER TABLE "sms_messages" ADD COLUMN "readAt" TIMESTAMP(3);
  11. ALTER TABLE "telegram_messages" ADD COLUMN "readAt" TIMESTAMP(3);
  12. ALTER TABLE "whatsapp_messages" ADD COLUMN "readAt" TIMESTAMP(3);
  13. UPDATE "sms_messages" SET "readAt" = "createdAt" WHERE "direction" = 'inbound';
  14. UPDATE "telegram_messages" SET "readAt" = "createdAt" WHERE "direction" = 'inbound';
  15. UPDATE "whatsapp_messages" SET "readAt" = "createdAt" WHERE "direction" = 'inbound';
  16. CREATE INDEX "sms_messages_organizationId_readAt_idx" ON "sms_messages"("organizationId", "readAt");
  17. CREATE INDEX "telegram_messages_organizationId_readAt_idx" ON "telegram_messages"("organizationId", "readAt");
  18. CREATE INDEX "whatsapp_messages_organizationId_readAt_idx" ON "whatsapp_messages"("organizationId", "readAt");
  19. COMMIT;