129 lines
4.5 KiB
SQL
129 lines
4.5 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, event_type)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
|
|
|
|
-- name: ListCaptures :many
|
|
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, 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, 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, secret)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, 1, $8, $9);
|
|
|
|
-- name: GetBinding :one
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, secret, created_at, updated_at
|
|
FROM bindings WHERE id = $1;
|
|
|
|
-- name: ListBindings :many
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, secret, created_at, updated_at
|
|
FROM bindings ORDER BY created_at DESC;
|
|
|
|
-- name: ArmedBindingsForSource :many
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, secret, created_at, updated_at
|
|
FROM bindings WHERE source = $1 AND status = 'armed' ORDER BY created_at DESC;
|
|
|
|
-- name: UpdateBinding :execrows
|
|
-- An empty secret means "keep the stored one": the CASE leaves the column
|
|
-- untouched so an edit form that does not echo the secret cannot blank an
|
|
-- armed binding's HMAC key.
|
|
UPDATE bindings
|
|
SET name = $2, source = $3, mapping = $4, workflow = $5, owner = $6, repo = $7,
|
|
version = version + 1, status = $8,
|
|
secret = CASE WHEN sqlc.arg(secret)::text = '' THEN secret ELSE sqlc.arg(secret) END,
|
|
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));
|