Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- Only adds columns and tables: no table is rewritten, so this runs in
-- milliseconds. Adding columns to "Team", "Email" and "Campaign" still needs a
-- brief exclusive lock on each, held until COMMIT. Give up after 3s per lock
-- rather than queue behind a long query and hold up sending. On failure the
-- whole transaction rolls back, the old deployment keeps serving, and the
-- migration can be marked rolled back and retried. Avoid deploying while the
-- nightly cleanup runs (00:00-00:05 UTC).
BEGIN;
SET LOCAL lock_timeout = '3s';

-- CreateEnum
CREATE TYPE "TeamActionType" AS ENUM ('ALERT', 'PAUSE', 'RESUME', 'BLOCK', 'UNBLOCK');

-- AlterTable
ALTER TABLE "Team" ADD COLUMN "sendingPausedAt" TIMESTAMP(3);

-- AlterTable
ALTER TABLE "Email" ADD COLUMN "cohortDate" TEXT;

-- AlterTable
ALTER TABLE "Campaign" ADD COLUMN "acceptedComplained" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "acceptedHardBounced" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "acceptedSends" INTEGER NOT NULL DEFAULT 0;

-- CreateTable
CREATE TABLE "SendingCohort" (
"teamId" INTEGER NOT NULL,
"date" TEXT NOT NULL,
"sent" INTEGER NOT NULL DEFAULT 0,
"hardBounced" INTEGER NOT NULL DEFAULT 0,
"complained" INTEGER NOT NULL DEFAULT 0,

CONSTRAINT "SendingCohort_pkey" PRIMARY KEY ("teamId","date")
);

-- CreateTable
CREATE TABLE "TeamAction" (
"id" TEXT NOT NULL,
"teamId" INTEGER NOT NULL,
"action" "TeamActionType" NOT NULL,
"actor" TEXT NOT NULL,
"reason" TEXT NOT NULL,
"details" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "TeamAction_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "TeamAction_teamId_createdAt_idx" ON "TeamAction"("teamId", "createdAt");

-- AddForeignKey
ALTER TABLE "SendingCohort" ADD CONSTRAINT "SendingCohort_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TeamAction" ADD CONSTRAINT "TeamAction_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;

COMMIT;
138 changes: 95 additions & 43 deletions apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ model VerificationToken {
}

model User {
id Int @id @default(autoincrement())
name String?
email String? @unique
emailVerified DateTime?
image String?
isBetaUser Boolean @default(false)
isWaitlisted Boolean @default(false)
createdAt DateTime @default(now())
accounts Account[]
sessions Session[]
teamUsers TeamUser[]
webhookEndpoints Webhook[]
id Int @id @default(autoincrement())
name String?
email String? @unique
emailVerified DateTime?
image String?
isBetaUser Boolean @default(false)
isWaitlisted Boolean @default(false)
createdAt DateTime @default(now())
accounts Account[]
sessions Session[]
teamUsers TeamUser[]
webhookEndpoints Webhook[]
waitlistApplication WaitlistApplication?
feedback Feedback[]
}
Expand Down Expand Up @@ -143,6 +143,8 @@ model Team {
isBlocked Boolean @default(false)
/// Teams created before paid-only signups may keep sending on the free plan.
freePlanAllowed Boolean @default(false)
/// Set when sending is paused (e.g. bounce rate too high). Cleared on resume.
sendingPausedAt DateTime?
teamUsers TeamUser[]
domains Domain[]
apiKeys ApiKey[]
Expand All @@ -157,6 +159,48 @@ model Team {
webhookEndpoints Webhook[]
webhookCalls WebhookCall[]
feedback Feedback[]
actions TeamAction[]
sendingCohorts SendingCohort[]
}

/// Emails a team sent each day, with the hard bounces and complaints those
/// same emails caused (filed under the day they were sent). Used by the
/// sending rules, so rates always compare matching sends and outcomes.
model SendingCohort {
teamId Int
/// YYYY-MM-DD (UTC) the emails were sent
date String
sent Int @default(0)
hardBounced Int @default(0)
complained Int @default(0)

team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)

@@id([teamId, date])
}

enum TeamActionType {
ALERT
PAUSE
RESUME
BLOCK
UNBLOCK
}

/// Every pause, block and alert on a team, by the sending rules, an admin or the Guard.
model TeamAction {
id String @id @default(cuid())
teamId Int
action TeamActionType
/// "rules", "guard" or "admin:<email>"
actor String
reason String
details Json?
createdAt DateTime @default(now())

team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)

@@index([teamId, createdAt])
}

model TeamInvite {
Expand Down Expand Up @@ -295,6 +339,9 @@ model Email {
contactId String?
inReplyToId String?
headers String?
/// Day (YYYY-MM-DD, UTC) this email was counted as sent for the sending
/// rules. Its bounces and complaints are filed under the same day.
cohortDate String?
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
emailEvents EmailEvent[]

Expand Down Expand Up @@ -387,37 +434,42 @@ enum CampaignStatus {
}

model Campaign {
id String @id @default(cuid())
name String
teamId Int
from String
cc String[]
bcc String[]
replyTo String[]
domainId Int
subject String
previewText String?
html String?
content String?
contactBookId String?
scheduledAt DateTime?
total Int @default(0)
sent Int @default(0)
delivered Int @default(0)
opened Int @default(0)
clicked Int @default(0)
unsubscribed Int @default(0)
bounced Int @default(0)
hardBounced Int @default(0)
complained Int @default(0)
isApi Boolean @default(false)
status CampaignStatus @default(DRAFT)
batchSize Int @default(500)
batchWindowMinutes Int @default(0)
lastCursor String?
lastSentAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id String @id @default(cuid())
name String
teamId Int
from String
cc String[]
bcc String[]
replyTo String[]
domainId Int
subject String
previewText String?
html String?
content String?
contactBookId String?
scheduledAt DateTime?
total Int @default(0)
sent Int @default(0)
delivered Int @default(0)
opened Int @default(0)
clicked Int @default(0)
unsubscribed Int @default(0)
bounced Int @default(0)
hardBounced Int @default(0)
complained Int @default(0)
/// For the sending rules: emails counted as sent (marked with a cohort
/// day), and the hard bounces and complaints of those same emails.
acceptedSends Int @default(0)
acceptedHardBounced Int @default(0)
acceptedComplained Int @default(0)
isApi Boolean @default(false)
status CampaignStatus @default(DRAFT)
batchSize Int @default(500)
batchWindowMinutes Int @default(0)
lastCursor String?
lastSentAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)

Expand Down
Loading
Loading