-- Copyright 2026 Manuel Reyes Bravo -- SPDX-License-Identifier: Apache-2.0 -- pg_promise_guard 0.2.9 -> 0.2.10 -- -- pg_living_assertions becomes optional, the way the code uses it. Only watch() records -- anything there; check_promises(), promises_kept() and promise_breaks read the catalog and -- nothing else. Until 0.2.9 the control file required it, so CREATE EXTENSION failed without -- it while the README said it was needed "only for watch()". The control file no longer -- requires it (an existing installation keeps the dependency it was created with), and -- watch() now says what it needs when it is missing, instead of failing on a schema that does -- not exist. Nothing else changes; CREATE OR REPLACE keeps watch()'s owner, privileges and -- comment. \echo Use "ALTER EXTENSION pg_promise_guard UPDATE TO '0.2.10'" to load this file. \quit CREATE OR REPLACE FUNCTION @extschema@.watch(p_schema text DEFAULT NULL) RETURNS bigint LANGUAGE plpgsql SET search_path = pg_catalog, @extschema@, pg_temp AS $fn$ DECLARE schema_label text := coalesce(p_schema, '(every schema)'); existing bigint; BEGIN -- pg_living_assertions is optional (0.2.10): only this function uses it. IF NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'pg_living_assertions') THEN RAISE EXCEPTION 'watch() needs pg_living_assertions' USING HINT = 'CREATE EXTENSION pg_living_assertions; -- check_promises(), ' 'promises_kept() and promise_breaks work without it.'; END IF; -- A schema that does not exist is refused (0.2.8), before anything is declared. IF p_schema IS NOT NULL AND NOT EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = p_schema) THEN RAISE EXCEPTION 'no schema named %', p_schema; END IF; -- Idempotent (0.2.8): a schema already watched keeps the assertion it has. SELECT a.id INTO existing FROM living_assertions.assertions a WHERE a.name = 'promises:' || schema_label AND a.retired_at IS NULL; IF existing IS NOT NULL THEN RETURN existing; END IF; RETURN living_assertions.declare( 'promises:' || schema_label, 'the guarantees the schema ' || schema_label || ' advertises are actually ' 'being enforced', format($f$select @extschema@.promises_kept(%L) as holds, coalesce((select pg_catalog.string_agg(kind || ' on ' || object, '; ') from @extschema@.check_promises(%L) where severity = 'breach'), 'no breaches') as detail$f$, p_schema, p_schema)); END; $fn$;