WIP: convert GORM to sqlc, for jobs/tasks #104304
@ -518,13 +518,55 @@ func (db *DB) FetchTaskJobUUID(ctx context.Context, taskUUID string) (string, er
|
||||
return jobUUID.String, nil
|
||||
}
|
||||
|
||||
// SaveTask updates a task that already exists in the database.
|
||||
// This function is not used by the Flamenco API, only by unit tests.
|
||||
func (db *DB) SaveTask(ctx context.Context, t *Task) error {
|
||||
tx := db.gormDB.WithContext(ctx).
|
||||
Omit("job").
|
||||
Omit("worker").
|
||||
Save(t)
|
||||
if tx.Error != nil {
|
||||
return taskError(tx.Error, "saving task")
|
||||
if t.ID == 0 {
|
||||
panic(fmt.Errorf("cannot use this function to insert a task"))
|
||||
}
|
||||
|
||||
queries, err := db.queries()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commandsJSON, err := json.Marshal(t.Commands)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot convert commands to JSON: %w", err)
|
||||
}
|
||||
|
||||
param := sqlc.UpdateTaskParams{
|
||||
UpdatedAt: db.now(),
|
||||
Name: t.Name,
|
||||
Type: t.Type,
|
||||
Priority: int64(t.Priority),
|
||||
Status: string(t.Status),
|
||||
Commands: commandsJSON,
|
||||
Activity: t.Activity,
|
||||
ID: int64(t.ID),
|
||||
}
|
||||
if t.WorkerID != nil {
|
||||
param.WorkerID = sql.NullInt64{
|
||||
Int64: int64(*t.WorkerID),
|
||||
Valid: true,
|
||||
}
|
||||
} else if t.Worker != nil && t.Worker.ID > 0 {
|
||||
param.WorkerID = sql.NullInt64{
|
||||
Int64: int64(t.Worker.ID),
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
if !t.LastTouchedAt.IsZero() {
|
||||
param.LastTouchedAt = sql.NullTime{
|
||||
Time: t.LastTouchedAt,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
err = queries.UpdateTask(ctx, param)
|
||||
if err != nil {
|
||||
return taskError(err, "updating task")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -74,3 +74,17 @@ SELECT jobs.UUID as jobUUID
|
||||
FROM tasks
|
||||
LEFT JOIN jobs ON (tasks.job_id = jobs.id)
|
||||
WHERE tasks.uuid = @uuid;
|
||||
|
||||
-- name: UpdateTask :exec
|
||||
-- Update a Task, except its id, created_at, uuid, or job_id fields.
|
||||
UPDATE tasks SET
|
||||
updated_at = @updated_at,
|
||||
name = @name,
|
||||
type = @type,
|
||||
priority = @priority,
|
||||
status = @status,
|
||||
worker_id = @worker_id,
|
||||
last_touched_at = @last_touched_at,
|
||||
commands = @commands,
|
||||
activity = @activity
|
||||
WHERE id=@id;
|
||||
|
@ -379,3 +379,47 @@ func (q *Queries) SaveJobStorageInfo(ctx context.Context, arg SaveJobStorageInfo
|
||||
_, err := q.db.ExecContext(ctx, saveJobStorageInfo, arg.StorageShamanCheckoutID, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateTask = `-- name: UpdateTask :exec
|
||||
UPDATE tasks SET
|
||||
updated_at = ?1,
|
||||
name = ?2,
|
||||
type = ?3,
|
||||
priority = ?4,
|
||||
status = ?5,
|
||||
worker_id = ?6,
|
||||
last_touched_at = ?7,
|
||||
commands = ?8,
|
||||
activity = ?9
|
||||
WHERE id=?10
|
||||
`
|
||||
|
||||
type UpdateTaskParams struct {
|
||||
UpdatedAt sql.NullTime
|
||||
Name string
|
||||
Type string
|
||||
Priority int64
|
||||
Status string
|
||||
WorkerID sql.NullInt64
|
||||
LastTouchedAt sql.NullTime
|
||||
Commands json.RawMessage
|
||||
Activity string
|
||||
ID int64
|
||||
}
|
||||
|
||||
// Update a Task, except its id, created_at, uuid, or job_id fields.
|
||||
func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateTask,
|
||||
arg.UpdatedAt,
|
||||
arg.Name,
|
||||
arg.Type,
|
||||
arg.Priority,
|
||||
arg.Status,
|
||||
arg.WorkerID,
|
||||
arg.LastTouchedAt,
|
||||
arg.Commands,
|
||||
arg.Activity,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user