Skip to content

Commit

Permalink
use correct types when querying db
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Mar 13, 2024
1 parent 11cb32c commit 832774d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
16 changes: 4 additions & 12 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (d *DAL) UpsertRunner(ctx context.Context, runner Runner) error {
return fmt.Errorf("%s: %w", "failed to JSON encode runner labels", err)
}
deploymentID, err := d.db.UpsertRunner(ctx, sql.UpsertRunnerParams{
Key: dbKeyFromRunnerKey(runner.Key),
Key: runner.Key,
Endpoint: runner.Endpoint,
State: sql.RunnerState(runner.State),
DeploymentName: pgDeploymentName,
Expand All @@ -514,9 +514,6 @@ func (d *DAL) UpsertRunner(ctx context.Context, runner Runner) error {
if err != nil {
return translatePGError(err)
}
if err != nil {
return translatePGError(err)
}
if runner.Deployment.Ok() && !deploymentID.Ok() {
return fmt.Errorf("deployment %s not found", runner.Deployment)
}
Expand All @@ -537,7 +534,7 @@ func (d *DAL) KillStaleControllers(ctx context.Context, age time.Duration) (int6

// DeregisterRunner deregisters the given runner.
func (d *DAL) DeregisterRunner(ctx context.Context, key model.RunnerKey) error {
count, err := d.db.DeregisterRunner(ctx, dbKeyFromRunnerKey(key))
count, err := d.db.DeregisterRunner(ctx, key)
if err != nil {
return translatePGError(err)
}
Expand Down Expand Up @@ -854,15 +851,15 @@ func (d *DAL) GetRoutingTable(ctx context.Context, modules []string) (map[string
}

func (d *DAL) GetRunnerState(ctx context.Context, runnerKey model.RunnerKey) (RunnerState, error) {
state, err := d.db.GetRunnerState(ctx, dbKeyFromRunnerKey(runnerKey))
state, err := d.db.GetRunnerState(ctx, runnerKey)
if err != nil {
return "", translatePGError(err)
}
return RunnerState(state), nil
}

func (d *DAL) GetRunner(ctx context.Context, runnerKey model.RunnerKey) (Runner, error) {
row, err := d.db.GetRunner(ctx, dbKeyFromRunnerKey(runnerKey))
row, err := d.db.GetRunner(ctx, runnerKey)
if err != nil {
return Runner{}, translatePGError(err)
}
Expand Down Expand Up @@ -1049,8 +1046,3 @@ func translatePGError(err error) error {
}
return err
}

func dbKeyFromRunnerKey(key model.RunnerKey) string {
value, _ := key.Value()
return value.(string)
}
8 changes: 4 additions & 4 deletions backend/controller/sql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions backend/controller/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ WITH matches AS (
UPDATE runners
SET state = 'dead',
deployment_id = NULL
WHERE key = $1
WHERE key = sqlc.arg('key')::runner_key
RETURNING 1)
SELECT COUNT(*)
FROM matches;
Expand Down Expand Up @@ -222,7 +222,7 @@ RETURNING runners.*;
-- name: GetRunnerState :one
SELECT state
FROM runners
WHERE key = $1;
WHERE key = sqlc.arg('key')::runner_key;

-- name: GetRunner :one
SELECT DISTINCT ON (r.key) r.key AS runner_key,
Expand All @@ -236,7 +236,7 @@ SELECT DISTINCT ON (r.key) r.key AS runner_key
THEN d.name END, NULL) AS deployment_name
FROM runners r
LEFT JOIN deployments d on d.id = r.deployment_id OR r.deployment_id IS NULL
WHERE r.key = $1;
WHERE r.key = sqlc.arg('key')::runner_key;

-- name: GetRoutingTable :many
SELECT endpoint, r.key AS runner_key, r.module_name, d.name deployment_name
Expand All @@ -251,7 +251,7 @@ WHERE state = 'assigned'
SELECT endpoint, r.key AS runner_key, r.module_name, d.name deployment_name, r.state
FROM runners r
LEFT JOIN deployments d on r.deployment_id = d.id
WHERE r.key = $1;
WHERE r.key = sqlc.arg('key')::runner_key;

-- name: GetRunnersForDeployment :many
SELECT *
Expand Down
18 changes: 9 additions & 9 deletions backend/controller/sql/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 832774d

Please sign in to comment.