WIP: convert GORM to sqlc, for jobs/tasks #104304
@ -466,7 +466,25 @@ func (db *DB) FetchTask(ctx context.Context, taskUUID string) (*Task, error) {
|
||||
return nil, taskError(err, "fetching task %s", taskUUID)
|
||||
}
|
||||
|
||||
return convertSqlcTask(taskRow)
|
||||
convertedTask, err := convertSqlcTask(taskRow)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: remove this code, and let the caller fetch the Worker explicitly when needed.
|
||||
if taskRow.WorkerUUID.Valid {
|
||||
worker, err := queries.FetchWorkerUnconditional(ctx, taskRow.WorkerUUID.String)
|
||||
if err != nil {
|
||||
return nil, taskError(err, "fetching worker assigned to task %s", taskUUID)
|
||||
}
|
||||
convertedWorker, err := convertSqlcWorker(worker)
|
||||
if err != nil {
|
||||
return nil, taskError(err, "converting worker assigned to task %s", taskUUID)
|
||||
}
|
||||
convertedTask.Worker = convertedWorker
|
||||
}
|
||||
|
||||
return convertedTask, nil
|
||||
}
|
||||
|
||||
func (db *DB) FetchTaskJobUUID(ctx context.Context, taskUUID string) (string, error) {
|
||||
|
11
internal/manager/persistence/sqlc/query_workers.sql
Normal file
11
internal/manager/persistence/sqlc/query_workers.sql
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
-- Worker queries
|
||||
--
|
||||
|
||||
-- name: FetchWorker :one
|
||||
-- FetchWorker only returns the worker if it wasn't soft-deleted.
|
||||
SELECT * FROM workers WHERE workers.uuid = @uuid and deleted_at is not NULL;
|
||||
|
||||
-- name: FetchWorkerUnconditional :one
|
||||
-- FetchWorkerUnconditional ignores soft-deletion status and just returns the worker.
|
||||
SELECT * FROM workers WHERE workers.uuid = @uuid;
|
71
internal/manager/persistence/sqlc/query_workers.sql.go
Normal file
71
internal/manager/persistence/sqlc/query_workers.sql.go
Normal file
@ -0,0 +1,71 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.25.0
|
||||
// source: query_workers.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const fetchWorker = `-- name: FetchWorker :one
|
||||
|
||||
SELECT id, created_at, updated_at, uuid, secret, name, address, platform, software, status, last_seen_at, status_requested, lazy_status_request, supported_task_types, deleted_at, can_restart FROM workers WHERE workers.uuid = ?1 and deleted_at is not NULL
|
||||
`
|
||||
|
||||
// Worker queries
|
||||
//
|
||||
// FetchWorker only returns the worker if it wasn't soft-deleted.
|
||||
func (q *Queries) FetchWorker(ctx context.Context, uuid string) (Worker, error) {
|
||||
row := q.db.QueryRowContext(ctx, fetchWorker, uuid)
|
||||
var i Worker
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.UUID,
|
||||
&i.Secret,
|
||||
&i.Name,
|
||||
&i.Address,
|
||||
&i.Platform,
|
||||
&i.Software,
|
||||
&i.Status,
|
||||
&i.LastSeenAt,
|
||||
&i.StatusRequested,
|
||||
&i.LazyStatusRequest,
|
||||
&i.SupportedTaskTypes,
|
||||
&i.DeletedAt,
|
||||
&i.CanRestart,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const fetchWorkerUnconditional = `-- name: FetchWorkerUnconditional :one
|
||||
SELECT id, created_at, updated_at, uuid, secret, name, address, platform, software, status, last_seen_at, status_requested, lazy_status_request, supported_task_types, deleted_at, can_restart FROM workers WHERE workers.uuid = ?1
|
||||
`
|
||||
|
||||
// FetchWorkerUnconditional ignores soft-deletion status and just returns the worker.
|
||||
func (q *Queries) FetchWorkerUnconditional(ctx context.Context, uuid string) (Worker, error) {
|
||||
row := q.db.QueryRowContext(ctx, fetchWorkerUnconditional, uuid)
|
||||
var i Worker
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.UUID,
|
||||
&i.Secret,
|
||||
&i.Name,
|
||||
&i.Address,
|
||||
&i.Platform,
|
||||
&i.Software,
|
||||
&i.Status,
|
||||
&i.LastSeenAt,
|
||||
&i.StatusRequested,
|
||||
&i.LazyStatusRequest,
|
||||
&i.SupportedTaskTypes,
|
||||
&i.DeletedAt,
|
||||
&i.CanRestart,
|
||||
)
|
||||
return i, err
|
||||
}
|
@ -74,18 +74,17 @@ func (db *DB) CreateWorker(ctx context.Context, w *Worker) error {
|
||||
}
|
||||
|
||||
func (db *DB) FetchWorker(ctx context.Context, uuid string) (*Worker, error) {
|
||||
w := Worker{}
|
||||
tx := db.gormDB.WithContext(ctx).
|
||||
Preload("Tags").
|
||||
Find(&w, "uuid = ?", uuid).
|
||||
Limit(1)
|
||||
if tx.Error != nil {
|
||||
return nil, workerError(tx.Error, "fetching worker")
|
||||
queries, err := db.queries()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if w.ID == 0 {
|
||||
return nil, ErrWorkerNotFound
|
||||
|
||||
worker, err := queries.FetchWorker(ctx, uuid)
|
||||
if err != nil {
|
||||
return nil, workerError(err, "fetching worker %s", uuid)
|
||||
}
|
||||
return &w, nil
|
||||
|
||||
return convertSqlcWorker(worker)
|
||||
}
|
||||
|
||||
func (db *DB) DeleteWorker(ctx context.Context, uuid string) error {
|
||||
|
18
sqlc.yaml
18
sqlc.yaml
@ -15,4 +15,22 @@ sql:
|
||||
uuid: "UUID"
|
||||
uuids: "UUIDs"
|
||||
jobuuid: "JobUUID"
|
||||
taskUUID: "TaskUUID"
|
||||
workeruuid: "WorkerUUID"
|
||||
- engine: "sqlite"
|
||||
schema: "internal/manager/persistence/sqlc/schema.sql"
|
||||
queries: "internal/manager/persistence/sqlc/query_workers.sql"
|
||||
gen:
|
||||
go:
|
||||
out: "internal/manager/persistence/sqlc"
|
||||
overrides:
|
||||
- db_type: "jsonb"
|
||||
go_type:
|
||||
import: "encoding/json"
|
||||
type: "RawMessage"
|
||||
rename:
|
||||
uuid: "UUID"
|
||||
uuids: "UUIDs"
|
||||
jobuuid: "JobUUID"
|
||||
taskUUID: "TaskUUID"
|
||||
workeruuid: "WorkerUUID"
|
||||
|
Loading…
Reference in New Issue
Block a user