-- 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;