redo migration script

This commit is contained in:
m5r 2022-05-18 01:48:59 +02:00
parent cdaafec4e6
commit 18e16c84b2
2 changed files with 109 additions and 122 deletions

View File

@ -24,6 +24,8 @@ CREATE TABLE "Organization" (
"id" TEXT NOT NULL, "id" TEXT NOT NULL,
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMPTZ NOT NULL, "updatedAt" TIMESTAMPTZ NOT NULL,
"twilioAccountSid" TEXT,
"twilioSubAccountSid" TEXT,
CONSTRAINT "Organization_pkey" PRIMARY KEY ("id") CONSTRAINT "Organization_pkey" PRIMARY KEY ("id")
); );
@ -132,6 +134,7 @@ CREATE TABLE "PhoneNumber" (
"id" TEXT NOT NULL, "id" TEXT NOT NULL,
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"number" TEXT NOT NULL, "number" TEXT NOT NULL,
"isCurrent" BOOLEAN NOT NULL,
"isFetchingMessages" BOOLEAN, "isFetchingMessages" BOOLEAN,
"isFetchingCalls" BOOLEAN, "isFetchingCalls" BOOLEAN,
"organizationId" TEXT NOT NULL, "organizationId" TEXT NOT NULL,
@ -155,7 +158,7 @@ CREATE UNIQUE INDEX "Token_membershipId_key" ON "Token"("membershipId");
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type"); CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");
-- CreateIndex -- CreateIndex
CREATE UNIQUE INDEX "PhoneNumber_organizationId_id_key" ON "PhoneNumber"("organizationId", "id"); CREATE UNIQUE INDEX "PhoneNumber_organizationId_isCurrent_key" ON "PhoneNumber"("organizationId", "isCurrent") WHERE ("isCurrent" = true);
-- AddForeignKey -- AddForeignKey
ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -1,17 +1,16 @@
generator client {
provider = "prisma-client-js"
}
datasource db { datasource db {
provider = "postgresql" provider = "postgresql"
url = env("DATABASE_URL") url = env("DATABASE_URL")
} }
generator client {
provider = "prisma-client-js"
}
model Organization { model Organization {
id String @id @default(cuid()) id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz updatedAt DateTime @updatedAt @db.Timestamptz(6)
twilioAccountSid String? twilioAccountSid String?
twilioSubAccountSid String? twilioSubAccountSid String?
@ -21,9 +20,8 @@ model Organization {
} }
model Subscription { model Subscription {
createdAt DateTime @default(now()) @db.Timestamptz createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz updatedAt DateTime @updatedAt @db.Timestamptz(6)
paddleSubscriptionId Int @id @unique paddleSubscriptionId Int @id @unique
paddlePlanId Int paddlePlanId Int
paddleCheckoutId String paddleCheckoutId String
@ -33,110 +31,126 @@ model Subscription {
currency String currency String
unitPrice Float unitPrice Float
nextBillDate DateTime @db.Date nextBillDate DateTime @db.Date
lastEventTime DateTime @db.Timestamp lastEventTime DateTime @db.Timestamp(6)
cancellationEffectiveDate DateTime? @db.Date cancellationEffectiveDate DateTime? @db.Date
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
organizationId String organizationId String
} organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
enum SubscriptionStatus {
active
trialing // not used
past_due
paused // not used
deleted
} }
model Membership { model Membership {
id String @id @default(cuid()) id String @id @default(cuid())
role MembershipRole role MembershipRole
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
organizationId String organizationId String
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String? userId String?
// When the user joins, we will clear out the email and set the user.
invitedEmail String? invitedEmail String?
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
invitationToken Token? invitationToken Token?
@@unique([organizationId, invitedEmail]) @@unique([organizationId, invitedEmail])
} }
model User {
id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
fullName String
email String @unique
hashedPassword String?
role GlobalRole @default(CUSTOMER)
memberships Membership[]
sessions Session[]
tokens Token[]
}
model Session {
id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
expiresAt DateTime? @db.Timestamptz(6)
data String
userId String?
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model Token {
id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
hashedToken String
type TokenType
expiresAt DateTime @db.Timestamptz(6)
sentTo String
userId String
membershipId String @unique
membership Membership @relation(fields: [membershipId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([hashedToken, type])
}
model Message {
id String @id
sentAt DateTime @db.Timestamptz(6)
content String
from String
to String
direction Direction
status MessageStatus
phoneNumberId String
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
}
model PhoneCall {
id String @id @unique
createdAt DateTime @default(now()) @db.Timestamptz(6)
from String
to String
status CallStatus
direction Direction
duration String
phoneNumberId String
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
}
model PhoneNumber {
id String @id
createdAt DateTime @default(now()) @db.Timestamptz(6)
number String
isCurrent Boolean
isFetchingMessages Boolean?
isFetchingCalls Boolean?
organizationId String
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
messages Message[]
phoneCalls PhoneCall[]
@@unique([organizationId, isCurrent])
}
enum SubscriptionStatus {
active
trialing
past_due
paused
deleted
}
enum MembershipRole { enum MembershipRole {
OWNER OWNER
USER USER
} }
// The owners of the SaaS (you) can have a SUPERADMIN role to access all data
enum GlobalRole { enum GlobalRole {
SUPERADMIN SUPERADMIN
CUSTOMER CUSTOMER
} }
model User {
id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz
updatedAt DateTime @updatedAt @db.Timestamptz
fullName String
email String @unique
hashedPassword String?
role GlobalRole @default(CUSTOMER)
memberships Membership[]
tokens Token[]
sessions Session[]
}
model Session {
id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz
updatedAt DateTime @updatedAt @db.Timestamptz
expiresAt DateTime? @db.Timestamptz
data String
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String?
}
model Token {
id String @id @default(cuid())
createdAt DateTime @default(now()) @db.Timestamptz
updatedAt DateTime @updatedAt @db.Timestamptz
hashedToken String
type TokenType
expiresAt DateTime @db.Timestamptz
sentTo String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
membership Membership @relation(fields: [membershipId], references: [id], onDelete: Cascade)
membershipId String @unique
@@unique([hashedToken, type])
}
enum TokenType { enum TokenType {
RESET_PASSWORD RESET_PASSWORD
INVITE_MEMBER INVITE_MEMBER
} }
model Message {
id String @id
sentAt DateTime @db.Timestamptz
content String
from String
to String
direction Direction
status MessageStatus
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
phoneNumberId String
}
enum Direction { enum Direction {
Inbound Inbound
Outbound Outbound
@ -156,23 +170,9 @@ enum MessageStatus {
Read Read
PartiallyDelivered PartiallyDelivered
Canceled Canceled
Error Error
} }
model PhoneCall {
id String @id
createdAt DateTime @default(now()) @db.Timestamptz
from String
to String
status CallStatus
direction Direction
duration String
phoneNumber PhoneNumber @relation(fields: [phoneNumberId], references: [id], onDelete: Cascade)
phoneNumberId String
}
enum CallStatus { enum CallStatus {
Queued Queued
Ringing Ringing
@ -183,19 +183,3 @@ enum CallStatus {
NoAnswer NoAnswer
Canceled Canceled
} }
model PhoneNumber {
id String @id
createdAt DateTime @default(now()) @db.Timestamptz
number String
isFetchingMessages Boolean?
isFetchingCalls Boolean?
messages Message[]
phoneCalls PhoneCall[]
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
organizationId String
@@unique([organizationId, id])
}