Merge branch 'run/pg-w2-3' into worktree-giggly-marinating-lamport
This commit is contained in:
commit
0335beabae
6 files changed
+1765
No files matched your search
@@ -0,0 +1,577 @@
|
||||
// Package postgres opens archie's PostgreSQL database and applies its schema
|
||||
// migrations. This file is the PostgreSQL implementation of the EDA
|
||||
// persistence contracts (captures, mappings, bindings and the two dispatch
|
||||
// ledgers) plus the tool_calls transcript, mirroring the PocketBase-backed
|
||||
// internal/infrastructure/edastore.Store it will replace in the cutover wave.
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
// EDA is the PostgreSQL implementation of every event-capture contract, plus
|
||||
// the tool_calls transcript the composition projection writes through. It
|
||||
// satisfies the same surface as *edastore.Store, so the cutover wave can swap
|
||||
// one for the other behind storecontract without touching the consumers.
|
||||
var (
|
||||
_ storecontract.CaptureStore = (*EDA)(nil)
|
||||
_ storecontract.MappingStore = (*EDA)(nil)
|
||||
_ storecontract.BindingStore = (*EDA)(nil)
|
||||
_ storecontract.BindingDispatcher = (*EDA)(nil)
|
||||
_ storecontract.PlaybookDispatcher = (*EDA)(nil)
|
||||
)
|
||||
|
||||
// EDA is the PostgreSQL-backed EDA persistence.
|
||||
type EDA struct {
|
||||
pool *pgxpool.Pool
|
||||
q *postgresdb.Queries
|
||||
cipher edastore.BindingCipher
|
||||
notify func(events.Event)
|
||||
}
|
||||
|
||||
// NewEDA builds an EDA store over pool. A nil cipher keeps binding secrets in
|
||||
// plaintext (the behaviour that predates the option); the caller resolves the
|
||||
// keyring through edastore.NewBindingCipher, exactly as the PocketBase store
|
||||
// does. The pool is owned by the caller, not this store.
|
||||
func NewEDA(pool *pgxpool.Pool, cipher edastore.BindingCipher) *EDA {
|
||||
return &EDA{pool: pool, q: postgresdb.New(pool), cipher: cipher}
|
||||
}
|
||||
|
||||
// SetNotify wires the change-event callback that the PocketBase store's
|
||||
// Config.Notify provides. It is the same injection point: nil is a silent
|
||||
// no-op, which is what every production construction gets today.
|
||||
func (s *EDA) SetNotify(fn func(events.Event)) { s.notify = fn }
|
||||
|
||||
// notifyWrite announces one successful write on the bindings or mappings
|
||||
// tables. It mirrors edastore.(*Store).notifyWrite byte-for-byte: the data
|
||||
// carries the record id and action, and consumers refetch the row themselves.
|
||||
func (s *EDA) notifyWrite(kind, subject, action, id string) {
|
||||
if s.notify == nil {
|
||||
return
|
||||
}
|
||||
past := map[string]string{"create": "created", "update": "updated", "approve": "approved", "delete": "deleted"}[action]
|
||||
s.notify(events.Event{
|
||||
Kind: kind,
|
||||
Detail: subject + " " + past,
|
||||
Data: map[string]any{"id": id, "action": action},
|
||||
})
|
||||
}
|
||||
|
||||
func newRecordID() string { return uuid.NewString() }
|
||||
|
||||
// --- captures ---
|
||||
|
||||
// InsertCapture stores one inbound event verbatim. retention and maxEvents are
|
||||
// accepted for contract compatibility and applied as a prune after the insert,
|
||||
// exactly as the PocketBase store does.
|
||||
func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
|
||||
received := c.ReceivedAt
|
||||
if received.IsZero() {
|
||||
received = time.Now()
|
||||
}
|
||||
id := newRecordID()
|
||||
if err := s.q.InsertCapture(ctx, postgresdb.InsertCaptureParams{
|
||||
ID: id,
|
||||
Source: c.Source,
|
||||
RemoteAddr: c.RemoteAddr,
|
||||
ContentType: c.ContentType,
|
||||
Headers: c.Headers,
|
||||
Body: c.Body,
|
||||
Authenticated: c.Authenticated,
|
||||
ReceivedAt: received.UTC(),
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("edastore: insert capture: %w", err)
|
||||
}
|
||||
if err := s.pruneCaptures(ctx, retention, maxEvents); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *EDA) pruneCaptures(ctx context.Context, retention time.Duration, maxEvents int) error {
|
||||
if retention > 0 {
|
||||
if err := s.q.DeleteCapturesOlderThan(ctx, time.Now().Add(-retention).UTC()); err != nil {
|
||||
return fmt.Errorf("edastore: prune captures by age: %w", err)
|
||||
}
|
||||
}
|
||||
if maxEvents > 0 {
|
||||
if err := s.q.DeleteCapturesBeyondCount(ctx, int32(maxEvents)); err != nil {
|
||||
return fmt.Errorf("edastore: prune captures by count: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func captureValue(r postgresdb.Capture) storecontract.CapturedEvent {
|
||||
return storecontract.CapturedEvent{
|
||||
ID: r.ID,
|
||||
ReceivedAt: r.ReceivedAt,
|
||||
Source: r.Source,
|
||||
RemoteAddr: r.RemoteAddr,
|
||||
ContentType: r.ContentType,
|
||||
Headers: r.Headers,
|
||||
Body: r.Body,
|
||||
Authenticated: r.Authenticated,
|
||||
}
|
||||
}
|
||||
|
||||
// ListCaptures returns the newest captures first.
|
||||
func (s *EDA) ListCaptures(ctx context.Context, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
rows, err := s.q.ListCaptures(ctx, int32(limit))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list captures: %w", err)
|
||||
}
|
||||
out := make([]storecontract.CapturedEvent, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, captureValue(r))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListUndispatchedCaptures returns recent captures for the given sources that
|
||||
// no binding has dispatched yet.
|
||||
func (s *EDA) ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
if len(sources) == 0 || limit <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := s.q.ListUndispatchedCaptures(ctx, postgresdb.ListUndispatchedCapturesParams{
|
||||
Sources: sources,
|
||||
EntryLimit: int32(limit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list undispatched captures: %w", err)
|
||||
}
|
||||
out := make([]storecontract.CapturedEvent, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, captureValue(r))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// --- mappings ---
|
||||
|
||||
func marshalMappingFields(fields []mapping.Field) (string, error) {
|
||||
encoded, err := json.Marshal(fields)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("edastore: encode mapping fields: %w", err)
|
||||
}
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
func mappingValue(r postgresdb.Mapping) (mapping.Mapping, error) {
|
||||
var fields []mapping.Field
|
||||
if raw := r.Fields; raw != "" {
|
||||
if err := json.Unmarshal([]byte(raw), &fields); err != nil {
|
||||
return mapping.Mapping{}, fmt.Errorf("edastore: decode mapping fields: %w", err)
|
||||
}
|
||||
}
|
||||
return mapping.Mapping{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
SourceHint: r.SourceHint,
|
||||
Fields: fields,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *EDA) InsertMapping(ctx context.Context, m mapping.Mapping) (string, error) {
|
||||
encoded, err := marshalMappingFields(m.Fields)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
id := newRecordID()
|
||||
if err := s.q.InsertMapping(ctx, postgresdb.InsertMappingParams{
|
||||
ID: id,
|
||||
Name: m.Name,
|
||||
SourceHint: m.SourceHint,
|
||||
Fields: encoded,
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("edastore: insert mapping: %w", err)
|
||||
}
|
||||
s.notifyWrite(events.KindMappingChanged, "mapping", "create", id)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// GetMapping returns (nil, nil) for an absent mapping, matching the
|
||||
// PocketBase store's found=false convention the gRPC layer translates.
|
||||
func (s *EDA) GetMapping(ctx context.Context, id string) (*mapping.Mapping, error) {
|
||||
r, err := s.q.GetMapping(ctx, id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, err := mappingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func (s *EDA) ListMappings(ctx context.Context) ([]mapping.Mapping, error) {
|
||||
rows, err := s.q.ListMappings(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list mappings: %w", err)
|
||||
}
|
||||
out := make([]mapping.Mapping, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
m, err := mappingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *EDA) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
|
||||
encoded, err := marshalMappingFields(m.Fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := s.q.UpdateMapping(ctx, postgresdb.UpdateMappingParams{
|
||||
ID: m.ID,
|
||||
Name: m.Name,
|
||||
SourceHint: m.SourceHint,
|
||||
Fields: encoded,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrMappingNotFound
|
||||
}
|
||||
s.notifyWrite(events.KindMappingChanged, "mapping", "update", m.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EDA) DeleteMapping(ctx context.Context, id string) error {
|
||||
n, err := s.q.DeleteMapping(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrMappingNotFound
|
||||
}
|
||||
s.notifyWrite(events.KindMappingChanged, "mapping", "delete", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- bindings ---
|
||||
|
||||
func (s *EDA) encryptSecret(secret string) (string, error) {
|
||||
if s.cipher == nil {
|
||||
return secret, nil
|
||||
}
|
||||
encrypted, err := s.cipher.Encrypt(secret)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("edastore: encrypt binding secret: %w", err)
|
||||
}
|
||||
return encrypted, nil
|
||||
}
|
||||
|
||||
func (s *EDA) bindingValue(r postgresdb.Binding) (binding.Binding, error) {
|
||||
b := binding.Binding{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
Matcher: binding.Matcher{Source: r.Source},
|
||||
MappingID: r.Mapping,
|
||||
Workflow: r.Workflow,
|
||||
Owner: r.Owner,
|
||||
Repo: r.Repo,
|
||||
Version: int(r.Version),
|
||||
Status: binding.Status(r.Status),
|
||||
Secret: r.Secret,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}
|
||||
if s.cipher != nil && b.Secret != "" {
|
||||
plain, err := s.cipher.Decrypt(b.Secret)
|
||||
if err != nil {
|
||||
return binding.Binding{}, fmt.Errorf("edastore: decrypt binding secret: %w", err)
|
||||
}
|
||||
b.Secret = plain
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// InsertBinding stores a new binding as pending_approval. "One binding per
|
||||
// source" is a schema constraint (idx_bindings_source), so a concurrent
|
||||
// second insert for the same source is refused by the database and surfaced
|
||||
// here as ErrBindingOverlap.
|
||||
func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, error) {
|
||||
secret := b.Secret
|
||||
if secret != "" {
|
||||
var err error
|
||||
secret, err = s.encryptSecret(secret)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
id := newRecordID()
|
||||
err := s.q.InsertBinding(ctx, postgresdb.InsertBindingParams{
|
||||
ID: id,
|
||||
Name: b.Name,
|
||||
Source: b.Matcher.Source,
|
||||
Mapping: b.MappingID,
|
||||
Workflow: b.Workflow,
|
||||
Owner: b.Owner,
|
||||
Repo: b.Repo,
|
||||
Status: string(binding.StatusPendingApproval),
|
||||
Secret: secret,
|
||||
})
|
||||
if isUniqueViolation(err) {
|
||||
return "", storecontract.ErrBindingOverlap
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("edastore: insert binding: %w", err)
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "create", id)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// GetBinding returns (nil, nil) for an absent binding, matching the
|
||||
// PocketBase store's found=false convention the gRPC layer translates.
|
||||
func (s *EDA) GetBinding(ctx context.Context, id string) (*binding.Binding, error) {
|
||||
r, err := s.q.GetBinding(ctx, id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := s.bindingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &b, nil
|
||||
}
|
||||
|
||||
func (s *EDA) ListBindings(ctx context.Context) ([]binding.Binding, error) {
|
||||
rows, err := s.q.ListBindings(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list bindings: %w", err)
|
||||
}
|
||||
out := make([]binding.Binding, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
b, err := s.bindingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ArmedBindingsForSource returns the armed bindings a source can dispatch.
|
||||
func (s *EDA) ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error) {
|
||||
rows, err := s.q.ArmedBindingsForSource(ctx, source)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list armed bindings: %w", err)
|
||||
}
|
||||
out := make([]binding.Binding, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
b, err := s.bindingValue(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UpdateBinding rewrites a binding's editable fields, bumps its version and
|
||||
// drops it back to pending_approval. An empty secret means "keep the stored
|
||||
// one" (the CASE in the query), so an edit form that does not echo the secret
|
||||
// cannot blank an armed binding's HMAC key.
|
||||
func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error {
|
||||
secret := b.Secret
|
||||
if secret != "" {
|
||||
var err error
|
||||
secret, err = s.encryptSecret(secret)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
n, err := s.q.UpdateBinding(ctx, postgresdb.UpdateBindingParams{
|
||||
ID: b.ID,
|
||||
Name: b.Name,
|
||||
Source: b.Matcher.Source,
|
||||
Mapping: b.MappingID,
|
||||
Workflow: b.Workflow,
|
||||
Owner: b.Owner,
|
||||
Repo: b.Repo,
|
||||
Status: string(binding.StatusPendingApproval),
|
||||
Secret: secret,
|
||||
})
|
||||
if isUniqueViolation(err) {
|
||||
return storecontract.ErrBindingOverlap
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrBindingNotFound
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "update", b.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EDA) DeleteBinding(ctx context.Context, id string) error {
|
||||
n, err := s.q.DeleteBinding(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrBindingNotFound
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "delete", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApproveBinding is the only transition that arms a binding. "One armed
|
||||
// binding per source" is a partial unique constraint (idx_bindings_armed_source),
|
||||
// so a concurrent approval of a sibling binding on the same source is refused
|
||||
// by the database and surfaced here as ErrBindingOverlap.
|
||||
func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
|
||||
r, err := s.q.GetBinding(ctx, id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return storecontract.ErrBindingNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r.Status != string(binding.StatusPendingApproval) {
|
||||
return storecontract.ErrBindingTransition
|
||||
}
|
||||
n, err := s.q.SetBindingArmed(ctx, id)
|
||||
if isUniqueViolation(err) {
|
||||
return storecontract.ErrBindingOverlap
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrBindingNotFound
|
||||
}
|
||||
s.notifyWrite(events.KindBindingChanged, "binding", "approve", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- dispatch ledgers ---
|
||||
|
||||
// RecordDispatch writes one at-most-once binding dispatch. binding_version is
|
||||
// stored but is not part of the unique key, so a version bump does not permit
|
||||
// re-dispatching the same (binding, capture).
|
||||
func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64) error {
|
||||
err := s.q.InsertBindingDispatch(ctx, postgresdb.InsertBindingDispatchParams{
|
||||
Binding: bindingID,
|
||||
BindingVersion: bindingVersion,
|
||||
Capture: captureID,
|
||||
TaskID: taskID,
|
||||
})
|
||||
return ledgerWrite(err, "edastore: record dispatch")
|
||||
}
|
||||
|
||||
func (s *EDA) RecordPlaybookDispatch(ctx context.Context, playbookID, playbookVersion, eventID, actionID string) error {
|
||||
err := s.q.InsertPlaybookDispatch(ctx, postgresdb.InsertPlaybookDispatchParams{
|
||||
PlaybookID: playbookID,
|
||||
PlaybookVersion: playbookVersion,
|
||||
EventID: eventID,
|
||||
ActionID: actionID,
|
||||
})
|
||||
return ledgerWrite(err, "edastore: record playbook dispatch")
|
||||
}
|
||||
|
||||
func (s *EDA) DeletePlaybookDispatches(ctx context.Context, playbookID string) error {
|
||||
if err := s.q.DeletePlaybookDispatches(ctx, playbookID); err != nil {
|
||||
return fmt.Errorf("edastore: delete playbook dispatches: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ledgerWrite turns the unique-index refusal into ErrAlreadyDispatched and
|
||||
// leaves every other failure alone.
|
||||
func ledgerWrite(err error, what string) error {
|
||||
switch {
|
||||
case err == nil:
|
||||
return nil
|
||||
case isUniqueViolation(err):
|
||||
return storecontract.ErrAlreadyDispatched
|
||||
default:
|
||||
return fmt.Errorf("%s: %w", what, err)
|
||||
}
|
||||
}
|
||||
|
||||
// isUniqueViolation reports whether err is a PostgreSQL unique-index refusal,
|
||||
// detected by SQLSTATE 23505 rather than by matching the driver's error text.
|
||||
// A Postgres error whose message happens to say "unique" but whose SQLSTATE is
|
||||
// something else must not be treated as one.
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) {
|
||||
return false
|
||||
}
|
||||
return pgErr.Code == "23505"
|
||||
}
|
||||
|
||||
// --- tool calls ---
|
||||
|
||||
// InsertToolCall appends one row to the tool_calls transcript.
|
||||
func (s *EDA) InsertToolCall(ctx context.Context, tc edastore.ToolCall) error {
|
||||
calledAt := tc.CalledAt
|
||||
if calledAt.IsZero() {
|
||||
calledAt = time.Now()
|
||||
}
|
||||
if err := s.q.InsertToolCall(ctx, postgresdb.InsertToolCallParams{
|
||||
ID: newRecordID(),
|
||||
TaskID: tc.TaskID,
|
||||
Attempt: int64(tc.Attempt),
|
||||
Tool: tc.Tool,
|
||||
Result: tc.Result,
|
||||
Error: tc.Error,
|
||||
CalledAt: calledAt,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("edastore: insert tool call: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TaskToolCalls returns one task's tool calls in call order.
|
||||
func (s *EDA) TaskToolCalls(ctx context.Context, taskID int64) ([]edastore.ToolCall, error) {
|
||||
rows, err := s.q.TaskToolCalls(ctx, taskID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list tool calls: %w", err)
|
||||
}
|
||||
out := make([]edastore.ToolCall, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, edastore.ToolCall{
|
||||
ID: r.ID,
|
||||
TaskID: r.TaskID,
|
||||
Attempt: int(r.Attempt),
|
||||
Tool: r.Tool,
|
||||
Result: r.Result,
|
||||
Error: r.Error,
|
||||
CalledAt: r.CalledAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
|
||||
)
|
||||
|
||||
const edaTestKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
// edaFor builds an EDA store over a migrated database, with no cipher.
|
||||
func edaFor(t *testing.T) *EDA {
|
||||
t.Helper()
|
||||
pool, _ := migrated(t)
|
||||
return NewEDA(pool, nil)
|
||||
}
|
||||
|
||||
func edaWithCipher(t *testing.T, cipher edastore.BindingCipher) (*pgxpool.Pool, *EDA) {
|
||||
t.Helper()
|
||||
pool, _ := migrated(t)
|
||||
return pool, NewEDA(pool, cipher)
|
||||
}
|
||||
|
||||
func insertBinding(t *testing.T, s *EDA, source string) string {
|
||||
t.Helper()
|
||||
id, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: source}, Workflow: "implement",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Hazard 2: unique-violation detection must be by SQLSTATE, not by grepping
|
||||
// the driver's error text. A Postgres error whose message happens to contain
|
||||
// "unique" but whose SQLSTATE is not 23505 must not be treated as one, and a
|
||||
// 23505 with a message lacking those words must be detected.
|
||||
func TestIsUniqueViolationDetectsBySQLState(t *testing.T) {
|
||||
if !isUniqueViolation(&pgconn.PgError{Code: "23505", Message: "duplicate key value"}) {
|
||||
t.Error("SQLSTATE 23505 was not detected as a unique violation")
|
||||
}
|
||||
if isUniqueViolation(&pgconn.PgError{Code: "23502", Message: "null value in column of a unique constraint"}) {
|
||||
t.Error("a non-23505 error whose text says unique was treated as a unique violation; SQLSTATE is authoritative")
|
||||
}
|
||||
if isUniqueViolation(errors.New("not a database error")) {
|
||||
t.Error("a non-Postgres error was treated as a unique violation")
|
||||
}
|
||||
}
|
||||
|
||||
// Hazard 2 (integration): the dispatch ledger is at-most-once, refused by the
|
||||
// schema and surfaced as ErrAlreadyDispatched.
|
||||
func TestRecordDispatchIsIdempotent(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
if err := s.RecordDispatch(t.Context(), "b1", 1, "c1", 42); err != nil {
|
||||
t.Fatalf("first RecordDispatch() error = %v", err)
|
||||
}
|
||||
if err := s.RecordDispatch(t.Context(), "b1", 1, "c1", 99); !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Errorf("second RecordDispatch() error = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
}
|
||||
|
||||
// binding_version is stored but deliberately not part of the unique key: a
|
||||
// version bump does not permit re-dispatching the same (binding, capture).
|
||||
func TestBindingDispatchVersionBumpDoesNotPermitRedispatch(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
if err := s.RecordDispatch(t.Context(), "b1", 1, "c1", 42); err != nil {
|
||||
t.Fatalf("first RecordDispatch() error = %v", err)
|
||||
}
|
||||
if err := s.RecordDispatch(t.Context(), "b1", 2, "c1", 99); !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Errorf("redispatch with a version bump = %v, want ErrAlreadyDispatched: binding_version is not part of the key", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaybookDispatchIsIdempotent(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
if err := s.RecordPlaybookDispatch(t.Context(), "p1", "v1", "e1", "a1"); err != nil {
|
||||
t.Fatalf("first RecordPlaybookDispatch() error = %v", err)
|
||||
}
|
||||
if err := s.RecordPlaybookDispatch(t.Context(), "p1", "v1", "e1", "a1"); !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Errorf("second RecordPlaybookDispatch() error = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Hazard 3: "one binding per source" is a schema constraint, so concurrent
|
||||
// inserts for one source produce exactly one success and one ErrBindingOverlap
|
||||
// rather than two bindings that a single SQLite writer would have serialised.
|
||||
func TestConcurrentInsertBindingSameSource(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
type result struct{ err error }
|
||||
start := make(chan struct{})
|
||||
results := make(chan result, 2)
|
||||
for range 2 {
|
||||
go func() {
|
||||
<-start
|
||||
_, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: "sentry"}, Workflow: "implement",
|
||||
})
|
||||
results <- result{err: err}
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
|
||||
var succeeded, overlapped int
|
||||
for range 2 {
|
||||
outcome := <-results
|
||||
switch {
|
||||
case outcome.err == nil:
|
||||
succeeded++
|
||||
case errors.Is(outcome.err, storecontract.ErrBindingOverlap):
|
||||
overlapped++
|
||||
default:
|
||||
t.Fatalf("concurrent InsertBinding error = %v", outcome.err)
|
||||
}
|
||||
}
|
||||
if succeeded != 1 || overlapped != 1 {
|
||||
t.Fatalf("concurrent inserts = %d succeeded, %d overlapped; want 1 each", succeeded, overlapped)
|
||||
}
|
||||
}
|
||||
|
||||
// Hazard 1: UpdateBinding with an empty secret must leave the stored envelope
|
||||
// untouched, so an edit form that does not echo the secret cannot blank an
|
||||
// armed binding's HMAC key.
|
||||
func TestUpdateBindingEmptySecretPreservesStored(t *testing.T) {
|
||||
cipher, err := edastore.NewBindingCipher(edaTestKey, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBindingCipher() error = %v", err)
|
||||
}
|
||||
pool, s := edaWithCipher(t, cipher)
|
||||
|
||||
secretID, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: "src-secret"}, Workflow: "implement",
|
||||
Secret: "supersecretvalue0123",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
before := readRawSecret(t, pool, secretID)
|
||||
|
||||
// Edit with an empty secret: the stored envelope must not change.
|
||||
if err := s.UpdateBinding(t.Context(), binding.Binding{
|
||||
ID: secretID, Name: "renamed", Matcher: binding.Matcher{Source: "src-secret"}, Workflow: "escalate",
|
||||
}); err != nil {
|
||||
t.Fatalf("UpdateBinding() error = %v", err)
|
||||
}
|
||||
after := readRawSecret(t, pool, secretID)
|
||||
if after != before {
|
||||
t.Fatalf("stored secret changed on empty-secret edit: %q -> %q", before, after)
|
||||
}
|
||||
if after == "" {
|
||||
t.Fatal("stored secret is empty after an empty-secret edit; the binding was silently disarmed")
|
||||
}
|
||||
}
|
||||
|
||||
func readRawSecret(t *testing.T, pool *pgxpool.Pool, id string) string {
|
||||
t.Helper()
|
||||
var stored string
|
||||
if err := pool.QueryRow(t.Context(), "SELECT secret FROM bindings WHERE id = $1", id).Scan(&stored); err != nil {
|
||||
t.Fatalf("read raw secret: %v", err)
|
||||
}
|
||||
return stored
|
||||
}
|
||||
|
||||
// The cipher path must keep decrypt-in-store: what is stored is an envelope,
|
||||
// what reads back is the plaintext HMAC key the intake verifies with.
|
||||
func TestBindingSecretEncryptedAtRestAndDecryptedOnRead(t *testing.T) {
|
||||
cipher, err := edastore.NewBindingCipher(edaTestKey, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBindingCipher() error = %v", err)
|
||||
}
|
||||
pool, s := edaWithCipher(t, cipher)
|
||||
|
||||
id, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "n", Matcher: binding.Matcher{Source: "src-cipher"}, Workflow: "implement",
|
||||
Secret: "supersecretvalue0123",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding() error = %v", err)
|
||||
}
|
||||
stored := readRawSecret(t, pool, id)
|
||||
if stored == "supersecretvalue0123" {
|
||||
t.Fatal("secret is stored plaintext; it must be encrypted at rest")
|
||||
}
|
||||
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding() error = %v", err)
|
||||
}
|
||||
if got.Secret != "supersecretvalue0123" {
|
||||
t.Errorf("Secret = %q, want the plaintext on read: the wire carries plaintext, so decrypt must happen in the store", got.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
// The lifecycle gate survives the port: pending_approval -> armed, and only
|
||||
// from pending_approval.
|
||||
func TestBindingLifecycle(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
id := insertBinding(t, s, "src-lifecycle")
|
||||
|
||||
if err := s.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding(pending_approval) error = %v", err)
|
||||
}
|
||||
if err := s.ApproveBinding(t.Context(), id); !errors.Is(err, storecontract.ErrBindingTransition) {
|
||||
t.Errorf("re-approving an armed binding = %v, want ErrBindingTransition", err)
|
||||
}
|
||||
armed, err := s.ArmedBindingsForSource(t.Context(), "src-lifecycle")
|
||||
if err != nil || len(armed) != 1 {
|
||||
t.Fatalf("ArmedBindingsForSource() = %d bindings (err %v), want 1", len(armed), err)
|
||||
}
|
||||
}
|
||||
|
||||
// A capture round-trips its payload verbatim, and a dispatched capture drops
|
||||
// out of the undispatched listing.
|
||||
func TestCaptureRoundTripAndUndispatched(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
body := `{"action":"created"}`
|
||||
if _, err := s.InsertCapture(t.Context(), storecontract.CapturedEvent{
|
||||
Source: "sentry", Body: body, Headers: `{"X-Hook":"1"}`, ContentType: "application/json",
|
||||
}, 0, 0); err != nil {
|
||||
t.Fatalf("InsertCapture() error = %v", err)
|
||||
}
|
||||
list, err := s.ListCaptures(t.Context(), 10)
|
||||
if err != nil || len(list) != 1 {
|
||||
t.Fatalf("ListCaptures() = %d (err %v), want 1", len(list), err)
|
||||
}
|
||||
if list[0].Body != body {
|
||||
t.Errorf("Body round-trip = %q, want %q", list[0].Body, body)
|
||||
}
|
||||
|
||||
undispatched, err := s.ListUndispatchedCaptures(t.Context(), []string{"sentry"}, 10)
|
||||
if err != nil || len(undispatched) != 1 {
|
||||
t.Fatalf("ListUndispatchedCaptures() = %d (err %v), want 1", len(undispatched), err)
|
||||
}
|
||||
// Dispatch it; the capture must no longer be undispatched.
|
||||
if err := s.RecordDispatch(t.Context(), "b1", 1, list[0].ID, 42); err != nil {
|
||||
t.Fatalf("RecordDispatch() error = %v", err)
|
||||
}
|
||||
undispatched, err = s.ListUndispatchedCaptures(t.Context(), []string{"sentry"}, 10)
|
||||
if err != nil || len(undispatched) != 0 {
|
||||
t.Fatalf("ListUndispatchedCaptures() after dispatch = %d (err %v), want 0", len(undispatched), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingCRUD(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "payload-map"})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
got, err := s.GetMapping(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetMapping() = %v (err %v), want the mapping", got, err)
|
||||
}
|
||||
if got.Name != "payload-map" {
|
||||
t.Errorf("Name = %q, want payload-map", got.Name)
|
||||
}
|
||||
got.Name = "renamed"
|
||||
if err := s.UpdateMapping(t.Context(), *got); err != nil {
|
||||
t.Fatalf("UpdateMapping() error = %v", err)
|
||||
}
|
||||
if err := s.DeleteMapping(t.Context(), id); err != nil {
|
||||
t.Fatalf("DeleteMapping() error = %v", err)
|
||||
}
|
||||
if err := s.DeleteMapping(t.Context(), id); !errors.Is(err, storecontract.ErrMappingNotFound) {
|
||||
t.Errorf("DeleteMapping(absent) = %v, want ErrMappingNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The Notify callback is an injected seam, not a LISTEN/NOTIFY path: a store
|
||||
// wired with one fires a change event per successful bindings/mappings write,
|
||||
// and one wired with none is a silent no-op.
|
||||
func TestNotifyCallbackIsAnInjectedSeam(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
var got []events.Event
|
||||
s.SetNotify(func(e events.Event) { got = append(got, e) })
|
||||
|
||||
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "wired"})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping() error = %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("notified %d times, want 1", len(got))
|
||||
}
|
||||
if got[0].Data["id"] != id || got[0].Data["action"] != "create" {
|
||||
t.Errorf("event data = %+v, want id=%q action=create", got[0].Data, id)
|
||||
}
|
||||
|
||||
// A store composed without a notifier is a silent no-op.
|
||||
s.SetNotify(nil)
|
||||
if _, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "silent"}); err != nil {
|
||||
t.Fatalf("InsertMapping(nil notifier) error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// tool_calls is an append-only transcript with no unique index: repeated rows
|
||||
// are distinct entries, and one task's calls never surface in another's.
|
||||
func TestToolCallRoundTrip(t *testing.T) {
|
||||
s := edaFor(t)
|
||||
at := time.Date(2026, 9, 23, 1, 2, 3, 0, time.UTC)
|
||||
|
||||
for range 2 {
|
||||
if err := s.InsertToolCall(t.Context(), edastore.ToolCall{
|
||||
TaskID: 42, Attempt: 2, Tool: "shell", Result: "all checks passed", CalledAt: at,
|
||||
}); err != nil {
|
||||
t.Fatalf("InsertToolCall() error = %v", err)
|
||||
}
|
||||
}
|
||||
if err := s.InsertToolCall(t.Context(), edastore.ToolCall{
|
||||
TaskID: 7, Attempt: 1, Tool: "read_file", Error: "no such file", CalledAt: at.Add(time.Second),
|
||||
}); err != nil {
|
||||
t.Fatalf("InsertToolCall(other task) error = %v", err)
|
||||
}
|
||||
|
||||
got, err := s.TaskToolCalls(t.Context(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskToolCalls() error = %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("TaskToolCalls() returned %d rows, want 2 (the transcript is append-only)", len(got))
|
||||
}
|
||||
if got[0].Result != "all checks passed" || got[0].Error != "" {
|
||||
t.Errorf("first row result/error = %q/%q, want the summary with no error", got[0].Result, got[0].Error)
|
||||
}
|
||||
if got[0].CalledAt.IsZero() {
|
||||
t.Error("called_at did not round-trip")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
-- 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;
|
||||
@@ -0,0 +1,576 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: edastore.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const armedBindingsForSource = `-- 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
|
||||
`
|
||||
|
||||
func (q *Queries) ArmedBindingsForSource(ctx context.Context, source string) ([]Binding, error) {
|
||||
rows, err := q.db.Query(ctx, armedBindingsForSource, source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Binding
|
||||
for rows.Next() {
|
||||
var i Binding
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Source,
|
||||
&i.Mapping,
|
||||
&i.Workflow,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.Version,
|
||||
&i.Status,
|
||||
&i.Secret,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const deleteBinding = `-- name: DeleteBinding :execrows
|
||||
DELETE FROM bindings WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteBinding(ctx context.Context, id string) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteBinding, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteCapturesBeyondCount = `-- name: DeleteCapturesBeyondCount :exec
|
||||
DELETE FROM captures WHERE id NOT IN (
|
||||
SELECT id FROM captures ORDER BY received_at DESC LIMIT $1
|
||||
)
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteCapturesBeyondCount(ctx context.Context, limit int32) error {
|
||||
_, err := q.db.Exec(ctx, deleteCapturesBeyondCount, limit)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteCapturesOlderThan = `-- name: DeleteCapturesOlderThan :exec
|
||||
DELETE FROM captures WHERE received_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteCapturesOlderThan(ctx context.Context, receivedAt time.Time) error {
|
||||
_, err := q.db.Exec(ctx, deleteCapturesOlderThan, receivedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteMapping = `-- name: DeleteMapping :execrows
|
||||
DELETE FROM mappings WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteMapping(ctx context.Context, id string) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteMapping, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deletePlaybookDispatches = `-- name: DeletePlaybookDispatches :exec
|
||||
DELETE FROM playbook_dispatches WHERE playbook_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePlaybookDispatches(ctx context.Context, playbookID string) error {
|
||||
_, err := q.db.Exec(ctx, deletePlaybookDispatches, playbookID)
|
||||
return err
|
||||
}
|
||||
|
||||
const getBinding = `-- name: GetBinding :one
|
||||
SELECT id, name, source, mapping, workflow, owner, repo, version, status, secret, created_at, updated_at
|
||||
FROM bindings WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetBinding(ctx context.Context, id string) (Binding, error) {
|
||||
row := q.db.QueryRow(ctx, getBinding, id)
|
||||
var i Binding
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Source,
|
||||
&i.Mapping,
|
||||
&i.Workflow,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.Version,
|
||||
&i.Status,
|
||||
&i.Secret,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getMapping = `-- name: GetMapping :one
|
||||
SELECT id, name, source_hint, fields, created_at, updated_at
|
||||
FROM mappings WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetMapping(ctx context.Context, id string) (Mapping, error) {
|
||||
row := q.db.QueryRow(ctx, getMapping, id)
|
||||
var i Mapping
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.SourceHint,
|
||||
&i.Fields,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertBinding = `-- 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)
|
||||
`
|
||||
|
||||
type InsertBindingParams struct {
|
||||
ID string
|
||||
Name string
|
||||
Source string
|
||||
Mapping string
|
||||
Workflow string
|
||||
Owner string
|
||||
Repo string
|
||||
Status string
|
||||
Secret string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) error {
|
||||
_, err := q.db.Exec(ctx, insertBinding,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.Source,
|
||||
arg.Mapping,
|
||||
arg.Workflow,
|
||||
arg.Owner,
|
||||
arg.Repo,
|
||||
arg.Status,
|
||||
arg.Secret,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertBindingDispatch = `-- name: InsertBindingDispatch :exec
|
||||
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
`
|
||||
|
||||
type InsertBindingDispatchParams struct {
|
||||
Binding string
|
||||
BindingVersion int64
|
||||
Capture string
|
||||
TaskID int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertBindingDispatch(ctx context.Context, arg InsertBindingDispatchParams) error {
|
||||
_, err := q.db.Exec(ctx, insertBindingDispatch,
|
||||
arg.Binding,
|
||||
arg.BindingVersion,
|
||||
arg.Capture,
|
||||
arg.TaskID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertCapture = `-- 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)
|
||||
`
|
||||
|
||||
type InsertCaptureParams struct {
|
||||
ID string
|
||||
Source string
|
||||
RemoteAddr string
|
||||
ContentType string
|
||||
Headers string
|
||||
Body string
|
||||
Authenticated bool
|
||||
ReceivedAt time.Time
|
||||
}
|
||||
|
||||
// EDA queries: captures, mappings, bindings, dispatch ledgers, tool_calls.
|
||||
func (q *Queries) InsertCapture(ctx context.Context, arg InsertCaptureParams) error {
|
||||
_, err := q.db.Exec(ctx, insertCapture,
|
||||
arg.ID,
|
||||
arg.Source,
|
||||
arg.RemoteAddr,
|
||||
arg.ContentType,
|
||||
arg.Headers,
|
||||
arg.Body,
|
||||
arg.Authenticated,
|
||||
arg.ReceivedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertMapping = `-- name: InsertMapping :exec
|
||||
INSERT INTO mappings (id, name, source_hint, fields)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
`
|
||||
|
||||
type InsertMappingParams struct {
|
||||
ID string
|
||||
Name string
|
||||
SourceHint string
|
||||
Fields string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertMapping(ctx context.Context, arg InsertMappingParams) error {
|
||||
_, err := q.db.Exec(ctx, insertMapping,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.SourceHint,
|
||||
arg.Fields,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertPlaybookDispatch = `-- name: InsertPlaybookDispatch :exec
|
||||
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
`
|
||||
|
||||
type InsertPlaybookDispatchParams struct {
|
||||
PlaybookID string
|
||||
PlaybookVersion string
|
||||
EventID string
|
||||
ActionID string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertPlaybookDispatch(ctx context.Context, arg InsertPlaybookDispatchParams) error {
|
||||
_, err := q.db.Exec(ctx, insertPlaybookDispatch,
|
||||
arg.PlaybookID,
|
||||
arg.PlaybookVersion,
|
||||
arg.EventID,
|
||||
arg.ActionID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertToolCall = `-- name: InsertToolCall :exec
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
`
|
||||
|
||||
type InsertToolCallParams struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
Attempt int64
|
||||
Tool string
|
||||
Result string
|
||||
Error string
|
||||
CalledAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) InsertToolCall(ctx context.Context, arg InsertToolCallParams) error {
|
||||
_, err := q.db.Exec(ctx, insertToolCall,
|
||||
arg.ID,
|
||||
arg.TaskID,
|
||||
arg.Attempt,
|
||||
arg.Tool,
|
||||
arg.Result,
|
||||
arg.Error,
|
||||
arg.CalledAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listBindings = `-- 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
|
||||
`
|
||||
|
||||
func (q *Queries) ListBindings(ctx context.Context) ([]Binding, error) {
|
||||
rows, err := q.db.Query(ctx, listBindings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Binding
|
||||
for rows.Next() {
|
||||
var i Binding
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Source,
|
||||
&i.Mapping,
|
||||
&i.Workflow,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.Version,
|
||||
&i.Status,
|
||||
&i.Secret,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listCaptures = `-- name: ListCaptures :many
|
||||
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at
|
||||
FROM captures
|
||||
ORDER BY received_at DESC
|
||||
LIMIT $1
|
||||
`
|
||||
|
||||
func (q *Queries) ListCaptures(ctx context.Context, limit int32) ([]Capture, error) {
|
||||
rows, err := q.db.Query(ctx, listCaptures, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Capture
|
||||
for rows.Next() {
|
||||
var i Capture
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Source,
|
||||
&i.RemoteAddr,
|
||||
&i.ContentType,
|
||||
&i.Headers,
|
||||
&i.Body,
|
||||
&i.Authenticated,
|
||||
&i.ReceivedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMappings = `-- name: ListMappings :many
|
||||
SELECT id, name, source_hint, fields, created_at, updated_at
|
||||
FROM mappings ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
func (q *Queries) ListMappings(ctx context.Context) ([]Mapping, error) {
|
||||
rows, err := q.db.Query(ctx, listMappings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Mapping
|
||||
for rows.Next() {
|
||||
var i Mapping
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.SourceHint,
|
||||
&i.Fields,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listUndispatchedCaptures = `-- name: ListUndispatchedCaptures :many
|
||||
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at
|
||||
FROM captures
|
||||
WHERE source = ANY($1::text[])
|
||||
AND id NOT IN (SELECT capture FROM binding_dispatches)
|
||||
ORDER BY received_at DESC
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListUndispatchedCapturesParams struct {
|
||||
Sources []string
|
||||
EntryLimit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListUndispatchedCaptures(ctx context.Context, arg ListUndispatchedCapturesParams) ([]Capture, error) {
|
||||
rows, err := q.db.Query(ctx, listUndispatchedCaptures, arg.Sources, arg.EntryLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Capture
|
||||
for rows.Next() {
|
||||
var i Capture
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Source,
|
||||
&i.RemoteAddr,
|
||||
&i.ContentType,
|
||||
&i.Headers,
|
||||
&i.Body,
|
||||
&i.Authenticated,
|
||||
&i.ReceivedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const setBindingArmed = `-- name: SetBindingArmed :execrows
|
||||
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) SetBindingArmed(ctx context.Context, id string) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, setBindingArmed, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const taskToolCalls = `-- 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
|
||||
`
|
||||
|
||||
type TaskToolCallsRow struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
Attempt int64
|
||||
Tool string
|
||||
Result string
|
||||
Error string
|
||||
CalledAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) TaskToolCalls(ctx context.Context, taskID int64) ([]TaskToolCallsRow, error) {
|
||||
rows, err := q.db.Query(ctx, taskToolCalls, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []TaskToolCallsRow
|
||||
for rows.Next() {
|
||||
var i TaskToolCallsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.TaskID,
|
||||
&i.Attempt,
|
||||
&i.Tool,
|
||||
&i.Result,
|
||||
&i.Error,
|
||||
&i.CalledAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateBinding = `-- name: UpdateBinding :execrows
|
||||
UPDATE bindings
|
||||
SET name = $2, source = $3, mapping = $4, workflow = $5, owner = $6, repo = $7,
|
||||
version = version + 1, status = $8,
|
||||
secret = CASE WHEN $9::text = '' THEN secret ELSE $9 END,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateBindingParams struct {
|
||||
ID string
|
||||
Name string
|
||||
Source string
|
||||
Mapping string
|
||||
Workflow string
|
||||
Owner string
|
||||
Repo string
|
||||
Status string
|
||||
Secret string
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (q *Queries) UpdateBinding(ctx context.Context, arg UpdateBindingParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateBinding,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.Source,
|
||||
arg.Mapping,
|
||||
arg.Workflow,
|
||||
arg.Owner,
|
||||
arg.Repo,
|
||||
arg.Status,
|
||||
arg.Secret,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const updateMapping = `-- name: UpdateMapping :execrows
|
||||
UPDATE mappings
|
||||
SET name = $2, source_hint = $3, fields = $4, updated_at = now()
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateMappingParams struct {
|
||||
ID string
|
||||
Name string
|
||||
SourceHint string
|
||||
Fields string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateMapping(ctx context.Context, arg UpdateMappingParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateMapping,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.SourceHint,
|
||||
arg.Fields,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -16,6 +16,40 @@ type ApplyStatus struct {
|
||||
ReportedAt time.Time
|
||||
}
|
||||
|
||||
type Binding struct {
|
||||
ID string
|
||||
Name string
|
||||
Source string
|
||||
Mapping string
|
||||
Workflow string
|
||||
Owner string
|
||||
Repo string
|
||||
Version int64
|
||||
Status string
|
||||
Secret string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type BindingDispatch struct {
|
||||
Binding string
|
||||
BindingVersion int64
|
||||
Capture string
|
||||
TaskID int64
|
||||
DispatchedAt time.Time
|
||||
}
|
||||
|
||||
type Capture struct {
|
||||
ID string
|
||||
Source string
|
||||
RemoteAddr string
|
||||
ContentType string
|
||||
Headers string
|
||||
Body string
|
||||
Authenticated bool
|
||||
ReceivedAt time.Time
|
||||
}
|
||||
|
||||
type ChannelStatus struct {
|
||||
ID string
|
||||
Name string
|
||||
@@ -85,6 +119,23 @@ type IdentitySubject struct {
|
||||
BoundAt time.Time
|
||||
}
|
||||
|
||||
type Mapping struct {
|
||||
ID string
|
||||
Name string
|
||||
SourceHint string
|
||||
Fields string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type PlaybookDispatch struct {
|
||||
PlaybookID string
|
||||
PlaybookVersion string
|
||||
EventID string
|
||||
ActionID string
|
||||
DispatchedAt time.Time
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
Kind string
|
||||
Value []byte
|
||||
@@ -141,6 +192,18 @@ type Task struct {
|
||||
ReviewCursor int64
|
||||
}
|
||||
|
||||
type ToolCall struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
Attempt int64
|
||||
Tool string
|
||||
Args string
|
||||
Result string
|
||||
Error string
|
||||
DurationMs int64
|
||||
CalledAt time.Time
|
||||
}
|
||||
|
||||
type Transition struct {
|
||||
ID int64
|
||||
TaskID int64
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
-- 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;
|
||||
Reference in new issue
Block a user