-- Database evidence namespace, not a biological world/format change (ADR-0155). CREATE TABLE evolution.history_domains ( domain uuid PRIMARY KEY CHECK (domain <> '00000000-0000-0000-0000-000000000000'), parent uuid REFERENCES evolution.history_domains(domain), request uuid UNIQUE, kind text NOT NULL CHECK (kind IN ('adoption', 'restore')), recorded_at timestamptz NOT NULL DEFAULT pg_catalog.clock_timestamp(), CHECK ((kind = 'adoption' AND parent IS NULL AND request IS NULL) OR (kind = 'restore' AND parent IS NOT NULL AND request IS NOT NULL)), CHECK (parent IS DISTINCT FROM domain), CHECK (request IS NULL OR request <> '00000000-0000-0000-0000-000000000000') ); CREATE TABLE evolution.current_history_domain ( singleton boolean PRIMARY KEY CHECK (singleton), domain uuid NOT NULL REFERENCES evolution.history_domains(domain) ); CREATE TABLE evolution.history_domain_cuts ( domain uuid NOT NULL REFERENCES evolution.history_domains(domain), world_id evolution.u64 NOT NULL, region_id evolution.u64 NOT NULL, frame_id evolution.u64 NOT NULL, commit_sequence evolution.u64 NOT NULL, commit_id uuid NOT NULL, digest bytea NOT NULL CHECK (octet_length(digest) = 32), end_tick evolution.u64 NOT NULL, PRIMARY KEY (domain, world_id, region_id) ); REVOKE ALL ON evolution.history_domains, evolution.current_history_domain, evolution.history_domain_cuts FROM PUBLIC; WITH adopted AS ( INSERT INTO evolution.history_domains(domain, kind) VALUES (pg_catalog.gen_random_uuid(), 'adoption') RETURNING domain ) INSERT INTO evolution.current_history_domain SELECT true, domain FROM adopted; CREATE FUNCTION evolution.load_history_domain_v1() RETURNS uuid LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ SELECT domain FROM evolution.current_history_domain WHERE singleton; $$; REVOKE ALL ON FUNCTION evolution.load_history_domain_v1() FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.load_history_domain_v1() TO :"runtime_role"; -- Owner-only, called while the supported restore is still quarantined. CREATE FUNCTION evolution.fork_restored_history_v1(p_request uuid) RETURNS uuid LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ DECLARE old_domain uuid; new_domain uuid; BEGIN IF p_request IS NULL OR p_request = '00000000-0000-0000-0000-000000000000' THEN RAISE EXCEPTION 'nonzero restore request required' USING ERRCODE='22023'; END IF; SELECT domain INTO STRICT old_domain FROM evolution.current_history_domain WHERE singleton FOR UPDATE; SELECT domain INTO new_domain FROM evolution.history_domains WHERE request = p_request; IF FOUND THEN IF new_domain <> old_domain THEN RAISE EXCEPTION 'restore request is no longer current' USING ERRCODE='22023'; END IF; RETURN new_domain; END IF; IF EXISTS (SELECT 1 FROM evolution.retained_builds WHERE retired_at IS NULL) OR EXISTS (SELECT 1 FROM evolution.region_leases WHERE holder IS NOT NULL) THEN RAISE EXCEPTION 'restore must remain quarantined' USING ERRCODE='55000'; END IF; new_domain := pg_catalog.gen_random_uuid(); INSERT INTO evolution.history_domains(domain, parent, request, kind) VALUES (new_domain, old_domain, p_request, 'restore'); INSERT INTO evolution.history_domain_cuts SELECT new_domain, h.world_id, h.region_id, l.frame_id, h.commit_sequence, h.commit_id, h.digest, h.end_tick FROM evolution.region_heads h JOIN evolution.region_leases l USING(world_id, region_id); UPDATE evolution.current_history_domain SET domain = new_domain WHERE singleton; RETURN new_domain; END $$; REVOKE ALL ON FUNCTION evolution.fork_restored_history_v1(uuid) FROM PUBLIC; INSERT INTO evolution.schema_migrations VALUES (51, :'migration_sha_51');