From ddd6890a147460a4330dfc42a1b1c8d885772271 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Wed, 13 Mar 2024 14:32:50 +1100 Subject: [PATCH] cast sqlc queries to deployment_name --- backend/controller/dal/dal.go | 18 +++++++++--------- backend/controller/sql/queries.sql | 14 +++++++------- backend/controller/sql/queries.sql.go | 23 +++++++++++------------ 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/backend/controller/dal/dal.go b/backend/controller/dal/dal.go index 03813447ad..8dae530ab6 100644 --- a/backend/controller/dal/dal.go +++ b/backend/controller/dal/dal.go @@ -499,9 +499,9 @@ func (d *DAL) GetDeployment(ctx context.Context, name model.DeploymentName) (*mo // ErrConflict will be returned if a runner with the same endpoint and a // different key already exists. func (d *DAL) UpsertRunner(ctx context.Context, runner Runner) error { - var pgDeploymentName optional.Option[string] - if dkey, ok := runner.Deployment.Get(); ok { - pgDeploymentName = optional.Some(dkey.String()) + var pgDeploymentName optional.Option[model.DeploymentName] + if dname, ok := runner.Deployment.Get(); ok { + pgDeploymentName = optional.Some(dname) } attrBytes, err := json.Marshal(runner.Labels) if err != nil { @@ -612,7 +612,7 @@ func (p *postgresClaim) Rollback(ctx context.Context) error { func (p *postgresClaim) Runner() Runner { return p.runner } // SetDeploymentReplicas activates the given deployment. -func (d *DAL) SetDeploymentReplicas(ctx context.Context, key model.DeploymentName, minReplicas int) error { +func (d *DAL) SetDeploymentReplicas(ctx context.Context, name model.DeploymentName, minReplicas int) error { // Start the transaction tx, err := d.db.Begin(ctx) if err != nil { @@ -621,18 +621,18 @@ func (d *DAL) SetDeploymentReplicas(ctx context.Context, key model.DeploymentNam defer tx.CommitOrRollback(ctx, &err) - deployment, err := d.db.GetDeployment(ctx, key) + deployment, err := d.db.GetDeployment(ctx, name) if err != nil { return translatePGError(err) } - err = d.db.SetDeploymentDesiredReplicas(ctx, key, int32(minReplicas)) + err = d.db.SetDeploymentDesiredReplicas(ctx, name, int32(minReplicas)) if err != nil { return translatePGError(err) } err = tx.InsertDeploymentUpdatedEvent(ctx, sql.InsertDeploymentUpdatedEventParams{ - DeploymentName: string(key), + DeploymentName: name, MinReplicas: int32(minReplicas), PrevMinReplicas: deployment.MinReplicas, }) @@ -681,7 +681,7 @@ func (d *DAL) ReplaceDeployment(ctx context.Context, newDeploymentName model.Dep } err = tx.InsertDeploymentCreatedEvent(ctx, sql.InsertDeploymentCreatedEventParams{ - DeploymentName: newDeploymentName.String(), + DeploymentName: newDeploymentName, Language: newDeployment.Language, ModuleName: newDeployment.ModuleName, MinReplicas: int32(minReplicas), @@ -958,7 +958,7 @@ func (d *DAL) InsertCallEvent(ctx context.Context, call *CallEvent) error { requestName = optional.Some(string(rn)) } return translatePGError(d.db.InsertCallEvent(ctx, sql.InsertCallEventParams{ - DeploymentName: call.DeploymentName.String(), + DeploymentName: call.DeploymentName, RequestName: requestName, TimeStamp: call.Time, SourceModule: sourceModule, diff --git a/backend/controller/sql/queries.sql b/backend/controller/sql/queries.sql index f88d80de00..65dae70d66 100644 --- a/backend/controller/sql/queries.sql +++ b/backend/controller/sql/queries.sql @@ -86,11 +86,11 @@ WITH deployment_rel AS ( -- there is no corresponding deployment, then the deployment ID is -1 -- and the parent statement will fail due to a foreign key constraint. SELECT CASE - WHEN sqlc.narg('deployment_name')::TEXT IS NULL + WHEN sqlc.narg('deployment_name')::deployment_name IS NULL THEN NULL ELSE COALESCE((SELECT id FROM deployments d - WHERE d.name = sqlc.narg('deployment_name') + WHERE d.name = sqlc.narg('deployment_name')::deployment_name LIMIT 1), -1) END AS id) INSERT INTO runners (key, endpoint, state, labels, deployment_id, last_seen) @@ -210,7 +210,7 @@ SET state = 'reserved', -- and the update will fail due to a FK constraint. deployment_id = COALESCE((SELECT id FROM deployments d - WHERE d.name = sqlc.arg('deployment_name') + WHERE d.name = sqlc.arg('deployment_name')::deployment_name LIMIT 1), -1) WHERE id = (SELECT id FROM runners r @@ -274,7 +274,7 @@ FROM rows; -- name: InsertLogEvent :exec INSERT INTO events (deployment_id, request_id, time_stamp, custom_key_1, type, payload) -VALUES ((SELECT id FROM deployments d WHERE d.name = sqlc.arg('deployment_name') LIMIT 1), +VALUES ((SELECT id FROM deployments d WHERE d.name = sqlc.arg('deployment_name')::deployment_name LIMIT 1), (CASE WHEN sqlc.narg('request_name')::TEXT IS NULL THEN NULL ELSE (SELECT id FROM requests ir WHERE ir.name = sqlc.narg('request_name')::TEXT LIMIT 1) @@ -293,7 +293,7 @@ VALUES ((SELECT id FROM deployments d WHERE d.name = sqlc.arg('deployment_name') INSERT INTO events (deployment_id, type, custom_key_1, custom_key_2, payload) VALUES ((SELECT id FROM deployments - WHERE deployments.name = sqlc.arg('deployment_name')::TEXT), + WHERE deployments.name = sqlc.arg('deployment_name')::deployment_name), 'deployment_created', sqlc.arg('language')::TEXT, sqlc.arg('module_name')::TEXT, @@ -306,7 +306,7 @@ VALUES ((SELECT id INSERT INTO events (deployment_id, type, custom_key_1, custom_key_2, payload) VALUES ((SELECT id FROM deployments - WHERE deployments.name = sqlc.arg('deployment_name')::TEXT), + WHERE deployments.name = sqlc.arg('deployment_name')::deployment_name), 'deployment_updated', sqlc.arg('language')::TEXT, sqlc.arg('module_name')::TEXT, @@ -318,7 +318,7 @@ VALUES ((SELECT id -- name: InsertCallEvent :exec INSERT INTO events (deployment_id, request_id, time_stamp, type, custom_key_1, custom_key_2, custom_key_3, custom_key_4, payload) -VALUES ((SELECT id FROM deployments WHERE deployments.name = sqlc.arg('deployment_name')::TEXT), +VALUES ((SELECT id FROM deployments WHERE deployments.name = sqlc.arg('deployment_name')::deployment_name), (CASE WHEN sqlc.narg('request_name')::TEXT IS NULL THEN NULL ELSE (SELECT id FROM requests ir WHERE ir.name = sqlc.narg('request_name')::TEXT) diff --git a/backend/controller/sql/queries.sql.go b/backend/controller/sql/queries.sql.go index 922cc128e7..1d4d76e502 100644 --- a/backend/controller/sql/queries.sql.go +++ b/backend/controller/sql/queries.sql.go @@ -107,7 +107,6 @@ FROM matches ` func (q *Queries) DeregisterRunner(ctx context.Context, key model.RunnerKey) (int64, error) { - row := q.db.QueryRow(ctx, deregisterRunner, key) var count int64 err := row.Scan(&count) @@ -984,7 +983,7 @@ func (q *Queries) GetRunnersForDeployment(ctx context.Context, name model.Deploy const insertCallEvent = `-- name: InsertCallEvent :exec INSERT INTO events (deployment_id, request_id, time_stamp, type, custom_key_1, custom_key_2, custom_key_3, custom_key_4, payload) -VALUES ((SELECT id FROM deployments WHERE deployments.name = $1::TEXT), +VALUES ((SELECT id FROM deployments WHERE deployments.name = $1::deployment_name), (CASE WHEN $2::TEXT IS NULL THEN NULL ELSE (SELECT id FROM requests ir WHERE ir.name = $2::TEXT) @@ -1005,7 +1004,7 @@ VALUES ((SELECT id FROM deployments WHERE deployments.name = $1::TEXT), ` type InsertCallEventParams struct { - DeploymentName string + DeploymentName model.DeploymentName RequestName optional.Option[string] TimeStamp time.Time SourceModule optional.Option[string] @@ -1041,7 +1040,7 @@ const insertDeploymentCreatedEvent = `-- name: InsertDeploymentCreatedEvent :exe INSERT INTO events (deployment_id, type, custom_key_1, custom_key_2, payload) VALUES ((SELECT id FROM deployments - WHERE deployments.name = $1::TEXT), + WHERE deployments.name = $1::deployment_name), 'deployment_created', $2::TEXT, $3::TEXT, @@ -1052,7 +1051,7 @@ VALUES ((SELECT id ` type InsertDeploymentCreatedEventParams struct { - DeploymentName string + DeploymentName model.DeploymentName Language string ModuleName string MinReplicas int32 @@ -1074,7 +1073,7 @@ const insertDeploymentUpdatedEvent = `-- name: InsertDeploymentUpdatedEvent :exe INSERT INTO events (deployment_id, type, custom_key_1, custom_key_2, payload) VALUES ((SELECT id FROM deployments - WHERE deployments.name = $1::TEXT), + WHERE deployments.name = $1::deployment_name), 'deployment_updated', $2::TEXT, $3::TEXT, @@ -1085,7 +1084,7 @@ VALUES ((SELECT id ` type InsertDeploymentUpdatedEventParams struct { - DeploymentName string + DeploymentName model.DeploymentName Language string ModuleName string PrevMinReplicas int32 @@ -1138,7 +1137,7 @@ func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) error const insertLogEvent = `-- name: InsertLogEvent :exec INSERT INTO events (deployment_id, request_id, time_stamp, custom_key_1, type, payload) -VALUES ((SELECT id FROM deployments d WHERE d.name = $1 LIMIT 1), +VALUES ((SELECT id FROM deployments d WHERE d.name = $1::deployment_name LIMIT 1), (CASE WHEN $2::TEXT IS NULL THEN NULL ELSE (SELECT id FROM requests ir WHERE ir.name = $2::TEXT LIMIT 1) @@ -1243,7 +1242,7 @@ SET state = 'reserved', -- and the update will fail due to a FK constraint. deployment_id = COALESCE((SELECT id FROM deployments d - WHERE d.name = $2 + WHERE d.name = $2::deployment_name LIMIT 1), -1) WHERE id = (SELECT id FROM runners r @@ -1317,11 +1316,11 @@ func (q *Queries) UpsertModule(ctx context.Context, language string, name string const upsertRunner = `-- name: UpsertRunner :one WITH deployment_rel AS ( SELECT CASE - WHEN $5::TEXT IS NULL + WHEN $5::deployment_name IS NULL THEN NULL ELSE COALESCE((SELECT id FROM deployments d - WHERE d.name = $5 + WHERE d.name = $5::deployment_name LIMIT 1), -1) END AS id) INSERT INTO runners (key, endpoint, state, labels, deployment_id, last_seen) @@ -1344,7 +1343,7 @@ type UpsertRunnerParams struct { Endpoint string State RunnerState Labels []byte - DeploymentName optional.Option[string] + DeploymentName NullDeploymentName } // Upsert a runner and return the deployment ID that it is assigned to, if any.