WIP: convert GORM to sqlc, for jobs/tasks #104304
@ -664,16 +664,34 @@ func (db *DB) FetchTasksOfWorkerInStatus(ctx context.Context, worker *Worker, ta
|
||||
}
|
||||
|
||||
func (db *DB) FetchTasksOfWorkerInStatusOfJob(ctx context.Context, worker *Worker, taskStatus api.TaskStatus, job *Job) ([]*Task, error) {
|
||||
result := []*Task{}
|
||||
tx := db.gormDB.WithContext(ctx).
|
||||
Model(&Task{}).
|
||||
Joins("Job").
|
||||
Where("tasks.worker_id = ?", worker.ID).
|
||||
Where("tasks.status = ?", taskStatus).
|
||||
Where("job.id = ?", job.ID).
|
||||
Scan(&result)
|
||||
if tx.Error != nil {
|
||||
return nil, taskError(tx.Error, "finding tasks of worker %s in status %q and job %s", worker.UUID, taskStatus, job.UUID)
|
||||
queries, err := db.queries()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := queries.FetchTasksOfWorkerInStatusOfJob(ctx, sqlc.FetchTasksOfWorkerInStatusOfJobParams{
|
||||
WorkerID: sql.NullInt64{
|
||||
Int64: int64(worker.ID),
|
||||
Valid: true,
|
||||
},
|
||||
JobID: int64(job.ID),
|
||||
TaskStatus: string(taskStatus),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, taskError(err, "finding tasks of worker %s in status %q and job %s", worker.UUID, taskStatus, job.UUID)
|
||||
}
|
||||
|
||||
result := make([]*Task, len(rows))
|
||||
for i := range rows {
|
||||
gormTask, err := convertSqlcTask(rows[i].Task, job.UUID, worker.UUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gormTask.Job = job
|
||||
gormTask.JobID = job.ID
|
||||
gormTask.Worker = worker
|
||||
gormTask.WorkerID = &worker.ID
|
||||
result[i] = gormTask
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
@ -76,6 +76,13 @@ LEFT JOIN jobs ON (tasks.job_id = jobs.id)
|
||||
WHERE tasks.worker_id = @worker_id
|
||||
AND tasks.status = @task_status;
|
||||
|
||||
-- name: FetchTasksOfWorkerInStatusOfJob :many
|
||||
SELECT sqlc.embed(tasks)
|
||||
FROM tasks
|
||||
WHERE tasks.worker_id = @worker_id
|
||||
AND tasks.job_id = @job_id
|
||||
AND tasks.status = @task_status;
|
||||
|
||||
-- name: FetchTaskJobUUID :one
|
||||
SELECT jobs.UUID as jobUUID
|
||||
FROM tasks
|
||||
|
@ -341,6 +341,61 @@ func (q *Queries) FetchTasksOfWorkerInStatus(ctx context.Context, arg FetchTasks
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const fetchTasksOfWorkerInStatusOfJob = `-- name: FetchTasksOfWorkerInStatusOfJob :many
|
||||
SELECT tasks.id, tasks.created_at, tasks.updated_at, tasks.uuid, tasks.name, tasks.type, tasks.job_id, tasks.priority, tasks.status, tasks.worker_id, tasks.last_touched_at, tasks.commands, tasks.activity
|
||||
FROM tasks
|
||||
WHERE tasks.worker_id = ?1
|
||||
AND tasks.job_id = ?2
|
||||
AND tasks.status = ?3
|
||||
`
|
||||
|
||||
type FetchTasksOfWorkerInStatusOfJobParams struct {
|
||||
WorkerID sql.NullInt64
|
||||
JobID int64
|
||||
TaskStatus string
|
||||
}
|
||||
|
||||
type FetchTasksOfWorkerInStatusOfJobRow struct {
|
||||
Task Task
|
||||
}
|
||||
|
||||
func (q *Queries) FetchTasksOfWorkerInStatusOfJob(ctx context.Context, arg FetchTasksOfWorkerInStatusOfJobParams) ([]FetchTasksOfWorkerInStatusOfJobRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, fetchTasksOfWorkerInStatusOfJob, arg.WorkerID, arg.JobID, arg.TaskStatus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []FetchTasksOfWorkerInStatusOfJobRow
|
||||
for rows.Next() {
|
||||
var i FetchTasksOfWorkerInStatusOfJobRow
|
||||
if err := rows.Scan(
|
||||
&i.Task.ID,
|
||||
&i.Task.CreatedAt,
|
||||
&i.Task.UpdatedAt,
|
||||
&i.Task.UUID,
|
||||
&i.Task.Name,
|
||||
&i.Task.Type,
|
||||
&i.Task.JobID,
|
||||
&i.Task.Priority,
|
||||
&i.Task.Status,
|
||||
&i.Task.WorkerID,
|
||||
&i.Task.LastTouchedAt,
|
||||
&i.Task.Commands,
|
||||
&i.Task.Activity,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const requestJobDeletion = `-- name: RequestJobDeletion :exec
|
||||
UPDATE jobs SET
|
||||
updated_at = ?1,
|
||||
|
Loading…
Reference in New Issue
Block a user