Files
archie/internal/infrastructure/postgres/queries/resources.sql
T
sam 67c5447ebc fix(controlplane): refresh a stale seeded resource and re-seed a removed one
A seed was consulted only for a resource kind the store did not hold, so a
document that changed in the build never reached a database an earlier
release had seeded. For workflow-definitions that is not a stale setting but
a total outage: the collection validates as one document, so a stored step
type this build no longer has fails every task's pin, whatever workflow the
task names. The seed's request ID was stable per kind and the store answers a
known request ID from resource_history, which outlives the resource, so the
documented recovery -- remove the resource and restart -- wrote nothing
either.

Seeds now carry one identity the reader can name, and a kind the store holds
is refreshed only when the seed itself wrote the newest revision: a
file-derived value stays the store's, and an operator's replacement is never
undone. The request ID is derived from the value, so an idempotent replay is
still one revision without making a second, changed document impossible. A
replay whose resource is gone re-creates it from the ledger's revision.

The shipped-vs-file-derived distinction is the seed's to make, so it is a
property of the definition (Defaults is set exactly for the kinds that ship a
document) rather than a check at each consumer.
2026-09-29 14:27:57 +10:00

57 lines
2.1 KiB
SQL

-- name: LockResourceWrite :exec
-- This lock is acquired in its own statement before a resource read. PostgreSQL
-- takes a fresh Read Committed snapshot for the subsequent read, so concurrent
-- optimistic writers see the committed revision rather than both writing v1.
SELECT pg_advisory_xact_lock(hashtextextended($1, 0));
-- name: ResourceByKind :one
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1;
-- name: ResourceByRequest :one
SELECT kind, value, version, actor, source, request_id, expected_version,
current_version, at
FROM resource_history
WHERE kind = $1 AND request_id = $2;
-- name: ReinsertResource :one
-- Re-creates the current row for a revision resource_history already records.
-- The ledger outlives the row it describes, so a resource an operator removed
-- still has its revisions; putting one back needs the row re-created without a
-- second ledger entry, which idx_resource_history_kind_request would refuse.
-- The revision is the ledger's, not a fresh one: this is the row that write
-- produced, restored.
INSERT INTO resources (kind, value, version, updated_at)
VALUES ($1, $2, $3, $4)
RETURNING kind, value, version, updated_at, org_id;
-- name: ResourceHistory :many
SELECT kind, value, version, actor, source, request_id, expected_version,
current_version, at
FROM resource_history
WHERE kind = $1
ORDER BY version DESC
LIMIT NULLIF(sqlc.arg(entry_limit)::bigint, 0);
-- name: ResourceVersion :one
SELECT version FROM resources WHERE kind = $1;
-- name: InsertResource :one
INSERT INTO resources (kind, value, version, updated_at)
VALUES ($1, $2, 1, $3)
RETURNING kind, value, version, updated_at, org_id;
-- name: UpdateResource :one
UPDATE resources
SET value = $2, version = version + 1, updated_at = $3
WHERE kind = $1 AND version = $4
RETURNING kind, value, version, updated_at, org_id;
-- name: InsertResourceHistory :one
INSERT INTO resource_history (
kind, value, version, actor, source, request_id, expected_version,
current_version, at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING kind, value, version, actor, source, request_id, expected_version,
current_version, at;