Files
archie/internal/infrastructure/postgres/migrations/0002_edastore.sql
T
sam f3471131ff feat(postgres): port EDA store to Postgres (captures, mappings, bindings, ledgers, tool_calls)
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.
2026-09-24 02:17:31 +10:00

115 lines
4.8 KiB
SQL

-- EDA schema: captures, mappings, bindings, the two dispatch ledgers and
-- tool_calls. This is the Postgres replacement for the PocketBase-owned
-- collections in internal/infrastructure/edastore.
--
-- Translations from the PocketBase/SQLite schema this replaces, and why:
-- * Record ids stay text. PocketBase issued a random 15-character id and the
-- wire carries binding/capture/mapping ids as opaque strings; the adapter
-- mints a random id in Go, so nothing changes for consumers.
-- * Times become timestamptz, matching the State Store migration. The
-- SQLite store formatted captures.received_at as fixed-width text so
-- lexicographic order matched chronological order (PocketBase autodates
-- are only millisecond-precise); Postgres orders timestamptz natively, so
-- that normalisation does not exist here. This loses sub-microsecond
-- precision, which the State Store migration already accepted for event
-- timestamps.
-- * Every column behind a Go int is bigint. binding_version and task_id are
-- int64 on the wire; tool_calls.attempt and duration_ms are Go ints.
-- * bindings.mapping is a plain text column rather than a real foreign key:
-- the PocketBase Relation field was not enforced by SQLite (foreign_keys
-- is off; PocketBase resolves relations in app code), and the store
-- accepts an empty mapping id on insert. A real FK would reject those rows.
-- * bindings.secret stays text with no length bound: the cipher envelope is
-- base64url and must never be truncated or normalised, or Decrypt fails.
-- +goose Up
CREATE TABLE captures (
id text PRIMARY KEY,
source text NOT NULL,
remote_addr text NOT NULL DEFAULT '',
content_type text NOT NULL DEFAULT '',
headers text NOT NULL DEFAULT '',
body text NOT NULL DEFAULT '',
authenticated boolean NOT NULL DEFAULT false,
received_at timestamptz NOT NULL
);
CREATE INDEX idx_captures_source ON captures (source, received_at);
CREATE TABLE mappings (
id text PRIMARY KEY,
name text NOT NULL,
source_hint text NOT NULL DEFAULT '',
fields text NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE bindings (
id text PRIMARY KEY,
name text NOT NULL,
source text NOT NULL,
mapping text NOT NULL DEFAULT '',
workflow text NOT NULL DEFAULT '',
owner text NOT NULL DEFAULT '',
repo text NOT NULL DEFAULT '',
version bigint NOT NULL DEFAULT 0,
status text NOT NULL DEFAULT 'pending_approval',
secret text NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_bindings_source_status ON bindings (source, status);
-- "One binding per source" is now schema-enforced rather than read-then-refuse
-- in the store: a concurrent second insert for a source is refused by the
-- database (SQLSTATE 23505), which the store maps to ErrBindingOverlap.
CREATE UNIQUE INDEX idx_bindings_source ON bindings (source);
-- "One armed binding per source" is a distinct rule. The full unique above
-- already implies it, but the partial index keeps the invariant explicit and
-- durable if the per-source rule is ever relaxed to allow multiple matchers
-- while still forbidding two armed ones.
CREATE UNIQUE INDEX idx_bindings_armed_source ON bindings (source) WHERE status = 'armed';
CREATE TABLE binding_dispatches (
binding text NOT NULL,
binding_version bigint NOT NULL DEFAULT 0,
capture text NOT NULL,
task_id bigint NOT NULL DEFAULT 0,
dispatched_at timestamptz NOT NULL DEFAULT now()
);
-- At-most-once: binding_version is stored but deliberately not part of the key,
-- so a version bump does not permit re-dispatching the same (binding, capture).
CREATE UNIQUE INDEX idx_binding_dispatch_once ON binding_dispatches (binding, capture);
CREATE TABLE playbook_dispatches (
playbook_id text NOT NULL,
playbook_version text NOT NULL,
event_id text NOT NULL,
action_id text NOT NULL,
dispatched_at timestamptz NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX idx_playbook_dispatch_once ON playbook_dispatches
(playbook_id, playbook_version, event_id, action_id);
CREATE TABLE tool_calls (
id text PRIMARY KEY,
task_id bigint NOT NULL DEFAULT 0,
attempt bigint NOT NULL DEFAULT 0,
tool text NOT NULL,
args text NOT NULL DEFAULT '',
result text NOT NULL DEFAULT '',
error text NOT NULL DEFAULT '',
duration_ms bigint NOT NULL DEFAULT 0,
called_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_tool_calls_task ON tool_calls (task_id, attempt);
-- +goose Down
DROP TABLE tool_calls;
DROP TABLE playbook_dispatches;
DROP TABLE binding_dispatches;
DROP TABLE bindings;
DROP TABLE mappings;
DROP TABLE captures;