feat: Introduce dify tables with related migrations

This commit introduces a new feature, dify tables, along with related migration scripts. The `prisma/migrations/20240730152156_create_dify_tables/migration.sql` file contains the migration for creating the dify tables, while the `prisma/postgresql-schema.prisma` file has been modified to include the necessary schema changes for these tables. This change improves data organization and simplifies data management for the project.
This commit is contained in:
Davidson Gomes 2024-07-30 12:22:39 -03:00
parent 66814b403e
commit b604e4ecc7
2 changed files with 192 additions and 12 deletions

View File

@ -0,0 +1,103 @@
/*
Warnings:
- Changed the type of `botType` on the `OpenaiBot` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
*/
-- CreateEnum
CREATE TYPE "OpenaiBotType" AS ENUM ('assistant', 'chatCompletion');
-- CreateEnum
CREATE TYPE "DifyBotType" AS ENUM ('chatBot', 'textGenerator', 'agent', 'workflow');
-- DropIndex
DROP INDEX "OpenaiBot_assistantId_key";
-- AlterTable
ALTER TABLE "Message" ADD COLUMN "difySessionId" TEXT;
-- AlterTable
ALTER TABLE "OpenaiBot" DROP COLUMN "botType",
ADD COLUMN "botType" "OpenaiBotType" NOT NULL;
-- CreateTable
CREATE TABLE "Dify" (
"id" TEXT NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT true,
"botType" "DifyBotType" NOT NULL,
"apiUrl" VARCHAR(255),
"apiKey" VARCHAR(255),
"expire" INTEGER DEFAULT 0,
"keywordFinish" VARCHAR(100),
"delayMessage" INTEGER,
"unknownMessage" VARCHAR(100),
"listeningFromMe" BOOLEAN DEFAULT false,
"stopBotFromMe" BOOLEAN DEFAULT false,
"keepOpen" BOOLEAN DEFAULT false,
"debounceTime" INTEGER,
"ignoreJids" JSONB,
"triggerType" "TriggerType",
"triggerOperator" "TriggerOperator",
"triggerValue" TEXT,
"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP NOT NULL,
"instanceId" TEXT NOT NULL,
CONSTRAINT "Dify_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "DifySession" (
"id" TEXT NOT NULL,
"sessionId" VARCHAR(255) NOT NULL,
"remoteJid" VARCHAR(100) NOT NULL,
"status" "TypebotSessionStatus" NOT NULL,
"awaitUser" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP NOT NULL,
"difyId" TEXT NOT NULL,
"instanceId" TEXT NOT NULL,
CONSTRAINT "DifySession_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "DifySetting" (
"id" TEXT NOT NULL,
"expire" INTEGER DEFAULT 0,
"keywordFinish" VARCHAR(100),
"delayMessage" INTEGER,
"unknownMessage" VARCHAR(100),
"listeningFromMe" BOOLEAN DEFAULT false,
"stopBotFromMe" BOOLEAN DEFAULT false,
"keepOpen" BOOLEAN DEFAULT false,
"debounceTime" INTEGER,
"ignoreJids" JSONB,
"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP NOT NULL,
"difyIdFallback" VARCHAR(100),
"instanceId" TEXT NOT NULL,
CONSTRAINT "DifySetting_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "DifySetting_instanceId_key" ON "DifySetting"("instanceId");
-- AddForeignKey
ALTER TABLE "Message" ADD CONSTRAINT "Message_difySessionId_fkey" FOREIGN KEY ("difySessionId") REFERENCES "DifySession"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Dify" ADD CONSTRAINT "Dify_instanceId_fkey" FOREIGN KEY ("instanceId") REFERENCES "Instance"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DifySession" ADD CONSTRAINT "DifySession_difyId_fkey" FOREIGN KEY ("difyId") REFERENCES "Dify"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DifySession" ADD CONSTRAINT "DifySession_instanceId_fkey" FOREIGN KEY ("instanceId") REFERENCES "Instance"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DifySetting" ADD CONSTRAINT "DifySetting_difyIdFallback_fkey" FOREIGN KEY ("difyIdFallback") REFERENCES "Dify"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DifySetting" ADD CONSTRAINT "DifySetting_instanceId_fkey" FOREIGN KEY ("instanceId") REFERENCES "Instance"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -47,6 +47,18 @@ enum TriggerOperator {
regex
}
enum OpenaiBotType {
assistant
chatCompletion
}
enum DifyBotType {
chatBot
textGenerator
agent
workflow
}
model Instance {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
@ -83,6 +95,9 @@ model Instance {
OpenaiSession OpenaiSession[]
OpenaiSetting OpenaiSetting?
Template Template[]
Dify Dify[]
DifySession DifySession[]
DifySetting DifySetting?
}
model Session {
@ -138,6 +153,8 @@ model Message {
OpenaiSession OpenaiSession? @relation(fields: [openaiSessionId], references: [id])
openaiSessionId String?
webhookUrl String? @db.VarChar(500)
DifySession DifySession? @relation(fields: [difySessionId], references: [id])
difySessionId String?
}
model MessageUpdate {
@ -288,19 +305,19 @@ model Typebot {
}
model TypebotSession {
id String @id @default(cuid())
remoteJid String @db.VarChar(100)
pushName String? @db.VarChar(100)
sessionId String @db.VarChar(100)
id String @id @default(cuid())
remoteJid String @db.VarChar(100)
pushName String? @db.VarChar(100)
sessionId String @db.VarChar(100)
status TypebotSessionStatus
prefilledVariables Json? @db.JsonB
awaitUser Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
prefilledVariables Json? @db.JsonB
awaitUser Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
typebotId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
}
@ -350,8 +367,8 @@ model OpenaiCreds {
model OpenaiBot {
id String @id @default(cuid())
enabled Boolean @default(true) @db.Boolean
botType String @db.VarChar(100)
assistantId String? @unique @db.VarChar(255)
botType OpenaiBotType
assistantId String? @db.VarChar(255)
model String? @db.VarChar(100)
systemMessages Json? @db.JsonB
assistantMessages Json? @db.JsonB
@ -426,3 +443,63 @@ model Template {
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
}
model Dify {
id String @id @default(cuid())
enabled Boolean @default(true) @db.Boolean
botType DifyBotType
apiUrl String? @db.VarChar(255)
apiKey String? @db.VarChar(255)
expire Int? @default(0) @db.Integer
keywordFinish String? @db.VarChar(100)
delayMessage Int? @db.Integer
unknownMessage String? @db.VarChar(100)
listeningFromMe Boolean? @default(false) @db.Boolean
stopBotFromMe Boolean? @default(false) @db.Boolean
keepOpen Boolean? @default(false) @db.Boolean
debounceTime Int? @db.Integer
ignoreJids Json?
triggerType TriggerType?
triggerOperator TriggerOperator?
triggerValue String?
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
DifySession DifySession[]
DifySetting DifySetting[]
}
model DifySession {
id String @id @default(cuid())
sessionId String @db.VarChar(255)
remoteJid String @db.VarChar(100)
status TypebotSessionStatus
awaitUser Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Dify Dify @relation(fields: [difyId], references: [id], onDelete: Cascade)
difyId String
Message Message[]
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String
}
model DifySetting {
id String @id @default(cuid())
expire Int? @default(0) @db.Integer
keywordFinish String? @db.VarChar(100)
delayMessage Int? @db.Integer
unknownMessage String? @db.VarChar(100)
listeningFromMe Boolean? @default(false) @db.Boolean
stopBotFromMe Boolean? @default(false) @db.Boolean
keepOpen Boolean? @default(false) @db.Boolean
debounceTime Int? @db.Integer
ignoreJids Json?
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Fallback Dify? @relation(fields: [difyIdFallback], references: [id])
difyIdFallback String? @db.VarChar(100)
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String @unique
}