feat(eda): name and delete capture sources; an armed binding blocks the delete

This commit is contained in:
sam committed 2026-10-05 21:05:58 +11:00
1 parent 738044a949
commit d295fa2680
22 files changed
+1103 -592

No files matched your search

@@ -9,6 +9,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/eventtype"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/source"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
@@ -148,4 +149,24 @@ func TestBindingLifecycle(t *testing.T) {
if err := eda.PauseBinding(ctx, "missing"); !errors.Is(err, storecontract.ErrBindingNotFound) {
t.Fatalf("pause of a missing binding = %v, want ErrBindingNotFound", err)
}
// A source is not deleted while an armed binding fires on it.
if err := eda.InsertSource(ctx, source.Source{Path: "forge", Signing: source.SigningSigned}); err != nil {
t.Fatal(err)
}
if err := eda.ApproveBinding(ctx, id); err != nil {
t.Fatal(err)
}
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceInUse) {
t.Fatalf("delete of a source an armed binding fires on = %v, want ErrSourceInUse", err)
}
if err := eda.PauseBinding(ctx, id); err != nil {
t.Fatal(err)
}
if err := eda.DeleteSource(ctx, "forge"); err != nil {
t.Fatalf("delete of a source with no armed binding = %v", err)
}
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceNotFound) {
t.Fatalf("second delete = %v, want ErrSourceNotFound", err)
}
}
@@ -0,0 +1,10 @@
-- sources.name is the operator's label for a source; its path stays the
-- identity captures and bindings reference.
-- +goose Up
ALTER TABLE sources ADD COLUMN name text NOT NULL DEFAULT '';
-- +goose Down
ALTER TABLE sources DROP COLUMN name;
@@ -189,6 +189,21 @@ func (q *Queries) DeletePlaybookDispatches(ctx context.Context, playbookID strin
return err
}
const deleteUnboundSource = `-- name: DeleteUnboundSource :execrows
DELETE FROM sources s WHERE s.path = $1
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed')
`
// A source an armed binding fires on is not deleted: the binding would
// silently stop receiving events.
func (q *Queries) DeleteUnboundSource(ctx context.Context, path string) (int64, error) {
result, err := q.db.Exec(ctx, deleteUnboundSource, path)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const deriveSources = `-- name: DeriveSources :exec
SELECT derive_sources()
`
@@ -342,7 +357,7 @@ func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, err
}
const getSource = `-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources WHERE path = $1
`
func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
@@ -356,6 +371,7 @@ func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
&i.Name,
)
return i, err
}
@@ -837,7 +853,7 @@ func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
}
const listSources = `-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources ORDER BY created_at DESC, path
`
func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
@@ -857,6 +873,7 @@ func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
&i.Name,
); err != nil {
return nil, err
}
@@ -958,6 +975,23 @@ func (q *Queries) SetBindingDispatchTask(ctx context.Context, arg SetBindingDisp
return err
}
const setSourceName = `-- name: SetSourceName :execrows
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1
`
type SetSourceNameParams struct {
Path string
Name string
}
func (q *Queries) SetSourceName(ctx context.Context, arg SetSourceNameParams) (int64, error) {
result, err := q.db.Exec(ctx, setSourceName, arg.Path, arg.Name)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const setSourceSecret = `-- name: SetSourceSecret :execrows
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1
`
@@ -344,6 +344,7 @@ type Source struct {
UpdatedAt time.Time
OrgID string
WorkspaceID string
Name string
}
type StepExecution struct {
@@ -152,10 +152,10 @@ SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text))
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3);
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1;
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources WHERE path = $1;
-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path;
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources ORDER BY created_at DESC, path;
-- name: SetSourceSigning :execrows
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
@@ -164,6 +164,15 @@ 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: SetSourceName :execrows
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1;
-- name: DeleteUnboundSource :execrows
-- A source an armed binding fires on is not deleted: the binding would
-- silently stop receiving events.
DELETE FROM sources s WHERE s.path = $1
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed');
-- name: SourceExists :one
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
+32 -1
View File
@@ -102,6 +102,37 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
return nil
}
// SetSourceName relabels a source.
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name})
if err != nil {
return fmt.Errorf("edastore: set source name: %w", err)
}
if n == 0 {
return storecontract.ErrSourceNotFound
}
return nil
}
// DeleteSource deletes a source no armed binding fires on.
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
n, err := s.q.DeleteUnboundSource(ctx, path)
if err != nil {
return fmt.Errorf("edastore: delete source: %w", err)
}
if n > 0 {
return nil
}
exists, err := s.q.SourceExists(ctx, path)
if err != nil {
return fmt.Errorf("edastore: delete source: %w", err)
}
if exists {
return storecontract.ErrSourceInUse
}
return storecontract.ErrSourceNotFound
}
// sealSecret encrypts a non-empty secret when a cipher is configured.
func (s *EDA) sealSecret(secret string) (string, error) {
if s.cipher == nil || secret == "" {
@@ -124,7 +155,7 @@ func (s *EDA) sourceValue(r postgresdb.Source) (source.Source, error) {
secret = plain
}
return source.Source{
Path: r.Path, Signing: source.Signing(r.Signing), Secret: secret,
Path: r.Path, Name: r.Name, Signing: source.Signing(r.Signing), Secret: secret,
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
}, nil
}