Files
uptimesebi/internal/migrations/sql/00017_owned_regions.sql
T
Sebastian UnterschützandClaude Opus 5.5 607798d742 feat(store): let regions and agents belong to the platform or one organization
Regions are identified by id so organizations can reuse keys. Triggers
keep agents in regions of the same owner and monitors in platform or own
regions. Platform admins are a user flag.

Refs #52

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 01:36:02 +01:00

105 lines
5.0 KiB
PL/PgSQL

-- +goose Up
-- Regions, checkers and status page servers belong either to the platform
-- (org_id NULL, managed by platform admins, usable by every org) or to one
-- organization. Org-owned regions may reuse keys of other orgs, so regions
-- are now identified by id; check_results.region stores that id as text.
ALTER TABLE regions ADD COLUMN id uuid NOT NULL DEFAULT gen_random_uuid();
ALTER TABLE regions ADD COLUMN org_id uuid REFERENCES orgs (id) ON DELETE CASCADE;
ALTER TABLE monitor_regions ADD COLUMN region_id uuid;
UPDATE monitor_regions mr SET region_id = r.id FROM regions r WHERE r.key = mr.region;
ALTER TABLE agents ADD COLUMN region_id uuid;
UPDATE agents a SET region_id = r.id FROM regions r WHERE r.key = a.region;
UPDATE check_results c SET region = r.id::text FROM regions r WHERE r.key = c.region;
ALTER TABLE monitor_regions DROP CONSTRAINT monitor_regions_pkey;
ALTER TABLE monitor_regions DROP COLUMN region;
ALTER TABLE agents DROP CONSTRAINT agents_check;
ALTER TABLE agents DROP COLUMN region;
ALTER TABLE regions DROP CONSTRAINT regions_pkey;
ALTER TABLE regions ADD PRIMARY KEY (id);
ALTER TABLE regions ADD CONSTRAINT regions_owner_key_key UNIQUE NULLS NOT DISTINCT (org_id, key);
ALTER TABLE monitor_regions ALTER COLUMN region_id SET NOT NULL;
ALTER TABLE monitor_regions ADD PRIMARY KEY (monitor_id, region_id);
-- A region in use cannot be deleted; monitors have to move first.
ALTER TABLE monitor_regions ADD FOREIGN KEY (region_id) REFERENCES regions (id);
ALTER TABLE agents ADD FOREIGN KEY (region_id) REFERENCES regions (id);
ALTER TABLE agents ADD CONSTRAINT agents_checker_has_region CHECK ((kind = 'checker') = (region_id IS NOT NULL));
-- +goose StatementBegin
-- The region of an agent has the same owner as the agent, and monitors use
-- only platform regions or regions of their own org. A composite foreign
-- key cannot express this because platform rows have a NULL org_id.
CREATE FUNCTION region_owner(region uuid) RETURNS uuid
LANGUAGE sql STABLE AS $$ SELECT org_id FROM regions WHERE id = region $$;
CREATE FUNCTION agents_region_owner_check() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF NEW.region_id IS NOT NULL AND region_owner(NEW.region_id) IS DISTINCT FROM NEW.org_id THEN
RAISE EXCEPTION 'agent and region must have the same owner' USING ERRCODE = 'check_violation';
END IF;
RETURN NEW;
END
$$;
CREATE FUNCTION monitor_regions_owner_check() RETURNS trigger
LANGUAGE plpgsql AS $$
DECLARE
owner uuid := region_owner(NEW.region_id);
BEGIN
IF owner IS NOT NULL AND owner <> NEW.org_id THEN
RAISE EXCEPTION 'region belongs to another organization' USING ERRCODE = 'check_violation';
END IF;
RETURN NEW;
END
$$;
-- +goose StatementEnd
CREATE TRIGGER agents_region_owner BEFORE INSERT OR UPDATE ON agents
FOR EACH ROW EXECUTE FUNCTION agents_region_owner_check();
CREATE TRIGGER monitor_regions_owner BEFORE INSERT OR UPDATE ON monitor_regions
FOR EACH ROW EXECUTE FUNCTION monitor_regions_owner_check();
ALTER TABLE regions ENABLE ROW LEVEL SECURITY;
ALTER TABLE regions FORCE ROW LEVEL SECURITY;
CREATE POLICY regions_read ON regions FOR SELECT
USING (org_id IS NULL OR app_scope_system() OR org_id = app_org_id());
CREATE POLICY regions_write ON regions FOR ALL
USING (app_scope_system() OR (org_id IS NOT NULL AND org_id = app_org_id()))
WITH CHECK (app_scope_system() OR (org_id IS NOT NULL AND org_id = app_org_id()));
-- +goose Down
-- Only reversible while no organization owns regions.
DROP POLICY regions_write ON regions;
DROP POLICY regions_read ON regions;
ALTER TABLE regions NO FORCE ROW LEVEL SECURITY;
ALTER TABLE regions DISABLE ROW LEVEL SECURITY;
DROP TRIGGER monitor_regions_owner ON monitor_regions;
DROP TRIGGER agents_region_owner ON agents;
DROP FUNCTION monitor_regions_owner_check();
DROP FUNCTION agents_region_owner_check();
DROP FUNCTION region_owner(uuid);
ALTER TABLE monitor_regions ADD COLUMN region text;
UPDATE monitor_regions mr SET region = r.key FROM regions r WHERE r.id = mr.region_id;
ALTER TABLE agents ADD COLUMN region text;
UPDATE agents a SET region = r.key FROM regions r WHERE r.id = a.region_id;
UPDATE check_results c SET region = r.key FROM regions r WHERE r.id::text = c.region;
ALTER TABLE agents DROP CONSTRAINT agents_checker_has_region;
ALTER TABLE agents DROP COLUMN region_id;
ALTER TABLE monitor_regions DROP CONSTRAINT monitor_regions_pkey;
ALTER TABLE monitor_regions DROP COLUMN region_id;
ALTER TABLE regions DROP CONSTRAINT regions_owner_key_key;
ALTER TABLE regions DROP CONSTRAINT regions_pkey;
ALTER TABLE regions ADD PRIMARY KEY (key);
ALTER TABLE regions DROP COLUMN org_id;
ALTER TABLE regions DROP COLUMN id;
ALTER TABLE monitor_regions ALTER COLUMN region SET NOT NULL;
ALTER TABLE monitor_regions ADD PRIMARY KEY (monitor_id, region);
ALTER TABLE monitor_regions ADD FOREIGN KEY (region) REFERENCES regions (key);
ALTER TABLE agents ADD FOREIGN KEY (region) REFERENCES regions (key);
ALTER TABLE agents ADD CONSTRAINT agents_check CHECK ((kind = 'checker') = (region IS NOT NULL));