Files
lincodex/backend/db/migrations/00003_create_policy_versions.sql
T

117 lines
4.8 KiB
PL/PgSQL

-- +goose Up
-- Versioned product policy: deadlines, quotas, thresholds, and coefficients
-- that product rules read. Every row is one immutable version of one policy
-- family. Records built under a policy, such as grade decisions, selection
-- snapshots, and invitations, will store the policy_versions.id they used, so
-- a newer version never changes how an earlier decision was made.
CREATE TABLE policy_versions (
-- UUIDv7 generated in Go.
id uuid NOT NULL,
-- Policy family. Each family has one document layout and one decoder in
-- Go; "product" is the only family. A new family changes this CHECK.
policy_key text NOT NULL,
-- Numbered from 1 within the family; the newest publication has the
-- highest number.
version integer NOT NULL,
-- Calibration state of the whole version, the flag consumers read. "demo"
-- values are an initial policy for verification and make no calibrated
-- quality claim.
kind text NOT NULL,
-- The version can apply to decisions made at or after this time. A NULL
-- in the INSERT means "at publication"; the stamp trigger fills it in.
effective_at timestamptz NOT NULL,
-- Operator identity such as "system:demo-seed" or "operator:<handle>".
-- Never an email address or other contact data.
author text NOT NULL,
-- The typed policy serialized by the domain in canonical form. jsonb
-- normalizes key order and whitespace; integrity is checked against
-- document_sha256 after the domain re-encodes the decoded document.
document jsonb NOT NULL,
-- SHA-256 of the canonical document bytes, 32 raw bytes.
document_sha256 bytea NOT NULL,
-- Publication time from the database clock, set by the stamp trigger on
-- every insert; a value from the caller is ignored.
created_at timestamptz NOT NULL,
CONSTRAINT policy_versions_pkey PRIMARY KEY (id),
CONSTRAINT policy_versions_policy_key_version_key UNIQUE (
policy_key, version
),
CONSTRAINT policy_versions_policy_key_known CHECK (
policy_key IN ('product')
),
CONSTRAINT policy_versions_version_positive CHECK (version > 0),
CONSTRAINT policy_versions_kind_known CHECK (
kind IN ('demo', 'calibrated')
),
CONSTRAINT policy_versions_author_format CHECK (
char_length(author) <= 128
AND author ~ '^(system|operator):[a-z0-9][a-z0-9._-]*$'
),
CONSTRAINT policy_versions_document_object CHECK (
jsonb_typeof(document) = 'object'
),
-- A calibrated version cannot carry an assessment section that its own
-- document marks as uncalibrated.
CONSTRAINT policy_versions_calibrated_kind_matches_document CHECK (
kind <> 'calibrated'
OR document #>> '{assessment,calibration}' = 'calibrated'
),
CONSTRAINT policy_versions_document_sha256_size CHECK (
octet_length(document_sha256) = 32
),
-- A version is never backdated: publishing it cannot change which version
-- applied at a moment that already passed.
CONSTRAINT policy_versions_effective_after_creation CHECK (
effective_at >= created_at
)
);
-- Sets created_at from the database clock and defaults effective_at to it.
-- NOT NULL and CHECK constraints run after BEFORE triggers, so they see the
-- stamped values.
-- +goose StatementBegin
CREATE FUNCTION policy_versions_stamp_creation() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
NEW.created_at := clock_timestamp();
NEW.effective_at := coalesce(NEW.effective_at, NEW.created_at);
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER policy_versions_stamp_creation
BEFORE INSERT ON policy_versions
FOR EACH ROW EXECUTE FUNCTION policy_versions_stamp_creation();
-- Refuses every UPDATE, DELETE, and TRUNCATE of policy versions. The error
-- uses SQLSTATE 23000 (integrity_constraint_violation) and the constraint name
-- policy_versions_immutable, which the adapter maps to a port sentinel without
-- reading the message. The message quotes no row values.
-- +goose StatementBegin
CREATE FUNCTION policy_versions_refuse_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'policy_versions_immutable',
TABLE = 'policy_versions',
MESSAGE = 'policy versions are immutable: publish a new version instead';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER policy_versions_refuse_update_delete
BEFORE UPDATE OR DELETE ON policy_versions
FOR EACH ROW EXECUTE FUNCTION policy_versions_refuse_change();
-- Row triggers do not fire for TRUNCATE, so a statement trigger covers it.
CREATE TRIGGER policy_versions_refuse_truncate
BEFORE TRUNCATE ON policy_versions
FOR EACH STATEMENT EXECUTE FUNCTION policy_versions_refuse_change();
-- +goose Down
DROP TABLE policy_versions;
DROP FUNCTION policy_versions_refuse_change();
DROP FUNCTION policy_versions_stamp_creation();