From d7aaa9f7e2061d5e20f6bf576358c3bdce6f16ff Mon Sep 17 00:00:00 2001 From: Jim Date: Wed, 15 Jun 2022 14:43:39 -0400 Subject: [PATCH] fix (byow): minor updates to UpsertWorkerStatus(...) on conflict (#2194) --- internal/servers/repository_worker.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/servers/repository_worker.go b/internal/servers/repository_worker.go index 0105de6332..06e1fe1b71 100644 --- a/internal/servers/repository_worker.go +++ b/internal/servers/repository_worker.go @@ -277,8 +277,17 @@ func (r *Repository) UpsertWorkerStatus(ctx context.Context, worker *Worker, opt Action: append(db.SetColumns([]string{"address"}), db.SetColumnValues(map[string]interface{}{"last_status_time": "now()"})...), } - if err := w.Create(ctx, worker, db.WithOnConflict(workerCreateConflict)); err != nil { + var withRowsAffected int64 + err := w.Create(ctx, worker, db.WithOnConflict(workerCreateConflict), db.WithReturnRowsAffected(&withRowsAffected), + // The intent of this WithWhere option is to operate with the OnConflict such that the action + // taken by the OnConflict only applies if the conflict is on a row that is returned by this where + // statement, otherwise it should error out. + db.WithWhere("server_worker.type = 'kms'")) + switch { + case err != nil: return errors.Wrap(ctx, err, op, errors.WithMsg("error creating a worker")) + case withRowsAffected == 0: + return errors.New(ctx, errors.NotUnique, op, "error updating worker") } case opts.withKeyId != "": n, err := w.Update(ctx, worker, []string{"address"}, nil)