Composite foreign keys keep pages from listing monitors of other organizations, independent of row-level security. Refs #21 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
53 lines
2.5 KiB
SQL
53 lines
2.5 KiB
SQL
-- +goose Up
|
|
CREATE TABLE status_pages (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
|
-- Pages without a custom domain are served at /<slug>, so slugs are
|
|
-- unique across organizations.
|
|
slug citext NOT NULL UNIQUE CHECK (slug ~ '^[a-z0-9][a-z0-9-]{1,62}$'),
|
|
title text NOT NULL CHECK (length(title) BETWEEN 1 AND 200),
|
|
description text NOT NULL DEFAULT '' CHECK (length(description) <= 2000),
|
|
published boolean NOT NULL DEFAULT false,
|
|
custom_domain citext UNIQUE CHECK (custom_domain ~ '^[a-z0-9]([a-z0-9.-]{0,251}[a-z0-9])?$'),
|
|
-- Proves control of custom_domain via a TXT record.
|
|
domain_token text NOT NULL,
|
|
domain_verified_at timestamptz,
|
|
theme jsonb NOT NULL DEFAULT '{}',
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
UNIQUE (id, org_id)
|
|
);
|
|
|
|
ALTER TABLE monitors ADD CONSTRAINT monitors_id_org_key UNIQUE (id, org_id);
|
|
|
|
-- Composite foreign keys keep pages from listing monitors of other
|
|
-- organizations; foreign keys are checked regardless of row-level security.
|
|
CREATE TABLE status_page_monitors (
|
|
page_id uuid NOT NULL,
|
|
monitor_id uuid NOT NULL,
|
|
org_id uuid NOT NULL,
|
|
section text NOT NULL DEFAULT '' CHECK (length(section) <= 100),
|
|
label text NOT NULL DEFAULT '' CHECK (length(label) <= 200),
|
|
position integer NOT NULL DEFAULT 0,
|
|
PRIMARY KEY (page_id, monitor_id),
|
|
FOREIGN KEY (page_id, org_id) REFERENCES status_pages (id, org_id) ON DELETE CASCADE,
|
|
FOREIGN KEY (monitor_id, org_id) REFERENCES monitors (id, org_id) ON DELETE CASCADE
|
|
);
|
|
|
|
ALTER TABLE status_pages ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE status_pages FORCE ROW LEVEL SECURITY;
|
|
CREATE POLICY status_pages_tenant ON status_pages 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 status_page_monitors ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE status_page_monitors FORCE ROW LEVEL SECURITY;
|
|
CREATE POLICY status_page_monitors_tenant ON status_page_monitors 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 status_page_monitors;
|
|
ALTER TABLE monitors DROP CONSTRAINT monitors_id_org_key;
|
|
DROP TABLE status_pages;
|