From 37a9e316a8269c00487861fb5dd6f8b63d757c16 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 22 Jan 2025 11:58:34 -0300 Subject: [PATCH] Refactor database migrations and schema for `Chat` and `Setting` tables - Removed the unique constraint on `remoteJid` and `instanceId` in the `Chat` model to prevent potential migration failures due to existing duplicates. - Deleted the old migration file for adding `wavoipToken` to the `Setting` table and created a new migration that includes a check for the column's existence before adding it, ensuring smoother migration processes. - Updated migration logic to clear previous migrations related to `wavoip_token` to maintain a clean migration history. --- .../migration.sql | 17 --------------- .../migration.sql | 21 +++++++++++++++++++ prisma/postgresql-schema.prisma | 1 - 3 files changed, 21 insertions(+), 18 deletions(-) delete mode 100644 prisma/postgresql-migrations/20250116001412_add_wavoip_token_to_settings_table/migration.sql create mode 100644 prisma/postgresql-migrations/20250116001415_add_wavoip_token_to_settings_table/migration.sql diff --git a/prisma/postgresql-migrations/20250116001412_add_wavoip_token_to_settings_table/migration.sql b/prisma/postgresql-migrations/20250116001412_add_wavoip_token_to_settings_table/migration.sql deleted file mode 100644 index 594ba25c..00000000 --- a/prisma/postgresql-migrations/20250116001412_add_wavoip_token_to_settings_table/migration.sql +++ /dev/null @@ -1,17 +0,0 @@ -/* - Warnings: - - - A unique constraint covering the columns `[remoteJid,instanceId]` on the table `Chat` will be added. If there are existing duplicate values, this will fail. - -*/ --- AlterTable -ALTER TABLE "Setting" ADD COLUMN "wavoipToken" VARCHAR(100); - --- Remover registros duplicados antes de criar o índice único -DELETE FROM "Chat" a USING "Chat" b -WHERE a.ctid < b.ctid -AND a.remoteJid = b.remoteJid -AND a.instanceId = b.instanceId; - --- CreateIndex -CREATE UNIQUE INDEX "Chat_remoteJid_instanceId_key" ON "Chat"("remoteJid", "instanceId"); diff --git a/prisma/postgresql-migrations/20250116001415_add_wavoip_token_to_settings_table/migration.sql b/prisma/postgresql-migrations/20250116001415_add_wavoip_token_to_settings_table/migration.sql new file mode 100644 index 00000000..131bb642 --- /dev/null +++ b/prisma/postgresql-migrations/20250116001415_add_wavoip_token_to_settings_table/migration.sql @@ -0,0 +1,21 @@ +/* +Warnings: + +- A unique constraint covering the columns `[remoteJid,instanceId]` on the table `Chat` will be added. If there are existing duplicate values, this will fail. + +*/ + +-- Clear migrations +DELETE FROM "_prisma_migrations" WHERE "migration_name" LIKE '%_add_wavoip_token_to_settings_table'; +-- AlterTable +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'Setting' + AND column_name = 'wavoipToken' + ) THEN + ALTER TABLE "Setting" ADD COLUMN "wavoipToken" VARCHAR(100); + END IF; +END $$; \ No newline at end of file diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index 688ee5dd..a9782ce5 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -127,7 +127,6 @@ model Chat { unreadMessages Int @default(0) @@index([instanceId]) @@index([remoteJid]) - @@unique([remoteJid, instanceId]) } model Contact {