L2 of the PocketBase->Postgres migration (bead archie-core-7r0x.3): a new Postgres-backed EDA adapter (postgres.EDA) plus its goose migration (0002_edastore.sql) and sqlc queries (queries/edastore.sql), satisfying the same storecontract surfaces as internal/infrastructure/edastore.Store so the cutover wave can swap them behind the contract. The PocketBase opener in internal/app/archied/state_store.go is untouched: production still opens SQLite, and the cutover waits for the one-time import. Three recon-found hazards, each pinned by a test, not a comment: 1. Empty-secret early return. applyBinding deliberately never writes the secret column when the incoming secret is empty, so an edit form that does not echo the secret must not blank an armed binding's HMAC key. The UpdateBinding query keeps the stored secret via `secret = CASE WHEN $secret = '' THEN secret ELSE $secret END`; pinned by TestUpdateBindingEmptySecretPreservesStored (raw column unchanged across an empty-secret edit). 2. Unique-violation detection by SQLSTATE. isUniqueViolation previously grepped the driver's error text for "unique"/"already exists"; Postgres reports SQLSTATE 23505 and the text match would not fire, losing ErrAlreadyDispatched. The adapter detects by pgconn.PgError.Code == "23505"; pinned by TestIsUniqueViolationDetectsBySQLState (23505 with a message lacking those words is detected; a non-23505 whose text says "unique" is not). 3. One-binding-per-source / one-armed-binding-per-source were read-then-refuse with no transaction and a non-unique idx_bindings_source_status. Made them constraints: UNIQUE(source) plus a partial UNIQUE(source) WHERE status='armed' (the full unique subsumes the armed rule today, kept explicit for a future matcher extension). A concurrent insert for one source yields exactly one success and one ErrBindingOverlap; pinned by TestConcurrentInsertBindingSameSource. Dispatch ledgers kept exactly: UNIQUE(binding, capture) and UNIQUE(playbook_id, playbook_version, event_id, action_id); binding_version is stored but deliberately not part of the key (pinned by TestBindingDispatchVersionBumpDoesNotPermitRedispatch). The cipher is unchanged: AES-256-GCM, key = SHA-256(material), decrypt stays in the store and the gRPC wire still carries plaintext. Notify is left as an injected callback (EDA.SetNotify) and is NOT wired to LISTEN/NOTIFY. It is dead code today: edastore.Config.Notify is omitted at its only production construction and no consumer maps binding_changed/ mapping_changed, so building a LISTEN/NOTIFY path would be building for a consumer that does not exist. Wired or not is a separate decision.
100 lines
3.5 KiB
SQL
100 lines
3.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)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8);
|
|
|
|
-- name: ListCaptures :many
|
|
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at
|
|
FROM captures
|
|
ORDER BY received_at DESC
|
|
LIMIT $1;
|
|
|
|
-- name: ListUndispatchedCaptures :many
|
|
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at
|
|
FROM captures
|
|
WHERE source = ANY(@sources::text[])
|
|
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;
|