Skip to content

Commit

Permalink
Use sqlc.embed to embed a RiverJob row during unique insertion (#464
Browse files Browse the repository at this point in the history
)

Follows up #451 with a significant code quality improvement in which we
can use `sqlc.embed` (which TIL about) to embed a `RiverJob` row
directly on the returned struct, which means we can use our normal
`jobRowFromInternal` to map it to a driver result instead of having to
manually construct `RiverJob` property by property.

Tipped off to the existence of `sqlc.embed` by @tadejsv [1]. Thank you!

[1] #451 (comment)
  • Loading branch information
brandur authored Jul 20, 2024
1 parent 439c789 commit 3a4a06d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 109 deletions.
54 changes: 19 additions & 35 deletions riverdriver/riverdatabasesql/internal/dbsqlc/river_job.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 1 addition & 19 deletions riverdriver/riverdatabasesql/river_database_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,25 +302,7 @@ func (e *Executor) JobInsertUnique(ctx context.Context, params *riverdriver.JobI
return nil, interpretError(err)
}

jobRow, err := jobRowFromInternal(&dbsqlc.RiverJob{
ID: insertRes.ID,
Args: insertRes.Args,
Attempt: insertRes.Attempt,
AttemptedAt: insertRes.AttemptedAt,
AttemptedBy: insertRes.AttemptedBy,
CreatedAt: insertRes.CreatedAt,
Errors: insertRes.Errors,
FinalizedAt: insertRes.FinalizedAt,
Kind: insertRes.Kind,
MaxAttempts: insertRes.MaxAttempts,
Metadata: insertRes.Metadata,
Priority: insertRes.Priority,
Queue: insertRes.Queue,
ScheduledAt: insertRes.ScheduledAt,
State: insertRes.State,
Tags: insertRes.Tags,
UniqueKey: insertRes.UniqueKey,
})
jobRow, err := jobRowFromInternal(&insertRes.RiverJob)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion riverdriver/riverpgxv5/internal/dbsqlc/river_job.sql
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ INSERT INTO river_job(
ON CONFLICT (kind, unique_key) WHERE unique_key IS NOT NULL
-- Something needs to be updated for a row to be returned on a conflict.
DO UPDATE SET kind = EXCLUDED.kind
RETURNING *, (xmax != 0) AS unique_skipped_as_duplicate;
RETURNING sqlc.embed(river_job), (xmax != 0) AS unique_skipped_as_duplicate;

-- Run by the rescuer to queue for retry or discard depending on job state.
-- name: JobRescueMany :exec
Expand Down
54 changes: 19 additions & 35 deletions riverdriver/riverpgxv5/internal/dbsqlc/river_job.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 1 addition & 19 deletions riverdriver/riverpgxv5/river_pgx_v5_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,7 @@ func (e *Executor) JobInsertUnique(ctx context.Context, params *riverdriver.JobI
return nil, interpretError(err)
}

jobRow, err := jobRowFromInternal(&dbsqlc.RiverJob{
ID: insertRes.ID,
Args: insertRes.Args,
Attempt: insertRes.Attempt,
AttemptedAt: insertRes.AttemptedAt,
AttemptedBy: insertRes.AttemptedBy,
CreatedAt: insertRes.CreatedAt,
Errors: insertRes.Errors,
FinalizedAt: insertRes.FinalizedAt,
Kind: insertRes.Kind,
MaxAttempts: insertRes.MaxAttempts,
Metadata: insertRes.Metadata,
Priority: insertRes.Priority,
Queue: insertRes.Queue,
ScheduledAt: insertRes.ScheduledAt,
State: insertRes.State,
Tags: insertRes.Tags,
UniqueKey: insertRes.UniqueKey,
})
jobRow, err := jobRowFromInternal(&insertRes.RiverJob)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3a4a06d

Please sign in to comment.