diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index cfb67e0ec..eba516c1f 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -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 } diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index 1a09188b1..62c625c6f 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -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; diff --git a/internal/infrastructure/postgres/source_org_test.go b/internal/infrastructure/postgres/source_org_test.go new file mode 100644 index 000000000..a256def16 --- /dev/null +++ b/internal/infrastructure/postgres/source_org_test.go @@ -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) + } +} diff --git a/internal/infrastructure/postgres/sources.go b/internal/infrastructure/postgres/sources.go index caf21b475..39a40f960 100644 --- a/internal/infrastructure/postgres/sources.go +++ b/internal/infrastructure/postgres/sources.go @@ -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