-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_ChatState" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "externalBotId" TEXT NOT NULL,
    "stateJson" JSONB NOT NULL,
    "lockedByBot" TEXT NOT NULL,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "ChatState_externalBotId_fkey" FOREIGN KEY ("externalBotId") REFERENCES "ExternalBot" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_ChatState" ("externalBotId", "id", "lockedByBot", "stateJson", "updatedAt", "userId") SELECT "externalBotId", "id", "lockedByBot", "stateJson", "updatedAt", "userId" FROM "ChatState";
DROP TABLE "ChatState";
ALTER TABLE "new_ChatState" RENAME TO "ChatState";
CREATE UNIQUE INDEX "ChatState_userId_externalBotId_key" ON "ChatState"("userId", "externalBotId");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
