diff --git a/db/migrations/20210726100838_init/migration.sql b/db/migrations/20210726100838_init/migration.sql deleted file mode 100644 index e289462..0000000 --- a/db/migrations/20210726100838_init/migration.sql +++ /dev/null @@ -1,57 +0,0 @@ --- CreateTable -CREATE TABLE "User" ( - "id" SERIAL NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - "name" TEXT, - "email" TEXT NOT NULL, - "hashedPassword" TEXT, - "role" TEXT NOT NULL DEFAULT E'USER', - - PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "Session" ( - "id" SERIAL NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - "expiresAt" TIMESTAMP(3), - "handle" TEXT NOT NULL, - "hashedSessionToken" TEXT, - "antiCSRFToken" TEXT, - "publicData" TEXT, - "privateData" TEXT, - "userId" INTEGER, - - PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "Token" ( - "id" SERIAL NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - "hashedToken" TEXT NOT NULL, - "type" TEXT NOT NULL, - "expiresAt" TIMESTAMP(3) NOT NULL, - "sentTo" TEXT NOT NULL, - "userId" INTEGER NOT NULL, - - PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "User.email_unique" ON "User"("email"); - --- CreateIndex -CREATE UNIQUE INDEX "Session.handle_unique" ON "Session"("handle"); - --- CreateIndex -CREATE UNIQUE INDEX "Token.hashedToken_type_unique" ON "Token"("hashedToken", "type"); - --- AddForeignKey -ALTER TABLE "Session" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Token" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/migrations/20210727115631_import_models/migration.sql b/db/migrations/20210727115631_import_models/migration.sql deleted file mode 100644 index 7b74a5a..0000000 --- a/db/migrations/20210727115631_import_models/migration.sql +++ /dev/null @@ -1,137 +0,0 @@ -/* - Warnings: - - - The primary key for the `Session` table will be changed. If it partially fails, the table could be left without primary key constraint. - - The primary key for the `Token` table will be changed. If it partially fails, the table could be left without primary key constraint. - - The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint. - - A unique constraint covering the columns `[hashedToken,type]` on the table `Token` will be added. If there are existing duplicate values, this will fail. - - Changed the type of `type` on the `Token` 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 "TokenType" AS ENUM ('RESET_PASSWORD'); - --- CreateEnum -CREATE TYPE "Direction" AS ENUM ('Inbound', 'Outbound'); - --- CreateEnum -CREATE TYPE "MessageStatus" AS ENUM ('Queued', 'Sending', 'Sent', 'Failed', 'Delivered', 'Undelivered', 'Receiving', 'Received', 'Accepted', 'Scheduled', 'Read', 'PartiallyDelivered', 'Canceled'); - --- CreateEnum -CREATE TYPE "CallStatus" AS ENUM ('Queued', 'Ringing', 'InProgress', 'Completed', 'Busy', 'Failed', 'NoAnswer', 'Canceled'); - --- DropForeignKey -ALTER TABLE "Session" DROP CONSTRAINT "Session_userId_fkey"; - --- DropForeignKey -ALTER TABLE "Token" DROP CONSTRAINT "Token_userId_fkey"; - --- AlterTable -ALTER TABLE "Session" DROP CONSTRAINT "Session_pkey", -ALTER COLUMN "id" DROP DEFAULT, -ALTER COLUMN "id" SET DATA TYPE TEXT, -ALTER COLUMN "createdAt" SET DATA TYPE TIMESTAMPTZ, -ALTER COLUMN "updatedAt" SET DATA TYPE TIMESTAMPTZ, -ALTER COLUMN "expiresAt" SET DATA TYPE TIMESTAMPTZ, -ALTER COLUMN "userId" SET DATA TYPE TEXT, -ADD PRIMARY KEY ("id"); -DROP SEQUENCE "Session_id_seq"; - --- AlterTable -ALTER TABLE "Token" DROP CONSTRAINT "Token_pkey", -ALTER COLUMN "id" DROP DEFAULT, -ALTER COLUMN "id" SET DATA TYPE TEXT, -ALTER COLUMN "createdAt" SET DATA TYPE TIMESTAMPTZ, -ALTER COLUMN "updatedAt" SET DATA TYPE TIMESTAMPTZ, -DROP COLUMN "type", -ADD COLUMN "type" "TokenType" NOT NULL, -ALTER COLUMN "expiresAt" SET DATA TYPE TIMESTAMPTZ, -ALTER COLUMN "userId" SET DATA TYPE TEXT, -ADD PRIMARY KEY ("id"); -DROP SEQUENCE "Token_id_seq"; - --- AlterTable -ALTER TABLE "User" DROP CONSTRAINT "User_pkey", -ALTER COLUMN "id" DROP DEFAULT, -ALTER COLUMN "id" SET DATA TYPE TEXT, -ALTER COLUMN "createdAt" SET DATA TYPE TIMESTAMPTZ, -ALTER COLUMN "updatedAt" SET DATA TYPE TIMESTAMPTZ, -ADD PRIMARY KEY ("id"); -DROP SEQUENCE "User_id_seq"; - --- CreateTable -CREATE TABLE "Customer" ( - "id" TEXT NOT NULL, - "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMPTZ NOT NULL, - "encryptionKey" TEXT NOT NULL, - "accountSid" TEXT, - "authToken" TEXT, - "twimlAppSid" TEXT, - "paddleCustomerId" TEXT, - "paddleSubscriptionId" TEXT, - - PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "Message" ( - "id" TEXT NOT NULL, - "sentAt" TIMESTAMPTZ NOT NULL, - "content" TEXT NOT NULL, - "from" TEXT NOT NULL, - "to" TEXT NOT NULL, - "direction" "Direction" NOT NULL, - "status" "MessageStatus" NOT NULL, - "twilioSid" TEXT, - "customerId" TEXT NOT NULL, - - PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "PhoneCall" ( - "id" TEXT NOT NULL, - "createdAt" TIMESTAMPTZ NOT NULL, - "twilioSid" TEXT NOT NULL, - "from" TEXT NOT NULL, - "to" TEXT NOT NULL, - "status" "CallStatus" NOT NULL, - "direction" "Direction" NOT NULL, - "duration" TEXT NOT NULL, - "customerId" TEXT NOT NULL, - - PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "PhoneNumber" ( - "id" TEXT NOT NULL, - "createdAt" TIMESTAMPTZ NOT NULL, - "phoneNumberSid" TEXT NOT NULL, - "phoneNumber" TEXT NOT NULL, - "customerId" TEXT NOT NULL, - - PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "Token.hashedToken_type_unique" ON "Token"("hashedToken", "type"); - --- AddForeignKey -ALTER TABLE "Session" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Token" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Customer" ADD FOREIGN KEY ("id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Message" ADD FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "PhoneNumber" ADD FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/migrations/20210727125716_user_role_enum/migration.sql b/db/migrations/20210727125716_user_role_enum/migration.sql deleted file mode 100644 index faff467..0000000 --- a/db/migrations/20210727125716_user_role_enum/migration.sql +++ /dev/null @@ -1,12 +0,0 @@ -/* - Warnings: - - - The `role` column on the `User` table would be dropped and recreated. This will lead to data loss if there is data in the column. - -*/ --- CreateEnum -CREATE TYPE "Role" AS ENUM ('USER', 'ADMIN'); - --- AlterTable -ALTER TABLE "User" DROP COLUMN "role", -ADD COLUMN "role" "Role" NOT NULL DEFAULT E'USER'; diff --git a/db/migrations/20210728061812_default_created_at_to_now/migration.sql b/db/migrations/20210728061812_default_created_at_to_now/migration.sql deleted file mode 100644 index ff64b74..0000000 --- a/db/migrations/20210728061812_default_created_at_to_now/migration.sql +++ /dev/null @@ -1,5 +0,0 @@ --- AlterTable -ALTER TABLE "PhoneCall" ALTER COLUMN "createdAt" SET DEFAULT CURRENT_TIMESTAMP; - --- AlterTable -ALTER TABLE "PhoneNumber" ALTER COLUMN "createdAt" SET DEFAULT CURRENT_TIMESTAMP; diff --git a/db/migrations/20210801151308_web_push_subscriptions/migration.sql b/db/migrations/20210801151308_web_push_subscriptions/migration.sql deleted file mode 100644 index c4409a2..0000000 --- a/db/migrations/20210801151308_web_push_subscriptions/migration.sql +++ /dev/null @@ -1,16 +0,0 @@ --- CreateTable -CREATE TABLE "NotificationSubscription" ( - "id" TEXT NOT NULL, - "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMPTZ NOT NULL, - "endpoint" TEXT NOT NULL, - "expirationTime" INTEGER, - "keys_p256dh" TEXT NOT NULL, - "keys_auth" TEXT NOT NULL, - "customerId" TEXT NOT NULL, - - PRIMARY KEY ("id") -); - --- AddForeignKey -ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/migrations/20210801161503_unique_endpoint_for_notification_subscription/migration.sql b/db/migrations/20210801161503_unique_endpoint_for_notification_subscription/migration.sql deleted file mode 100644 index ed62261..0000000 --- a/db/migrations/20210801161503_unique_endpoint_for_notification_subscription/migration.sql +++ /dev/null @@ -1,8 +0,0 @@ -/* - Warnings: - - - A unique constraint covering the columns `[endpoint]` on the table `NotificationSubscription` will be added. If there are existing duplicate values, this will fail. - -*/ --- CreateIndex -CREATE UNIQUE INDEX "NotificationSubscription.endpoint_unique" ON "NotificationSubscription"("endpoint"); diff --git a/db/migrations/20210804092849_add_orgs/migration.sql b/db/migrations/20210804092849_add_orgs/migration.sql deleted file mode 100644 index fa43942..0000000 --- a/db/migrations/20210804092849_add_orgs/migration.sql +++ /dev/null @@ -1,148 +0,0 @@ -/* - Warnings: - - - You are about to drop the column `customerId` on the `Message` table. All the data in the column will be lost. - - You are about to drop the column `twilioSid` on the `Message` table. All the data in the column will be lost. - - You are about to drop the column `customerId` on the `NotificationSubscription` table. All the data in the column will be lost. - - You are about to drop the column `customerId` on the `PhoneCall` table. All the data in the column will be lost. - - You are about to drop the column `twilioSid` on the `PhoneCall` table. All the data in the column will be lost. - - You are about to drop the column `customerId` on the `PhoneNumber` table. All the data in the column will be lost. - - You are about to drop the column `phoneNumber` on the `PhoneNumber` table. All the data in the column will be lost. - - You are about to drop the column `phoneNumberSid` on the `PhoneNumber` table. All the data in the column will be lost. - - The `role` column on the `User` table would be dropped and recreated. This will lead to data loss if there is data in the column. - - You are about to drop the `Customer` table. If the table is not empty, all the data it contains will be lost. - - A unique constraint covering the columns `[phoneNumberId,id]` on the table `Message` will be added. If there are existing duplicate values, this will fail. - - A unique constraint covering the columns `[phoneNumberId,id]` on the table `PhoneCall` will be added. If there are existing duplicate values, this will fail. - - A unique constraint covering the columns `[organizationId,id]` on the table `PhoneNumber` will be added. If there are existing duplicate values, this will fail. - - Added the required column `phoneNumberId` to the `Message` table without a default value. This is not possible if the table is not empty. - - Added the required column `organizationId` to the `NotificationSubscription` table without a default value. This is not possible if the table is not empty. - - Added the required column `phoneNumberId` to the `NotificationSubscription` table without a default value. This is not possible if the table is not empty. - - Added the required column `phoneNumberId` to the `PhoneCall` table without a default value. This is not possible if the table is not empty. - - Added the required column `number` to the `PhoneNumber` table without a default value. This is not possible if the table is not empty. - - Added the required column `organizationId` to the `PhoneNumber` table without a default value. This is not possible if the table is not empty. - -*/ --- CreateEnum -CREATE TYPE "MembershipRole" AS ENUM ('OWNER', 'ADMIN', 'USER'); - --- CreateEnum -CREATE TYPE "GlobalRole" AS ENUM ('SUPERADMIN', 'CUSTOMER'); - --- AlterEnum -ALTER TYPE "MessageStatus" ADD VALUE 'Error'; - --- DropForeignKey -ALTER TABLE "Customer" DROP CONSTRAINT "Customer_id_fkey"; - --- DropForeignKey -ALTER TABLE "Message" DROP CONSTRAINT "Message_customerId_fkey"; - --- DropForeignKey -ALTER TABLE "NotificationSubscription" DROP CONSTRAINT "NotificationSubscription_customerId_fkey"; - --- DropForeignKey -ALTER TABLE "PhoneCall" DROP CONSTRAINT "PhoneCall_customerId_fkey"; - --- DropForeignKey -ALTER TABLE "PhoneNumber" DROP CONSTRAINT "PhoneNumber_customerId_fkey"; - --- AlterTable -ALTER TABLE "Message" DROP COLUMN "customerId", -DROP COLUMN "twilioSid", -ADD COLUMN "phoneNumberId" TEXT NOT NULL; - --- AlterTable -ALTER TABLE "NotificationSubscription" DROP COLUMN "customerId", -ADD COLUMN "organizationId" TEXT NOT NULL, -ADD COLUMN "phoneNumberId" TEXT NOT NULL; - --- AlterTable -ALTER TABLE "PhoneCall" DROP COLUMN "customerId", -DROP COLUMN "twilioSid", -ADD COLUMN "phoneNumberId" TEXT NOT NULL; - --- AlterTable -ALTER TABLE "PhoneNumber" DROP COLUMN "customerId", -DROP COLUMN "phoneNumber", -DROP COLUMN "phoneNumberSid", -ADD COLUMN "number" TEXT NOT NULL, -ADD COLUMN "organizationId" TEXT NOT NULL; - --- AlterTable -ALTER TABLE "User" DROP COLUMN "role", -ADD COLUMN "role" "GlobalRole" NOT NULL DEFAULT E'CUSTOMER'; - --- DropTable -DROP TABLE "Customer"; - --- CreateTable -CREATE TABLE "TwilioCredentials" ( - "accountSid" TEXT NOT NULL, - "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMPTZ NOT NULL, - "authToken" TEXT NOT NULL, - "twimlAppSid" TEXT, - "organizationId" TEXT NOT NULL, - - PRIMARY KEY ("accountSid") -); - --- CreateTable -CREATE TABLE "Organization" ( - "id" TEXT NOT NULL, - "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMPTZ NOT NULL, - "encryptionKey" TEXT NOT NULL, - "paddleCustomerId" TEXT, - "paddleSubscriptionId" TEXT, - - PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "Membership" ( - "id" TEXT NOT NULL, - "role" "MembershipRole" NOT NULL, - "organizationId" TEXT NOT NULL, - "userId" TEXT, - "invitedName" TEXT, - "invitedEmail" TEXT, - - PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "Membership.organizationId_invitedEmail_unique" ON "Membership"("organizationId", "invitedEmail"); - --- CreateIndex -CREATE UNIQUE INDEX "Message.phoneNumberId_id_unique" ON "Message"("phoneNumberId", "id"); - --- CreateIndex -CREATE UNIQUE INDEX "PhoneCall.phoneNumberId_id_unique" ON "PhoneCall"("phoneNumberId", "id"); - --- CreateIndex -CREATE UNIQUE INDEX "PhoneNumber.organizationId_id_unique" ON "PhoneNumber"("organizationId", "id"); - --- AddForeignKey -ALTER TABLE "TwilioCredentials" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Membership" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Membership" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Message" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "PhoneNumber" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/migrations/20210805150436_delete_twiliocredentials_entity/migration.sql b/db/migrations/20210805150436_delete_twiliocredentials_entity/migration.sql deleted file mode 100644 index 94b03dd..0000000 --- a/db/migrations/20210805150436_delete_twiliocredentials_entity/migration.sql +++ /dev/null @@ -1,16 +0,0 @@ -/* - Warnings: - - - You are about to drop the `TwilioCredentials` table. If the table is not empty, all the data it contains will be lost. - -*/ --- DropForeignKey -ALTER TABLE "TwilioCredentials" DROP CONSTRAINT "TwilioCredentials_organizationId_fkey"; - --- AlterTable -ALTER TABLE "Organization" ADD COLUMN "twilioAccountSid" TEXT, -ADD COLUMN "twilioAuthToken" TEXT, -ADD COLUMN "twimlAppSid" TEXT; - --- DropTable -DROP TABLE "TwilioCredentials"; diff --git a/db/migrations/20210805153229_org_id_wesh/migration.sql b/db/migrations/20210805153229_org_id_wesh/migration.sql deleted file mode 100644 index a1e0ebf..0000000 --- a/db/migrations/20210805153229_org_id_wesh/migration.sql +++ /dev/null @@ -1,36 +0,0 @@ -/* - Warnings: - - - A unique constraint covering the columns `[organizationId,phoneNumberId,id]` on the table `Message` will be added. If there are existing duplicate values, this will fail. - - A unique constraint covering the columns `[id,twilioAccountSid]` on the table `Organization` will be added. If there are existing duplicate values, this will fail. - - A unique constraint covering the columns `[organizationId,phoneNumberId,id]` on the table `PhoneCall` will be added. If there are existing duplicate values, this will fail. - - Added the required column `organizationId` to the `Message` table without a default value. This is not possible if the table is not empty. - - Added the required column `organizationId` to the `PhoneCall` table without a default value. This is not possible if the table is not empty. - -*/ --- DropIndex -DROP INDEX "Message.phoneNumberId_id_unique"; - --- DropIndex -DROP INDEX "PhoneCall.phoneNumberId_id_unique"; - --- AlterTable -ALTER TABLE "Message" ADD COLUMN "organizationId" TEXT NOT NULL; - --- AlterTable -ALTER TABLE "PhoneCall" ADD COLUMN "organizationId" TEXT NOT NULL; - --- CreateIndex -CREATE UNIQUE INDEX "Message.organizationId_phoneNumberId_id_unique" ON "Message"("organizationId", "phoneNumberId", "id"); - --- CreateIndex -CREATE UNIQUE INDEX "Organization.id_twilioAccountSid_unique" ON "Organization"("id", "twilioAccountSid"); - --- CreateIndex -CREATE UNIQUE INDEX "PhoneCall.organizationId_phoneNumberId_id_unique" ON "PhoneCall"("organizationId", "phoneNumberId", "id"); - --- AddForeignKey -ALTER TABLE "Message" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/migrations/20210807134203_init/migration.sql b/db/migrations/20210807134203_init/migration.sql new file mode 100644 index 0000000..1824a67 --- /dev/null +++ b/db/migrations/20210807134203_init/migration.sql @@ -0,0 +1,205 @@ +-- CreateEnum +CREATE TYPE "MembershipRole" AS ENUM ('OWNER', 'ADMIN', 'USER'); + +-- CreateEnum +CREATE TYPE "GlobalRole" AS ENUM ('SUPERADMIN', 'CUSTOMER'); + +-- CreateEnum +CREATE TYPE "Role" AS ENUM ('USER', 'ADMIN'); + +-- CreateEnum +CREATE TYPE "TokenType" AS ENUM ('RESET_PASSWORD'); + +-- CreateEnum +CREATE TYPE "Direction" AS ENUM ('Inbound', 'Outbound'); + +-- CreateEnum +CREATE TYPE "MessageStatus" AS ENUM ('Queued', 'Sending', 'Sent', 'Failed', 'Delivered', 'Undelivered', 'Receiving', 'Received', 'Accepted', 'Scheduled', 'Read', 'PartiallyDelivered', 'Canceled', 'Error'); + +-- CreateEnum +CREATE TYPE "CallStatus" AS ENUM ('Queued', 'Ringing', 'InProgress', 'Completed', 'Busy', 'Failed', 'NoAnswer', 'Canceled'); + +-- CreateTable +CREATE TABLE "Organization" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMPTZ NOT NULL, + "encryptionKey" TEXT NOT NULL, + "paddleCustomerId" TEXT, + "paddleSubscriptionId" TEXT, + "twilioAccountSid" TEXT, + "twilioAuthToken" TEXT, + "twimlAppSid" TEXT, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Membership" ( + "id" TEXT NOT NULL, + "role" "MembershipRole" NOT NULL, + "organizationId" TEXT NOT NULL, + "userId" TEXT, + "invitedName" TEXT, + "invitedEmail" TEXT, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMPTZ NOT NULL, + "name" TEXT, + "email" TEXT NOT NULL, + "hashedPassword" TEXT, + "role" "GlobalRole" NOT NULL DEFAULT E'CUSTOMER', + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMPTZ NOT NULL, + "expiresAt" TIMESTAMPTZ, + "handle" TEXT NOT NULL, + "hashedSessionToken" TEXT, + "antiCSRFToken" TEXT, + "publicData" TEXT, + "privateData" TEXT, + "userId" TEXT, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Token" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMPTZ NOT NULL, + "hashedToken" TEXT NOT NULL, + "type" "TokenType" NOT NULL, + "expiresAt" TIMESTAMPTZ NOT NULL, + "sentTo" TEXT NOT NULL, + "userId" TEXT NOT NULL, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Message" ( + "id" TEXT NOT NULL, + "sentAt" TIMESTAMPTZ NOT NULL, + "content" TEXT NOT NULL, + "from" TEXT NOT NULL, + "to" TEXT NOT NULL, + "direction" "Direction" NOT NULL, + "status" "MessageStatus" NOT NULL, + "organizationId" TEXT NOT NULL, + "phoneNumberId" TEXT NOT NULL, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "PhoneCall" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "from" TEXT NOT NULL, + "to" TEXT NOT NULL, + "status" "CallStatus" NOT NULL, + "direction" "Direction" NOT NULL, + "duration" TEXT NOT NULL, + "organizationId" TEXT NOT NULL, + "phoneNumberId" TEXT NOT NULL, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "PhoneNumber" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "number" TEXT NOT NULL, + "organizationId" TEXT NOT NULL, + + PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "NotificationSubscription" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMPTZ NOT NULL, + "endpoint" TEXT NOT NULL, + "expirationTime" INTEGER, + "keys_p256dh" TEXT NOT NULL, + "keys_auth" TEXT NOT NULL, + "organizationId" TEXT NOT NULL, + "phoneNumberId" TEXT NOT NULL, + + PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "Organization.id_twilioAccountSid_unique" ON "Organization"("id", "twilioAccountSid"); + +-- CreateIndex +CREATE UNIQUE INDEX "Membership.organizationId_invitedEmail_unique" ON "Membership"("organizationId", "invitedEmail"); + +-- CreateIndex +CREATE UNIQUE INDEX "User.email_unique" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session.handle_unique" ON "Session"("handle"); + +-- CreateIndex +CREATE UNIQUE INDEX "Token.hashedToken_type_unique" ON "Token"("hashedToken", "type"); + +-- CreateIndex +CREATE UNIQUE INDEX "Message.organizationId_phoneNumberId_id_unique" ON "Message"("organizationId", "phoneNumberId", "id"); + +-- CreateIndex +CREATE UNIQUE INDEX "PhoneCall.organizationId_phoneNumberId_id_unique" ON "PhoneCall"("organizationId", "phoneNumberId", "id"); + +-- CreateIndex +CREATE UNIQUE INDEX "PhoneNumber.organizationId_id_unique" ON "PhoneNumber"("organizationId", "id"); + +-- CreateIndex +CREATE UNIQUE INDEX "NotificationSubscription.endpoint_unique" ON "NotificationSubscription"("endpoint"); + +-- AddForeignKey +ALTER TABLE "Membership" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Membership" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Session" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Token" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Message" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Message" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "PhoneCall" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "PhoneNumber" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "NotificationSubscription" ADD FOREIGN KEY ("phoneNumberId") REFERENCES "PhoneNumber"("id") ON DELETE CASCADE ON UPDATE CASCADE;