fix(runs,pg): grant the runtime role on sequences, and read a missing result as null (archie #2005)

Two faults stopped an investigation from finishing on staging.

The runtime role was granted DML on every table but nothing on sequences, so
the first table keyed by a serial (deliveries.messages) failed its insert with
"permission denied for sequence messages_seq_seq", failing the run at its
notification step.

A run that failed has no result, and a step with no output is recorded as NULL;
the rows were read straight into jsontext.Value, which can't hold NULL, so the
trail API answered 500 for exactly the runs and steps that needed reading. The
reads coalesce to JSON null instead.

Verified on staging after deploying: the same run's trail reads back.
This commit is contained in:
sam committed 2026-11-16 00:09:46 +11:00
1 parent 18d1e07a21
commit 6d97f62929
2 files changed
+11 -4

No files matched your search

+4
View File
@@ -78,6 +78,10 @@ func grantRuntime(ctx context.Context, db *sqlx.DB, runtimeURL string, schemas [
for _, stmt := range []string{
"GRANT USAGE ON SCHEMA " + name + " TO " + role,
"GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA " + name + " TO " + role,
// A table keyed by a serial draws from a sequence, which is its
// own grant: a module whose insert draws on one would fail DML it
// is otherwise allowed.
"GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA " + name + " TO " + role,
} {
if _, err := db.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("grant runtime role on %s: %w", schema, err)
+7 -4
View File
@@ -130,7 +130,7 @@ func (s *Store) RenewLease(ctx context.Context, id, token uuid.UUID, ttl time.Du
func (s *Store) Steps(ctx context.Context, id uuid.UUID) ([]Step, error) {
var steps []Step
if err := s.db.SelectContext(ctx, &steps, `
SELECT run_id, seq, name, outcome, output, error FROM runs.steps
SELECT run_id, seq, name, outcome, coalesce(output, 'null') AS output, error FROM runs.steps
WHERE run_id = $1 ORDER BY seq`, id); err != nil {
return nil, fmt.Errorf("load steps: %w", err)
}
@@ -260,7 +260,9 @@ type Summary struct {
FinishedAt sql.NullTime `db:"finished_at"`
}
// Detail is a run with its input and result.
// Detail is a run with its input and result. A run that failed has no result,
// which the row holds as NULL and every reader gets as JSON null, as a step
// with no output does.
type Detail struct {
Summary
Input jsontext.Value `db:"input"`
@@ -289,7 +291,8 @@ func (s *Store) Recent(ctx context.Context, orgID string, limit int) ([]Summary,
func (s *Store) Detail(ctx context.Context, orgID string, id uuid.UUID) (Detail, bool, error) {
var d Detail
err := s.db.GetContext(ctx, &d, `
SELECT id, workflow, state, attempts, error, created_at, finished_at, input, result
SELECT id, workflow, state, attempts, error, created_at, finished_at, input,
coalesce(result, 'null') AS result
FROM runs.runs WHERE org_id = $1 AND id = $2`, orgID, id)
if errors.Is(err, sql.ErrNoRows) {
return Detail{}, false, nil
@@ -304,7 +307,7 @@ func (s *Store) Detail(ctx context.Context, orgID string, id uuid.UUID) (Detail,
func (s *Store) RecordedSteps(ctx context.Context, id uuid.UUID) ([]Recorded, error) {
var out []Recorded
if err := s.db.SelectContext(ctx, &out, `
SELECT run_id, seq, name, outcome, output, error, recorded_at FROM runs.steps
SELECT run_id, seq, name, outcome, coalesce(output, 'null') AS output, error, recorded_at FROM runs.steps
WHERE run_id = $1 ORDER BY seq`, id); err != nil {
return nil, fmt.Errorf("load steps: %w", err)
}