fix(sources): store the acting org on source creation

This commit is contained in:
sam committed 2026-10-08 21:25:49 +11:00
1 parent b8bedf4d7b
commit d6b693a9b2
4 files changed
+51 -7

No files matched your search

@@ -586,17 +586,25 @@ func (q *Queries) InsertPlaybookDispatch(ctx context.Context, arg InsertPlaybook
}
const insertSource = `-- name: InsertSource :exec
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3)
INSERT INTO sources (path, signing, secret, org_id, workspace_id) VALUES ($1, $2, $3, $4, $5)
`
type InsertSourceParams struct {
Path string
Signing string
Secret string
Path string
Signing string
Secret string
OrgID string
WorkspaceID string
}
func (q *Queries) InsertSource(ctx context.Context, arg InsertSourceParams) error {
_, err := q.db.Exec(ctx, insertSource, arg.Path, arg.Signing, arg.Secret)
_, err := q.db.Exec(ctx, insertSource,
arg.Path,
arg.Signing,
arg.Secret,
arg.OrgID,
arg.WorkspaceID,
)
return err
}
@@ -152,7 +152,7 @@ DELETE FROM event_types WHERE id = $1;
-- overlap check against a set that excludes the other.
SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text));
-- name: InsertSource :exec
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3);
INSERT INTO sources (path, signing, secret, org_id, workspace_id) VALUES ($1, $2, $3, $4, $5);
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = $1;
@@ -0,0 +1,33 @@
package postgres_test
import (
"testing"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/source"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
)
// TestInsertSourceKeepsActingOrg pins that a source created while acting in a
// non-default org is owned by that org, so its captures inherit the org.
func TestInsertSourceKeepsActingOrg(t *testing.T) {
ctx := t.Context()
db := pgstore.Open(t)
eda := postgres.NewEDA(db.Pool, nil)
if err := eda.InsertSource(org.WithOrg(ctx, "acme"), source.Source{Path: "acme-hook", Signing: source.SigningUnsigned}); err != nil {
t.Fatal(err)
}
var owner, workspace string
if err := db.Pool.QueryRow(ctx, "SELECT org_id, workspace_id FROM sources WHERE path=$1", "acme-hook").Scan(&owner, &workspace); err != nil {
t.Fatal(err)
}
if owner != "acme" || workspace != string(org.DefaultWorkspaceID) {
t.Fatalf("source owner %s/%s, want acme/%s", owner, workspace, org.DefaultWorkspaceID)
}
// Lookup stays global: the capture receiver resolves by path alone.
got, err := eda.GetSource(ctx, "acme-hook")
if err != nil || got == nil {
t.Fatalf("GetSource = %#v, err %v", got, err)
}
}
+4 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/source"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
@@ -14,7 +15,8 @@ import (
var _ storecontract.SourceStore = (*EDA)(nil)
// InsertSource stores a new source. The path is the primary key, so a taken
// InsertSource stores a new source in the acting org's default workspace.
// The path is the primary key, so a taken
// path is refused by the database and surfaced as ErrSourcePathTaken.
func (s *EDA) InsertSource(ctx context.Context, src source.Source) error {
secret, err := s.sealSecret(src.Secret)
@@ -23,6 +25,7 @@ func (s *EDA) InsertSource(ctx context.Context, src source.Source) error {
}
err = s.q.InsertSource(ctx, postgresdb.InsertSourceParams{
Path: src.Path, Signing: string(src.Signing), Secret: secret,
OrgID: string(org.OrgFromContext(ctx)), WorkspaceID: string(org.DefaultWorkspaceID),
})
if isUniqueViolation(err) {
return storecontract.ErrSourcePathTaken