Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import { relations, sql } from "drizzle-orm";
import {
check,
index,
integer,
sqliteTable,
text,
uniqueIndex,
} from "drizzle-orm/sqlite-core";
import { page } from "../pages";
import { pageSubscriberToPageComponent } from "./page_subscriber_to_page_component";
export const pageSubscriber = sqliteTable(
"page_subscriber",
{
id: integer("id").primaryKey(),
email: text("email"),
pageId: integer("page_id")
.notNull()
.references(() => page.id, { onDelete: "cascade" }),
// Added: channel type discriminator
channelType: text("channel_type", {
enum: ["email", "webhook", "slack"],
})
.notNull()
.default("email"),
// Added: webhook-specific fields (null for email channel)
webhookUrl: text("webhook_url"),
channelConfig: text("channel_config"),
// Slack channel id (null for non-slack channels). teamId + channelName
// live in channelConfig; the bot token is resolved from the integration
// row at send time, never persisted here.
slackChannelId: text("slack_channel_id"),
// How the subscription was created. Vendor-added rows skip verification
// and are editable from the dashboard.
source: text("source", {
enum: ["self_signup", "vendor", "import"],
})
.notNull()
.default("self_signup"),
// Optional human-readable label (e.g. "Supabase #incidents").
// When set, rendered in place of the raw destination in UI.
name: text("name"),
token: text("token"),
acceptedAt: integer("accepted_at", { mode: "timestamp" }),
expiresAt: integer("expires_at", { mode: "timestamp" }),
unsubscribedAt: integer("unsubscribed_at", { mode: "timestamp" }),
createdAt: integer("created_at", { mode: "timestamp" }).default(
sql`(strftime('%s', 'now'))`,
),
updatedAt: integer("updated_at", { mode: "timestamp" }).default(
sql`(strftime('%s', 'now'))`,
),
},
(table) => ({
// Plain, not partial: the three partial uniques below all lead with a
// channel identifier, so nothing could serve a page_id lookup.
pageIdIdx: index("page_subscriber_page_id_idx").on(table.pageId),
// Partial unique index: one active email subscription per page
emailPageActiveIdx: uniqueIndex("idx_page_subscriber_email_page_active")
.on(sql`LOWER(${table.email})`, table.pageId)
.where(
sql`${table.unsubscribedAt} IS NULL AND ${table.channelType} = 'email'`,
),
// Partial unique index: one active webhook subscription per page.
webhookPageActiveIdx: uniqueIndex("idx_page_subscriber_webhook_page_active")
.on(sql`LOWER(${table.webhookUrl})`, table.pageId)
.where(
sql`${table.unsubscribedAt} IS NULL AND ${table.channelType} = 'webhook'`,
),
// Partial unique index: one active slack subscription per (channel, page).
slackChannelPageActiveIdx: uniqueIndex(
"idx_page_subscriber_slack_channel_page_active",
)
.on(table.slackChannelId, table.pageId)
.where(
sql`${table.unsubscribedAt} IS NULL AND ${table.channelType} = 'slack'`,
),
// CHECK constraint: only correct identifier populated per channel type
channelCheck: check(
"page_subscriber_channel_check",
sql`(${table.channelType} = 'email' AND ${table.email} IS NOT NULL AND ${table.webhookUrl} IS NULL) OR (${table.channelType} = 'webhook' AND ${table.webhookUrl} IS NOT NULL AND ${table.email} IS NULL) OR (${table.channelType} = 'slack' AND ${table.slackChannelId} IS NOT NULL AND ${table.email} IS NULL AND ${table.webhookUrl} IS NULL)`,
),
}),
);
export const pageSubscriberRelation = relations(
pageSubscriber,
({ one, many }) => ({
page: one(page, {
fields: [pageSubscriber.pageId],
references: [page.id],
}),
components: many(pageSubscriberToPageComponent),
}),
);