Skip to content

Commit

Permalink
Replace uses of tx.Exec with tx.ExecContext
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Stromberg <[email protected]>
  • Loading branch information
tstromberg committed Sep 30, 2021
1 parent 6a7f85d commit cd512b9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions db/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (d TinkDB) DeleteFromDB(ctx context.Context, id string) error {
return errors.Wrap(err, "BEGIN transaction")
}

res, err := tx.Exec(`
res, err := tx.ExecContext(ctx, `
UPDATE hardware
SET
deleted_at = NOW()
Expand Down Expand Up @@ -47,7 +47,7 @@ func (d TinkDB) InsertIntoDB(ctx context.Context, data string) error {
return errors.Wrap(err, "BEGIN transaction")
}

_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
INSERT INTO
hardware (inserted_at, id, data)
VALUES
Expand Down
10 changes: 5 additions & 5 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
if err != nil {
return errors.Wrap(err, "BEGIN transaction")
}
_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
INSERT INTO
template (created_at, updated_at, name, data, id)
VALUES
Expand Down Expand Up @@ -107,7 +107,7 @@ func (d TinkDB) DeleteTemplate(ctx context.Context, id string) error {
return errors.Wrap(err, "BEGIN transaction")
}

res, err := tx.Exec(`
res, err := tx.ExecContext(ctx, `
UPDATE template
SET
deleted_at = NOW()
Expand Down Expand Up @@ -183,11 +183,11 @@ func (d TinkDB) UpdateTemplate(ctx context.Context, name string, data string, id

switch {
case data == "" && name != "":
_, err = tx.Exec(`UPDATE template SET updated_at = NOW(), name = $2 WHERE id = $1;`, id, name)
_, err = tx.ExecContext(ctx, `UPDATE template SET updated_at = NOW(), name = $2 WHERE id = $1;`, id, name)
case data != "" && name == "":
_, err = tx.Exec(`UPDATE template SET updated_at = NOW(), data = $2 WHERE id = $1;`, id, data)
_, err = tx.ExecContext(ctx, `UPDATE template SET updated_at = NOW(), data = $2 WHERE id = $1;`, id, data)
default:
_, err = tx.Exec(`UPDATE template SET updated_at = NOW(), name = $2, data = $3 WHERE id = $1;`, id, name, data)
_, err = tx.ExecContext(ctx, `UPDATE template SET updated_at = NOW(), name = $2, data = $3 WHERE id = $1;`, id, name, data)
}

if err != nil {
Expand Down
34 changes: 17 additions & 17 deletions db/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d TinkDB) CreateWorkflow(ctx context.Context, wf Workflow, data string, id
if err != nil {
return errors.Wrap(err, "failed to create workflow")
}
err = insertInWorkflow(wf, tx)
err = insertInWorkflow(ctx, wf, tx)
if err != nil {
return errors.Wrap(err, "failed to create workflow")
}
Expand All @@ -55,8 +55,8 @@ func (d TinkDB) CreateWorkflow(ctx context.Context, wf Workflow, data string, id
return nil
}

func insertInWorkflow(wf Workflow, tx *sql.Tx) error {
_, err := tx.Exec(`
func insertInWorkflow(ctx context.Context, wf Workflow, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
INSERT INTO
workflow (created_at, updated_at, template, devices, id)
VALUES
Expand All @@ -72,8 +72,8 @@ func insertInWorkflow(wf Workflow, tx *sql.Tx) error {
return nil
}

func insertIntoWfWorkerTable(wfID uuid.UUID, workerID uuid.UUID, tx *sql.Tx) error {
_, err := tx.Exec(`
func insertIntoWfWorkerTable(ctx context.Context, wfID uuid.UUID, workerID uuid.UUID, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
INSERT INTO
workflow_worker_map (workflow_id, worker_id)
VALUES
Expand Down Expand Up @@ -116,7 +116,7 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid.
return err
}
if uniqueWorkerID != workerUID {
err = insertIntoWfWorkerTable(id, workerUID, tx)
err = insertIntoWfWorkerTable(ctx, id, workerUID, tx)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid.
return err
}

_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
INSERT INTO
workflow_state (workflow_id, current_worker, current_task_name, current_action_name, current_action_state, action_list, current_action_index, total_number_of_actions)
VALUES
Expand Down Expand Up @@ -203,7 +203,7 @@ func (d TinkDB) InsertIntoWfDataTable(ctx context.Context, req *pb.UpdateWorkflo
return errors.Wrap(err, "BEGIN transaction")
}

_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
INSERT INTO
workflow_data (workflow_id, version, metadata, data)
VALUES
Expand All @@ -215,7 +215,7 @@ func (d TinkDB) InsertIntoWfDataTable(ctx context.Context, req *pb.UpdateWorkflo

if version > int32(maxVersions) {
cleanVersion := version - int32(maxVersions)
_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
UPDATE workflow_data
SET
data = NULL
Expand Down Expand Up @@ -375,7 +375,7 @@ func (d TinkDB) DeleteWorkflow(ctx context.Context, id string, _ int32) error {
return errors.Wrap(err, "BEGIN transaction")
}

_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
DELETE FROM workflow_worker_map
WHERE
workflow_id = $1;
Expand All @@ -384,7 +384,7 @@ func (d TinkDB) DeleteWorkflow(ctx context.Context, id string, _ int32) error {
return errors.Wrap(err, "Delete Workflow Error")
}

_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
DELETE FROM workflow_state
WHERE
workflow_id = $1;
Expand All @@ -393,7 +393,7 @@ func (d TinkDB) DeleteWorkflow(ctx context.Context, id string, _ int32) error {
return errors.Wrap(err, "Delete Workflow Error")
}

res, err := tx.Exec(`
res, err := tx.ExecContext(ctx, `
UPDATE workflow
SET
deleted_at = NOW()
Expand Down Expand Up @@ -469,11 +469,11 @@ func (d TinkDB) UpdateWorkflow(ctx context.Context, wf Workflow, _ int32) error

switch {
case wf.Hardware == "" && wf.Template != "":
_, err = tx.Exec(`UPDATE workflow SET updated_at = NOW(), template = $2 WHERE id = $1;`, wf.ID, wf.Template)
_, err = tx.ExecContext(ctx, `UPDATE workflow SET updated_at = NOW(), template = $2 WHERE id = $1;`, wf.ID, wf.Template)
case wf.Hardware != "" && wf.Template == "":
_, err = tx.Exec(`UPDATE workflow SET updated_at = NOW(), devices = $2 WHERE id = $1;`, wf.ID, wf.Hardware)
_, err = tx.ExecContext(ctx, `UPDATE workflow SET updated_at = NOW(), devices = $2 WHERE id = $1;`, wf.ID, wf.Hardware)
default:
_, err = tx.Exec(`UPDATE workflow SET updated_at = NOW(), template = $2, devices = $3 WHERE id = $1;`, wf.ID, wf.Template, wf.Hardware)
_, err = tx.ExecContext(ctx, `UPDATE workflow SET updated_at = NOW(), template = $2, devices = $3 WHERE id = $1;`, wf.ID, wf.Template, wf.Hardware)
}

if err != nil {
Expand All @@ -494,7 +494,7 @@ func (d TinkDB) UpdateWorkflowState(ctx context.Context, wfContext *pb.WorkflowC
return errors.Wrap(err, "BEGIN transaction")
}

_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
UPDATE workflow_state
SET current_task_name = $2,
current_action_name = $3,
Expand Down Expand Up @@ -581,7 +581,7 @@ func (d TinkDB) InsertIntoWorkflowEventTable(ctx context.Context, wfEvent *pb.Wo
}

// TODO "created_at" field should be set in worker and come in the request
_, err = tx.Exec(`
_, err = tx.ExecContext(ctx, `
INSERT INTO
workflow_event (workflow_id, worker_id, task_name, action_name, execution_time, message, status, created_at)
VALUES
Expand Down

0 comments on commit cd512b9

Please sign in to comment.