feat(policy): add immutable versioned product policy store
This commit is contained in:
1 parent
0e2a3b02ab
commit
7a5105acc7
30 files changed
+3893
-37
No files matched your search
@@ -0,0 +1,116 @@
|
||||
-- +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();
|
||||
@@ -0,0 +1,96 @@
|
||||
-- Stores a version and returns the stored row with the publication time and
|
||||
-- effective time set by the stamp trigger. A taken ID or version number
|
||||
-- returns no row instead of an error, so a conflict does not abort the
|
||||
-- surrounding transaction. created_at is never taken from the caller, and a
|
||||
-- NULL effective_at means "at publication".
|
||||
-- name: InsertPolicyVersion :one
|
||||
INSERT INTO policy_versions (
|
||||
id,
|
||||
policy_key,
|
||||
version,
|
||||
kind,
|
||||
effective_at,
|
||||
author,
|
||||
document,
|
||||
document_sha256
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(policy_key),
|
||||
sqlc.arg(version),
|
||||
sqlc.arg(kind),
|
||||
sqlc.narg(effective_at),
|
||||
sqlc.arg(author),
|
||||
sqlc.arg(document),
|
||||
sqlc.arg(document_sha256)
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING
|
||||
id,
|
||||
policy_key,
|
||||
version,
|
||||
kind,
|
||||
effective_at,
|
||||
author,
|
||||
document,
|
||||
document_sha256,
|
||||
created_at;
|
||||
|
||||
-- name: GetPolicyVersionByID :one
|
||||
SELECT
|
||||
id,
|
||||
policy_key,
|
||||
version,
|
||||
kind,
|
||||
effective_at,
|
||||
author,
|
||||
document,
|
||||
document_sha256,
|
||||
created_at
|
||||
FROM policy_versions
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- Among the versions of a family already effective at a time, the newest
|
||||
-- publication wins. A version scheduled for later therefore never undoes a
|
||||
-- version published after it. The unique (policy_key, version) index serves
|
||||
-- the backward scan.
|
||||
-- name: GetEffectivePolicyVersion :one
|
||||
SELECT
|
||||
id,
|
||||
policy_key,
|
||||
version,
|
||||
kind,
|
||||
effective_at,
|
||||
author,
|
||||
document,
|
||||
document_sha256,
|
||||
created_at
|
||||
FROM policy_versions
|
||||
WHERE
|
||||
policy_key = sqlc.arg(policy_key)
|
||||
AND effective_at <= sqlc.arg(at)
|
||||
ORDER BY version DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetLatestPolicyVersion :one
|
||||
SELECT
|
||||
id,
|
||||
policy_key,
|
||||
version,
|
||||
kind,
|
||||
effective_at,
|
||||
author,
|
||||
document,
|
||||
document_sha256,
|
||||
created_at
|
||||
FROM policy_versions
|
||||
WHERE policy_key = sqlc.arg(policy_key)
|
||||
ORDER BY version DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- Reports whether a family has any version without reading a document, so a
|
||||
-- caller that only needs presence never depends on decoding.
|
||||
-- name: PolicyVersionExists :one
|
||||
SELECT exists(
|
||||
SELECT 1 FROM policy_versions
|
||||
WHERE policy_key = sqlc.arg(policy_key)
|
||||
) AS present;
|
||||
Reference in new issue
Block a user