Manager: Convert 'last-rendered' to sqlc

Convert 'last-rendered' to sqlc. The query is slightly suboptimal.
There's a bug in sqlc: the `ON CONFLICT DO UPDATE` clause is generated
incorrectly. See https://github.com/sqlc-dev/sqlc/issues/3334 for more
info.
This commit is contained in:
Sybren A. Stüvel 2024-06-08 21:30:00 +02:00
parent 1ec65e8c29
commit 5a31336efb
4 changed files with 76 additions and 21 deletions

@ -4,8 +4,10 @@ package persistence
import (
"context"
"database/sql"
"errors"
"gorm.io/gorm/clause"
"projects.blender.org/studio/flamenco/internal/manager/persistence/sqlc"
)
// LastRendered only has one entry in its database table, to indicate the job
@ -19,30 +21,32 @@ type LastRendered struct {
// SetLastRendered sets this job as the one with the most recent rendered image.
func (db *DB) SetLastRendered(ctx context.Context, j *Job) error {
render := LastRendered{
// Always use the same database ID to ensure a single entry.
Model: Model{ID: uint(1)},
JobID: j.ID,
Job: j,
queries, err := db.queries()
if err != nil {
return err
}
tx := db.gormDB.
WithContext(ctx).
Clauses(clause.OnConflict{UpdateAll: true}).
Create(&render)
return tx.Error
now := db.now()
return queries.SetLastRendered(ctx, sqlc.SetLastRenderedParams{
CreatedAt: now.Time,
UpdatedAt: now,
JobID: int64(j.ID),
})
}
// GetLastRendered returns the UUID of the job with the most recent rendered image.
func (db *DB) GetLastRenderedJobUUID(ctx context.Context) (string, error) {
job := Job{}
tx := db.gormDB.WithContext(ctx).
Joins("inner join last_rendereds LR on jobs.id = LR.job_id").
Select("uuid").
Find(&job)
if tx.Error != nil {
return "", jobError(tx.Error, "finding job with most rencent render")
queries, err := db.queries()
if err != nil {
return "", err
}
return job.UUID, nil
jobUUID, err := queries.GetLastRenderedJobUUID(ctx)
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
if err != nil {
return "", jobError(err, "finding job with most rencent render")
}
return jobUUID, nil
}

@ -190,3 +190,19 @@ WHERE task_id in (SELECT id FROM tasks WHERE job_id=@job_id);
SELECT sqlc.embed(workers) FROM workers
INNER JOIN task_failures TF on TF.worker_id=workers.id
WHERE TF.task_id=@task_id;
-- name: SetLastRendered :exec
-- Set the 'last rendered' job info.
--
-- Note that the use of ?2 and ?3 in the SQL is not desirable, and should be
-- replaced with @updated_at and @job_id as soon as sqlc issue #3334 is fixed.
-- See https://github.com/sqlc-dev/sqlc/issues/3334 for more info.
INSERT INTO last_rendereds (id, created_at, updated_at, job_id)
VALUES (1, @created_at, @updated_at, @job_id)
ON CONFLICT DO UPDATE
SET updated_at=?2, job_id=?3
WHERE id=1;
-- name: GetLastRenderedJobUUID :one
SELECT uuid FROM jobs
INNER JOIN last_rendereds LR ON jobs.id = LR.job_id;

@ -623,6 +623,18 @@ func (q *Queries) FetchTasksOfWorkerInStatusOfJob(ctx context.Context, arg Fetch
return items, nil
}
const getLastRenderedJobUUID = `-- name: GetLastRenderedJobUUID :one
SELECT uuid FROM jobs
INNER JOIN last_rendereds LR ON jobs.id = LR.job_id
`
func (q *Queries) GetLastRenderedJobUUID(ctx context.Context) (string, error) {
row := q.db.QueryRowContext(ctx, getLastRenderedJobUUID)
var uuid string
err := row.Scan(&uuid)
return uuid, err
}
const jobCountTaskStatuses = `-- name: JobCountTaskStatuses :many
SELECT status, count(*) as num_tasks FROM tasks
WHERE job_id = ?1
@ -771,6 +783,30 @@ func (q *Queries) SaveJobStorageInfo(ctx context.Context, arg SaveJobStorageInfo
return err
}
const setLastRendered = `-- name: SetLastRendered :exec
INSERT INTO last_rendereds (id, created_at, updated_at, job_id)
VALUES (1, ?1, ?2, ?3)
ON CONFLICT DO UPDATE
SET updated_at=?2, job_id=?3
WHERE id=1
`
type SetLastRenderedParams struct {
CreatedAt time.Time
UpdatedAt sql.NullTime
JobID int64
}
// Set the 'last rendered' job info.
//
// Note that the use of ?2 and ?3 in the SQL is not desirable, and should be
// replaced with @updated_at and @job_id as soon as sqlc issue #3334 is fixed.
// See https://github.com/sqlc-dev/sqlc/issues/3334 for more info.
func (q *Queries) SetLastRendered(ctx context.Context, arg SetLastRenderedParams) error {
_, err := q.db.ExecContext(ctx, setLastRendered, arg.CreatedAt, arg.UpdatedAt, arg.JobID)
return err
}
const taskAssignToWorker = `-- name: TaskAssignToWorker :exec
UPDATE tasks SET
updated_at = ?1,

@ -80,7 +80,6 @@ type CreateWorkerParams struct {
}
// Worker queries
//
func (q *Queries) CreateWorker(ctx context.Context, arg CreateWorkerParams) (int64, error) {
row := q.db.QueryRowContext(ctx, createWorker,
arg.CreatedAt,