Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: use QueryRow if possible (part 2) (#1763)
p0ny18a5b61b7769
diff --git a/store/task.go b/store/task.goindex f2bf0166ef..c38f50d174 100644--- a/store/task.go+++ b/store/task.go@@ -380,8 +380,7 @@ func (s *Store) patchTaskRawStatus(ctx context.Context, patch *api.TaskStatusPat// createTaskImpl creates a new task.func (s *Store) createTaskImpl(ctx context.Context, tx *sql.Tx, create *api.TaskCreate) (*taskRaw, error) {- var row *sql.Rows- var err error+ var row *sql.Rowif create.Payload == "" {create.Payload = "{}"@@ -403,7 +402,7 @@ func (s *Store) createTaskImpl(ctx context.Context, tx *sql.Tx, create *api.TaskRETURNING id, creator_id, created_ts, updater_id, updated_ts, pipeline_id, stage_id, instance_id, database_id, name, status, type, payload, earliest_allowed_ts`if create.DatabaseID == nil {- row, err = tx.QueryContext(ctx, query,+ row = tx.QueryRowContext(ctx, query,create.CreatorID,create.CreatorID,create.PipelineID,@@ -433,7 +432,7 @@ func (s *Store) createTaskImpl(ctx context.Context, tx *sql.Tx, create *api.TaskVALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)RETURNING id, creator_id, created_ts, updater_id, updated_ts, pipeline_id, stage_id, instance_id, database_id, name, status, type, payload, earliest_allowed_ts`- row, err = tx.QueryContext(ctx, query,+ row = tx.QueryRowContext(ctx, query,create.CreatorID,create.CreatorID,create.PipelineID,@@ -448,42 +447,34 @@ func (s *Store) createTaskImpl(ctx context.Context, tx *sql.Tx, create *api.Task)}- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var taskRaw taskRaw- var databaseID sql.NullInt32- if err := row.Scan(- &taskRaw.ID,- &taskRaw.CreatorID,- &taskRaw.CreatedTs,- &taskRaw.UpdaterID,- &taskRaw.UpdatedTs,- &taskRaw.PipelineID,- &taskRaw.StageID,- &taskRaw.InstanceID,- &databaseID,- &taskRaw.Name,- &taskRaw.Status,- &taskRaw.Type,- &taskRaw.Payload,- &taskRaw.EarliestAllowedTs,- ); err != nil {- return nil, FormatError(err)- }- if databaseID.Valid {- val := int(databaseID.Int32)- taskRaw.DatabaseID = &val+ var taskRaw taskRaw+ var databaseID sql.NullInt32+ if err := row.Scan(+ &taskRaw.ID,+ &taskRaw.CreatorID,+ &taskRaw.CreatedTs,+ &taskRaw.UpdaterID,+ &taskRaw.UpdatedTs,+ &taskRaw.PipelineID,+ &taskRaw.StageID,+ &taskRaw.InstanceID,+ &databaseID,+ &taskRaw.Name,+ &taskRaw.Status,+ &taskRaw.Type,+ &taskRaw.Payload,+ &taskRaw.EarliestAllowedTs,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)}- return &taskRaw, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, common.FormatDBErrorEmptyRowWithQuery(query)+ if databaseID.Valid {+ val := int(databaseID.Int32)+ taskRaw.DatabaseID = &val+ }+ return &taskRaw, nil}func (s *Store) findTaskImpl(ctx context.Context, tx *sql.Tx, find *api.TaskFind) ([]*taskRaw, error) {@@ -601,46 +592,37 @@ func (s *Store) patchTaskImpl(ctx context.Context, tx *sql.Tx, patch *api.TaskPa}args = append(args, patch.ID)+ var taskRaw taskRaw// Execute update query with RETURNING.- row, err := tx.QueryContext(ctx, fmt.Sprintf(`+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`UPDATE taskSET `+strings.Join(set, ", ")+`WHERE id = $%dRETURNING id, creator_id, created_ts, updater_id, updated_ts, pipeline_id, stage_id, instance_id, database_id, name, status, type, payload, earliest_allowed_ts`, len(args)),args...,- )- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var taskRaw taskRaw- if err := row.Scan(- &taskRaw.ID,- &taskRaw.CreatorID,- &taskRaw.CreatedTs,- &taskRaw.UpdaterID,- &taskRaw.UpdatedTs,- &taskRaw.PipelineID,- &taskRaw.StageID,- &taskRaw.InstanceID,- &taskRaw.DatabaseID,- &taskRaw.Name,- &taskRaw.Status,- &taskRaw.Type,- &taskRaw.Payload,- &taskRaw.EarliestAllowedTs,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &taskRaw.ID,+ &taskRaw.CreatorID,+ &taskRaw.CreatedTs,+ &taskRaw.UpdaterID,+ &taskRaw.UpdatedTs,+ &taskRaw.PipelineID,+ &taskRaw.StageID,+ &taskRaw.InstanceID,+ &taskRaw.DatabaseID,+ &taskRaw.Name,+ &taskRaw.Status,+ &taskRaw.Type,+ &taskRaw.Payload,+ &taskRaw.EarliestAllowedTs,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("task not found with ID %d", patch.ID)}}- return &taskRaw, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("task not found with ID %d", patch.ID)}+ return &taskRaw, nil}// patchTaskStatusImpl updates a task status by ID. Returns the new state of the task after update.@@ -718,21 +700,17 @@ func (s *Store) patchTaskStatusImpl(ctx context.Context, tx *sql.Tx, patch *api.set, args = append(set, "status = $2"), append(args, patch.Status)args = append(args, patch.ID)+ var taskPatchedRaw *taskRaw+ var taskRaw taskRaw// Execute update query with RETURNING.- row := tx.QueryRowContext(ctx, `+ if err := tx.QueryRowContext(ctx, `UPDATE taskSET `+strings.Join(set, ", ")+`WHERE id = $3RETURNING id, creator_id, created_ts, updater_id, updated_ts, pipeline_id, stage_id, instance_id, database_id, name, status, type, payload, earliest_allowed_ts`,args...,- )-- var taskPatchedRaw *taskRaw- // TODO(dragonly): change all row.Next() to use QueryRow/QueryRowContext().-- var taskRaw taskRaw- if err := row.Scan(+ ).Scan(&taskRaw.ID,&taskRaw.CreatorID,&taskRaw.CreatedTs,diff --git a/store/task_dag.go b/store/task_dag.goindex 3e3be17aaa..b7446b347a 100644--- a/store/task_dag.go+++ b/store/task_dag.go@@ -109,34 +109,25 @@ func createTaskDAGImpl(ctx context.Context, tx *sql.Tx, create *api.TaskDAGCreatVALUES ($1, $2, $3)RETURNING id, created_ts, updated_ts, from_task_id, to_task_id, payload`- row, err := tx.QueryContext(ctx, query,+ var taskDAGRaw taskDAGRaw+ if err := tx.QueryRowContext(ctx, query,create.FromTaskID,create.ToTaskID,create.Payload,- )- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var taskDAGRaw taskDAGRaw- if err := row.Scan(- &taskDAGRaw.ID,- &taskDAGRaw.CreatedTs,- &taskDAGRaw.UpdatedTs,- &taskDAGRaw.FromTaskID,- &taskDAGRaw.ToTaskID,- &taskDAGRaw.Payload,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &taskDAGRaw.ID,+ &taskDAGRaw.CreatedTs,+ &taskDAGRaw.UpdatedTs,+ &taskDAGRaw.FromTaskID,+ &taskDAGRaw.ToTaskID,+ &taskDAGRaw.Payload,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)}- return &taskDAGRaw, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, common.FormatDBErrorEmptyRowWithQuery(query)+ return &taskDAGRaw, nil}func findTaskDAGRawListImpl(ctx context.Context, tx *sql.Tx, find *api.TaskDAGFind) ([]*taskDAGRaw, error) {diff --git a/store/task_run.go b/store/task_run.goindex 8254e047d9..752d38b961 100644--- a/store/task_run.go+++ b/store/task_run.go@@ -78,45 +78,35 @@ func (s *Store) createTaskRunImpl(ctx context.Context, tx *sql.Tx, create *api.TVALUES ($1, $2, $3, $4, 'RUNNING', $5, $6)RETURNING id, creator_id, created_ts, updater_id, updated_ts, task_id, name, status, type, code, comment, result, payload`- row, err := tx.QueryContext(ctx, query,+ var taskRunRaw taskRunRaw+ if err := tx.QueryRowContext(ctx, query,create.CreatorID,create.CreatorID,create.TaskID,create.Name,create.Type,create.Payload,- )-- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var taskRunRaw taskRunRaw- if err := row.Scan(- &taskRunRaw.ID,- &taskRunRaw.CreatorID,- &taskRunRaw.CreatedTs,- &taskRunRaw.UpdaterID,- &taskRunRaw.UpdatedTs,- &taskRunRaw.TaskID,- &taskRunRaw.Name,- &taskRunRaw.Status,- &taskRunRaw.Type,- &taskRunRaw.Code,- &taskRunRaw.Comment,- &taskRunRaw.Result,- &taskRunRaw.Payload,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &taskRunRaw.ID,+ &taskRunRaw.CreatorID,+ &taskRunRaw.CreatedTs,+ &taskRunRaw.UpdaterID,+ &taskRunRaw.UpdatedTs,+ &taskRunRaw.TaskID,+ &taskRunRaw.Name,+ &taskRunRaw.Status,+ &taskRunRaw.Type,+ &taskRunRaw.Code,+ &taskRunRaw.Comment,+ &taskRunRaw.Result,+ &taskRunRaw.Payload,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)}- return &taskRunRaw, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, common.FormatDBErrorEmptyRowWithQuery(query)+ return &taskRunRaw, nil}// getTaskRunRawTx retrieves a single taskRun based on find.@@ -162,45 +152,35 @@ func (s *Store) patchTaskRunStatusImpl(ctx context.Context, tx *sql.Tx, patch *awhere, args = append(where, fmt.Sprintf("task_id = $%d", len(args)+1)), append(args, *v)}- row, err := tx.QueryContext(ctx, `+ var taskRunRaw taskRunRaw+ if err := tx.QueryRowContext(ctx, `UPDATE task_runSET `+strings.Join(set, ", ")+`WHERE `+strings.Join(where, " AND ")+`RETURNING id, creator_id, created_ts, updater_id, updated_ts, task_id, name, status, type, code, comment, result, payload`,args...,- )-- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var taskRunRaw taskRunRaw- if err := row.Scan(- &taskRunRaw.ID,- &taskRunRaw.CreatorID,- &taskRunRaw.CreatedTs,- &taskRunRaw.UpdaterID,- &taskRunRaw.UpdatedTs,- &taskRunRaw.TaskID,- &taskRunRaw.Name,- &taskRunRaw.Status,- &taskRunRaw.Type,- &taskRunRaw.Code,- &taskRunRaw.Comment,- &taskRunRaw.Result,- &taskRunRaw.Payload,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &taskRunRaw.ID,+ &taskRunRaw.CreatorID,+ &taskRunRaw.CreatedTs,+ &taskRunRaw.UpdaterID,+ &taskRunRaw.UpdatedTs,+ &taskRunRaw.TaskID,+ &taskRunRaw.Name,+ &taskRunRaw.Status,+ &taskRunRaw.Type,+ &taskRunRaw.Code,+ &taskRunRaw.Comment,+ &taskRunRaw.Result,+ &taskRunRaw.Payload,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project ID not found: %d", patch.ID)}}- return &taskRunRaw, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project ID not found: %d", patch.ID)}+ return &taskRunRaw, nil}func (s *Store) findTaskRunImpl(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*taskRunRaw, error) {diff --git a/store/vcs.go b/store/vcs.goindex 2d6e2b5656..aeed9f4e69 100644--- a/store/vcs.go+++ b/store/vcs.go@@ -263,7 +263,8 @@ func createVCSImpl(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*vcsVALUES ($1, $2, $3, $4, $5, $6, $7, $8)RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, type, instance_url, api_url, application_id, secret`- row, err := tx.QueryContext(ctx, query,+ var vcs vcsRaw+ if err := tx.QueryRowContext(ctx, query,create.CreatorID,create.CreatorID,create.Name,@@ -272,36 +273,25 @@ func createVCSImpl(ctx context.Context, tx *sql.Tx, create *api.VCSCreate) (*vcscreate.APIURL,create.ApplicationID,create.Secret,- )-- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var vcs vcsRaw- if err := row.Scan(- &vcs.ID,- &vcs.CreatorID,- &vcs.CreatedTs,- &vcs.UpdaterID,- &vcs.UpdatedTs,- &vcs.Name,- &vcs.Type,- &vcs.InstanceURL,- &vcs.APIURL,- &vcs.ApplicationID,- &vcs.Secret,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &vcs.ID,+ &vcs.CreatorID,+ &vcs.CreatedTs,+ &vcs.UpdaterID,+ &vcs.UpdatedTs,+ &vcs.Name,+ &vcs.Type,+ &vcs.InstanceURL,+ &vcs.APIURL,+ &vcs.ApplicationID,+ &vcs.Secret,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)}- return &vcs, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, common.FormatDBErrorEmptyRowWithQuery(query)+ return &vcs, nil}func findVCSImpl(ctx context.Context, tx *sql.Tx, find *api.VCSFind) ([]*vcsRaw, error) {@@ -377,43 +367,34 @@ func patchVCSImpl(ctx context.Context, tx *sql.Tx, patch *api.VCSPatch) (*vcsRaw}args = append(args, patch.ID)+ var vcs vcsRaw// Execute update query with RETURNING.- row, err := tx.QueryContext(ctx, fmt.Sprintf(`+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`UPDATE vcsSET `+strings.Join(set, ", ")+`WHERE id = $%dRETURNING id, creator_id, created_ts, updater_id, updated_ts, name, type, instance_url, api_url, application_id, secret`, len(args)),args...,- )- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var vcs vcsRaw- if err := row.Scan(- &vcs.ID,- &vcs.CreatorID,- &vcs.CreatedTs,- &vcs.UpdaterID,- &vcs.UpdatedTs,- &vcs.Name,- &vcs.Type,- &vcs.InstanceURL,- &vcs.APIURL,- &vcs.ApplicationID,- &vcs.Secret,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &vcs.ID,+ &vcs.CreatorID,+ &vcs.CreatedTs,+ &vcs.UpdaterID,+ &vcs.UpdatedTs,+ &vcs.Name,+ &vcs.Type,+ &vcs.InstanceURL,+ &vcs.APIURL,+ &vcs.ApplicationID,+ &vcs.Secret,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("vcs ID not found: %d", patch.ID)}}- return &vcs, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("vcs ID not found: %d", patch.ID)}+ return &vcs, nil}// deleteVCSImpl permanently deletes a vcs by ID.diff --git a/store/view.go b/store/view.goindex 4ad3624529..0194197e75 100644--- a/store/view.go+++ b/store/view.go@@ -182,7 +182,8 @@ func (s *Store) createViewImpl(ctx context.Context, tx *sql.Tx, create *api.ViewVALUES ($1, $2, $3, $4, $5, $6, $7, $8)` +"RETURNING id, creator_id, created_ts, updater_id, updated_ts, database_id, name, definition, comment" + ``- row, err := tx.QueryContext(ctx, query,+ var viewRaw viewRaw+ if err := tx.QueryRowContext(ctx, query,create.CreatorID,create.CreatedTs,create.CreatorID,@@ -191,34 +192,23 @@ func (s *Store) createViewImpl(ctx context.Context, tx *sql.Tx, create *api.Viewcreate.Name,create.Definition,create.Comment,- )-- if err != nil {- return nil, FormatError(err)- }- defer row.Close()-- if row.Next() {- var viewRaw viewRaw- if err := row.Scan(- &viewRaw.ID,- &viewRaw.CreatorID,- &viewRaw.CreatedTs,- &viewRaw.UpdaterID,- &viewRaw.UpdatedTs,- &viewRaw.DatabaseID,- &viewRaw.Name,- &viewRaw.Definition,- &viewRaw.Comment,- ); err != nil {- return nil, FormatError(err)+ ).Scan(+ &viewRaw.ID,+ &viewRaw.CreatorID,+ &viewRaw.CreatedTs,+ &viewRaw.UpdaterID,+ &viewRaw.UpdatedTs,+ &viewRaw.DatabaseID,+ &viewRaw.Name,+ &viewRaw.Definition,+ &viewRaw.Comment,+ ); err != nil {+ if err == sql.ErrNoRows {+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)}- return &viewRaw, nil- }- if err := row.Err(); err != nil {return nil, FormatError(err)}- return nil, common.FormatDBErrorEmptyRowWithQuery(query)+ return &viewRaw, nil}func (s *Store) findViewImpl(ctx context.Context, tx *sql.Tx, find *api.ViewFind) ([]*viewRaw, error) {