-- +goose Up -- Organizations add statuses of their own, with a color, next to the -- built-in ones. A status in use cannot be deleted: the history would lose -- its meaning. CREATE TABLE incident_statuses ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE, name text NOT NULL CHECK (length(name) BETWEEN 1 AND 40), color text NOT NULL CHECK (color ~ '^#[0-9a-f]{6}$'), created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (org_id, name) ); ALTER TABLE incident_statuses ENABLE ROW LEVEL SECURITY; ALTER TABLE incident_statuses FORCE ROW LEVEL SECURITY; CREATE POLICY incident_statuses_tenant ON incident_statuses FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); -- status is a built-in status or 'custom' with status_id naming it. ALTER TABLE incident_updates DROP CONSTRAINT incident_updates_status_check; ALTER TABLE incident_updates ADD CONSTRAINT incident_updates_status_check CHECK (status IN ('investigating', 'identified', 'monitoring', 'resolved', 'custom')); ALTER TABLE incident_updates ADD COLUMN status_id uuid REFERENCES incident_statuses (id) ON DELETE RESTRICT; ALTER TABLE incident_updates ADD CONSTRAINT incident_updates_custom_status CHECK ((status = 'custom') = (status_id IS NOT NULL)); ALTER TABLE incidents DROP CONSTRAINT incidents_status_check; ALTER TABLE incidents ADD CONSTRAINT incidents_status_check CHECK (status IN ('investigating', 'identified', 'monitoring', 'resolved', 'custom')); ALTER TABLE incidents ADD COLUMN status_id uuid REFERENCES incident_statuses (id) ON DELETE RESTRICT; ALTER TABLE incidents ADD CONSTRAINT incidents_custom_status CHECK ((status = 'custom') = (status_id IS NOT NULL)); -- +goose Down DELETE FROM incident_updates WHERE status = 'custom'; UPDATE incidents SET status = NULL WHERE status = 'custom'; ALTER TABLE incidents DROP CONSTRAINT incidents_custom_status; ALTER TABLE incidents DROP COLUMN status_id; ALTER TABLE incidents DROP CONSTRAINT incidents_status_check; ALTER TABLE incidents ADD CONSTRAINT incidents_status_check CHECK (status IN ('investigating', 'identified', 'monitoring', 'resolved')); ALTER TABLE incident_updates DROP CONSTRAINT incident_updates_custom_status; ALTER TABLE incident_updates DROP COLUMN status_id; ALTER TABLE incident_updates DROP CONSTRAINT incident_updates_status_check; ALTER TABLE incident_updates ADD CONSTRAINT incident_updates_status_check CHECK (status IN ('investigating', 'identified', 'monitoring', 'resolved')); DROP TABLE incident_statuses;