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 return convertedTask, nil
} }
// FetchTaskJobUUID fetches the job UUID of the given task.
func (db *DB) FetchTaskJobUUID(ctx context.Context, taskUUID string) (string, error) { func (db *DB) FetchTaskJobUUID(ctx context.Context, taskUUID string) (string, error) {
queries, err := db.queries() queries, err := db.queries()
if err != nil { if err != nil {

View File

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

View File

@ -7,7 +7,6 @@ package sqlc
import ( import (
"context" "context"
"database/sql"
) )
const fetchWorker = `-- name: FetchWorker :one 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 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 SELECT worker_tags.id, worker_tags.created_at, worker_tags.updated_at, worker_tags.uuid, worker_tags.name, worker_tags.description
FROM workers FROM worker_tags
LEFT JOIN worker_tag_membership memb ON (memb.worker_id = workers.id) LEFT JOIN worker_tag_membership m ON (m.worker_tag_id = worker_tags.id)
LEFT JOIN worker_tags ON (memb.worker_tag_id = worker_tags.id) LEFT JOIN workers on (m.worker_id = workers.id)
WHERE workers.uuid = ?1 WHERE workers.uuid = ?1
` `
type FetchWorkerTagsRow struct { func (q *Queries) FetchWorkerTags(ctx context.Context, uuid string) ([]WorkerTag, error) {
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) {
rows, err := q.db.QueryContext(ctx, fetchWorkerTags, uuid) rows, err := q.db.QueryContext(ctx, fetchWorkerTags, uuid)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var items []FetchWorkerTagsRow var items []WorkerTag
for rows.Next() { for rows.Next() {
var i FetchWorkerTagsRow var i WorkerTag
if err := rows.Scan( if err := rows.Scan(
&i.ID, &i.ID,
&i.CreatedAt, &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 // Change this to a filename if you want to run a single test and inspect the
// resulting database. // resulting database.
// const TestDSN = "file::memory:" const TestDSN = "file::memory:"
const TestDSN = "C:/workspace/flamenco/unittest.sqlite"
func CreateTestDB(t *testing.T) (db *DB, closer func()) { func CreateTestDB(t *testing.T) (db *DB, closer func()) {
// Delete the SQLite file if it exists on disk. // 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 := convertSqlcWorker(worker)
convertedWorker.Tags = make([]*WorkerTag, len(workerTags)) convertedWorker.Tags = make([]*WorkerTag, len(workerTags))
for index := range workerTags { for index := range workerTags {
tag := workerTags[index].WorkerTag convertedTag := convertSqlcWorkerTag(workerTags[index])
convertedTag := convertSqlcWorkerTag(tag)
convertedWorker.Tags[index] = &convertedTag convertedWorker.Tags[index] = &convertedTag
} }