WIP: convert GORM to sqlc, for jobs/tasks #104304

Closed
Sybren A. Stüvel wants to merge 27 commits from sqlc-task into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
5 changed files with 12 additions and 23 deletions
Showing only changes of commit 7769f99bcb - Show all commits

View File

@ -484,6 +484,7 @@ func (db *DB) FetchTask(ctx context.Context, taskUUID string) (*Task, error) {
return convertedTask, nil
}
// FetchTaskJobUUID fetches the job UUID of the given task.
func (db *DB) FetchTaskJobUUID(ctx context.Context, taskUUID string) (string, error) {
queries, err := db.queries()
if err != nil {

View File

@ -12,7 +12,7 @@ SELECT * FROM workers WHERE workers.uuid = @uuid;
-- name: FetchWorkerTags :many
SELECT worker_tags.*
FROM workers
LEFT JOIN worker_tag_membership memb ON (memb.worker_id = workers.id)
LEFT JOIN worker_tags ON (memb.worker_tag_id = worker_tags.id)
FROM worker_tags
LEFT JOIN worker_tag_membership m ON (m.worker_tag_id = worker_tags.id)
LEFT JOIN workers on (m.worker_id = workers.id)
WHERE workers.uuid = @uuid;

View File

@ -7,7 +7,6 @@ package sqlc
import (
"context"
"database/sql"
)
const fetchWorker = `-- name: FetchWorker :one
@ -44,30 +43,21 @@ func (q *Queries) FetchWorker(ctx context.Context, uuid string) (Worker, error)
const fetchWorkerTags = `-- name: FetchWorkerTags :many
SELECT worker_tags.id, worker_tags.created_at, worker_tags.updated_at, worker_tags.uuid, worker_tags.name, worker_tags.description
FROM workers
LEFT JOIN worker_tag_membership memb ON (memb.worker_id = workers.id)
LEFT JOIN worker_tags ON (memb.worker_tag_id = worker_tags.id)
FROM worker_tags
LEFT JOIN worker_tag_membership m ON (m.worker_tag_id = worker_tags.id)
LEFT JOIN workers on (m.worker_id = workers.id)
WHERE workers.uuid = ?1
`
type FetchWorkerTagsRow struct {
ID sql.NullInt64
CreatedAt sql.NullTime
UpdatedAt sql.NullTime
UUID sql.NullString
Name sql.NullString
Description sql.NullString
}
func (q *Queries) FetchWorkerTags(ctx context.Context, uuid string) ([]FetchWorkerTagsRow, error) {
func (q *Queries) FetchWorkerTags(ctx context.Context, uuid string) ([]WorkerTag, error) {
rows, err := q.db.QueryContext(ctx, fetchWorkerTags, uuid)
if err != nil {
return nil, err
}
defer rows.Close()
var items []FetchWorkerTagsRow
var items []WorkerTag
for rows.Next() {
var i FetchWorkerTagsRow
var i WorkerTag
if err := rows.Scan(
&i.ID,
&i.CreatedAt,

View File

@ -20,8 +20,7 @@ import (
// Change this to a filename if you want to run a single test and inspect the
// resulting database.
// const TestDSN = "file::memory:"
const TestDSN = "C:/workspace/flamenco/unittest.sqlite"
const TestDSN = "file::memory:"
func CreateTestDB(t *testing.T) (db *DB, closer func()) {
// Delete the SQLite file if it exists on disk.

View File

@ -93,8 +93,7 @@ func (db *DB) FetchWorker(ctx context.Context, uuid string) (*Worker, error) {
convertedWorker := convertSqlcWorker(worker)
convertedWorker.Tags = make([]*WorkerTag, len(workerTags))
for index := range workerTags {
tag := workerTags[index].WorkerTag
convertedTag := convertSqlcWorkerTag(tag)
convertedTag := convertSqlcWorkerTag(workerTags[index])
convertedWorker.Tags[index] = &convertedTag
}