Files
archie/internal/infrastructure/postgres/queries/edastore.sql
T
sam 8a2b5b95f9 merge: t2db.40 sources and signing into pg/foundation
Resolves against t2db.36: captures carry both event_type and unsigned (proto field 10), the State Store serves sources and event types from the Postgres EDA store, the capture row shows event type and signature together, and the staterpc sentinels map both ways from one table.
2026-09-24 14:38:40 +10:00

145 lines
5.0 KiB
SQL

-- EDA queries: captures, mappings, bindings, dispatch ledgers, tool_calls.
-- name: InsertCapture :exec
INSERT INTO captures (id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
-- name: ListCaptures :many
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type
FROM captures
ORDER BY received_at DESC
LIMIT $1;
-- name: ListUndispatchedCaptures :many
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type
FROM captures
WHERE source = ANY(@sources::text[])
-- An unidentified capture is never dispatched.
AND event_type <> ''
AND id NOT IN (SELECT capture FROM binding_dispatches)
ORDER BY received_at DESC
LIMIT @entry_limit;
-- name: DeleteCapturesOlderThan :exec
DELETE FROM captures WHERE received_at < $1;
-- name: DeleteCapturesBeyondCount :exec
DELETE FROM captures WHERE id NOT IN (
SELECT id FROM captures ORDER BY received_at DESC LIMIT $1
);
-- name: InsertMapping :exec
INSERT INTO mappings (id, name, source_hint, fields)
VALUES ($1, $2, $3, $4);
-- name: GetMapping :one
SELECT id, name, source_hint, fields, created_at, updated_at
FROM mappings WHERE id = $1;
-- name: ListMappings :many
SELECT id, name, source_hint, fields, created_at, updated_at
FROM mappings ORDER BY created_at DESC;
-- name: UpdateMapping :execrows
UPDATE mappings
SET name = $2, source_hint = $3, fields = $4, updated_at = now()
WHERE id = $1;
-- name: DeleteMapping :execrows
DELETE FROM mappings WHERE id = $1;
-- name: InsertBinding :exec
INSERT INTO bindings (id, name, source, mapping, workflow, owner, repo, version, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, 1, $8);
-- name: GetBinding :one
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at
FROM bindings WHERE id = $1;
-- name: ListBindings :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at
FROM bindings ORDER BY created_at DESC;
-- name: ArmedBindingsForSource :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at
FROM bindings WHERE source = $1 AND status = 'armed' ORDER BY created_at DESC;
-- name: UpdateBinding :execrows
UPDATE bindings
SET name = $2, source = $3, mapping = $4, workflow = $5, owner = $6, repo = $7,
version = version + 1, status = $8, updated_at = now()
WHERE id = $1;
-- name: SetBindingArmed :execrows
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1;
-- name: DeleteBinding :execrows
DELETE FROM bindings WHERE id = $1;
-- name: InsertBindingDispatch :exec
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id)
VALUES ($1, $2, $3, $4);
-- name: InsertPlaybookDispatch :exec
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
VALUES ($1, $2, $3, $4);
-- name: DeletePlaybookDispatches :exec
DELETE FROM playbook_dispatches WHERE playbook_id = $1;
-- name: InsertToolCall :exec
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at)
VALUES ($1, $2, $3, $4, $5, $6, $7);
-- name: TaskToolCalls :many
SELECT id, task_id, attempt, tool, result, error, called_at
FROM tool_calls WHERE task_id = $1 ORDER BY called_at ASC;
-- name: InsertEventType :exec
INSERT INTO event_types (id, source, name, rule, schema)
VALUES ($1, $2, $3, $4, $5);
-- name: GetEventType :one
SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE id = $1;
-- name: ListEventTypes :many
SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types ORDER BY source, name;
-- name: EventTypesForSource :many
SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE source = $1 ORDER BY name;
-- name: UpdateEventType :execrows
UPDATE event_types SET name = $2, rule = $3, updated_at = now() WHERE id = $1;
-- name: DeleteEventType :execrows
DELETE FROM event_types WHERE id = $1;
-- name: LockEventTypeSource :exec
-- Serialises saves per source so two concurrent saves cannot each pass the
-- overlap check against a set that excludes the other.
SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text));
-- name: InsertSource :exec
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3);
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at FROM sources WHERE path = $1;
-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at FROM sources ORDER BY created_at DESC, path;
-- name: SetSourceSigning :execrows
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing);
-- name: SetSourceSecret :execrows
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1;
-- name: SourceExists :one
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
-- name: DeriveSources :exec
SELECT derive_sources();