Sybren A. Stüvel 96cc8255e7 Manager: simplify use of sqlc
Instead of returning an error when getting the sqlc queries object, just
panic. This'll make the calling code quite a bit simpler. The situation
in which it might error out is so rare that I've never seen it, and I
don't even know if it will ever be possible to happen with the SQLite
implementation we use now. Furthermore, once we get rid of GORM, it
should just always work anyway.

Ref: #104305
2024-09-18 10:17:20 +02:00

47 lines
1.3 KiB
Go

package persistence
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"context"
"database/sql"
"errors"
"projects.blender.org/studio/flamenco/internal/manager/persistence/sqlc"
)
// LastRendered only has one entry in its database table, to indicate the job
// that was the last to receive a "last rendered image" from a Worker.
// This is used to show the global last-rendered image in the web interface.
type LastRendered struct {
Model
JobID uint `gorm:"default:0"`
Job *Job `gorm:"foreignkey:JobID;references:ID;constraint:OnDelete:CASCADE"`
}
// SetLastRendered sets this job as the one with the most recent rendered image.
func (db *DB) SetLastRendered(ctx context.Context, j *Job) error {
queries := db.queries()
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) {
queries := db.queries()
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
}