-- +goose Up -- Visitors of a status page can subscribe to its incidents by email once -- the organization allows it on the page. ALTER TABLE status_pages ADD COLUMN subscriptions boolean NOT NULL DEFAULT false; -- How many subscriptions the pages of the organization may gain in seven -- days; NULL follows the platform default, 0 means unlimited. ALTER TABLE orgs ADD COLUMN limit_subscribers_per_week integer CHECK (limit_subscribers_per_week >= 0); CREATE TABLE status_page_subscribers ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), org_id uuid NOT NULL, page_id uuid NOT NULL, email citext NOT NULL CHECK (length(email) BETWEEN 3 AND 254), -- Language of the mails, as the visitor's browser asked for it. locale text NOT NULL DEFAULT 'en', -- Where the subscriber saw the page, for the links in mails when the -- platform does not know where the page is served. page_url text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), -- Set once the address confirmed the subscription; until then nothing -- is sent to it but the confirmation. confirmed_at timestamptz, UNIQUE (page_id, email), FOREIGN KEY (page_id, org_id) REFERENCES status_pages (id, org_id) ON DELETE CASCADE ); -- Counts the subscriptions of the last days for the weekly limit. CREATE INDEX status_page_subscribers_org_idx ON status_page_subscribers (org_id, created_at); -- Outbox: a row per subscriber and incident event. CREATE TABLE subscriber_notifications ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE, subscriber_id uuid NOT NULL REFERENCES status_page_subscribers (id) ON DELETE CASCADE, incident_id uuid NOT NULL REFERENCES incidents (id) ON DELETE CASCADE, -- The public update announced, for event 'update'. update_id uuid REFERENCES incident_updates (id) ON DELETE CASCADE, event text NOT NULL CHECK (event IN ('opened', 'resolved', 'update')), status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')), attempts integer NOT NULL DEFAULT 0, next_attempt_at timestamptz NOT NULL DEFAULT now(), last_error text, created_at timestamptz NOT NULL DEFAULT now(), sent_at timestamptz ); CREATE UNIQUE INDEX subscriber_notifications_once_idx ON subscriber_notifications (subscriber_id, incident_id, event, coalesce(update_id, '00000000-0000-0000-0000-000000000000')); CREATE INDEX subscriber_notifications_due_idx ON subscriber_notifications (next_attempt_at) WHERE status = 'pending'; ALTER TABLE status_page_subscribers ENABLE ROW LEVEL SECURITY; ALTER TABLE status_page_subscribers FORCE ROW LEVEL SECURITY; CREATE POLICY status_page_subscribers_tenant ON status_page_subscribers FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); ALTER TABLE subscriber_notifications ENABLE ROW LEVEL SECURITY; ALTER TABLE subscriber_notifications FORCE ROW LEVEL SECURITY; CREATE POLICY subscriber_notifications_tenant ON subscriber_notifications FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); -- +goose Down DROP TABLE subscriber_notifications; DROP TABLE status_page_subscribers; ALTER TABLE orgs DROP COLUMN limit_subscribers_per_week; ALTER TABLE status_pages DROP COLUMN subscriptions;