-- CreateEnum CREATE TYPE "MembershipRole" AS ENUM ('OWNER', 'USER'); -- CreateEnum CREATE TYPE "GlobalRole" AS ENUM ('SUPERADMIN', 'CUSTOMER'); -- CreateEnum CREATE TYPE "TokenType" AS ENUM ('RESET_PASSWORD', 'INVITE_MEMBER'); -- CreateTable CREATE TABLE "Organization" ( "id" TEXT NOT NULL, "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMPTZ NOT NULL, "slug" TEXT NOT NULL, "name" TEXT NOT NULL, "stripeCustomerId" TEXT, "stripeSubscriptionId" TEXT, "stripePriceId" TEXT, "stripeCurrentPeriodEnd" TIMESTAMP(3), CONSTRAINT "Organization_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "Membership" ( "id" TEXT NOT NULL, "role" "MembershipRole" NOT NULL, "organizationId" TEXT NOT NULL, "userId" TEXT, "invitedEmail" TEXT, CONSTRAINT "Membership_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "User" ( "id" TEXT NOT NULL, "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMPTZ NOT NULL, "fullName" TEXT NOT NULL, "email" TEXT NOT NULL, "hashedPassword" TEXT, "role" "GlobalRole" NOT NULL DEFAULT E'CUSTOMER', CONSTRAINT "User_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "Session" ( "id" TEXT NOT NULL, "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMPTZ NOT NULL, "expiresAt" TIMESTAMPTZ, "data" TEXT NOT NULL, "userId" TEXT, CONSTRAINT "Session_pkey" 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, "membershipId" TEXT NOT NULL, CONSTRAINT "Token_pkey" PRIMARY KEY ("id") ); -- CreateIndex CREATE UNIQUE INDEX "Organization_slug_key" ON "Organization"("slug"); -- CreateIndex CREATE UNIQUE INDEX "Organization_stripeCustomerId_key" ON "Organization"("stripeCustomerId"); -- CreateIndex CREATE UNIQUE INDEX "Organization_stripeSubscriptionId_key" ON "Organization"("stripeSubscriptionId"); -- CreateIndex CREATE UNIQUE INDEX "Membership_organizationId_invitedEmail_key" ON "Membership"("organizationId", "invitedEmail"); -- CreateIndex CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); -- CreateIndex CREATE UNIQUE INDEX "Token_membershipId_key" ON "Token"("membershipId"); -- CreateIndex CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type"); -- AddForeignKey ALTER TABLE "Membership" ADD CONSTRAINT "Membership_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "Membership" ADD CONSTRAINT "Membership_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "Token" ADD CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "Token" ADD CONSTRAINT "Token_membershipId_fkey" FOREIGN KEY ("membershipId") REFERENCES "Membership"("id") ON DELETE CASCADE ON UPDATE CASCADE;