Skip to content

Commit 65aa040

Browse files
committed
fix: use the db transaction reader and writer
1 parent acb3083 commit 65aa040

11 files changed

+46
-12
lines changed

internal/auth/oidc/repository_auth_method.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
179179
var rowCnt int
180180
for rows.Next() {
181181
rowCnt += 1
182-
err = r.reader.ScanRows(ctx, rows, &result)
182+
err = reader.ScanRows(ctx, rows, &result)
183183
if err != nil {
184184
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to scan rows for account"))
185185
}

internal/auth/repository_auth_method.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (amr *AuthMethodRepository) ListDeletedIds(ctx context.Context, since time.
147147
var deletedAuthMethodIDs []string
148148
var transactionTimestamp time.Time
149149
if _, err := amr.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, w db.Writer) error {
150-
rows, err := amr.writer.Query(ctx, listDeletedIdsQuery, []any{sql.Named("since", since)})
150+
rows, err := w.Query(ctx, listDeletedIdsQuery, []any{sql.Named("since", since)})
151151
if err != nil {
152152
return errors.Wrap(ctx, err, op)
153153
}

internal/credential/repository_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (s *StoreRepository) ListDeletedIds(ctx context.Context, since time.Time) (
118118
var deletedStoreIDs []string
119119
var transactionTimestamp time.Time
120120
if _, err := s.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, w db.Writer) error {
121-
rows, err := s.writer.Query(ctx, listDeletedIdsQuery, []any{sql.Named("since", since)})
121+
rows, err := w.Query(ctx, listDeletedIdsQuery, []any{sql.Named("since", since)})
122122
if err != nil {
123123
return errors.Wrap(ctx, err, op)
124124
}

internal/host/repository_catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (s *CatalogRepository) ListDeletedIds(ctx context.Context, since time.Time)
119119
var deletedCatalogIDs []string
120120
var transactionTimestamp time.Time
121121
if _, err := s.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, w db.Writer) error {
122-
rows, err := s.writer.Query(ctx, listDeletedIdsQuery, []any{sql.Named("since", since)})
122+
rows, err := w.Query(ctx, listDeletedIdsQuery, []any{sql.Named("since", since)})
123123
if err != nil {
124124
return errors.Wrap(ctx, err, op)
125125
}

internal/host/static/repository_host.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (r *Repository) UpdateHost(ctx context.Context, projectId string, h *Host,
168168
var rowsUpdated int
169169
var returnedHost *Host
170170
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
171-
func(_ db.Reader, w db.Writer) error {
171+
func(r db.Reader, w db.Writer) error {
172172
returnedHost = h.clone()
173173
var err error
174174
rowsUpdated, err = w.Update(ctx, returnedHost, dbMask, nullFields,
@@ -183,7 +183,7 @@ func (r *Repository) UpdateHost(ctx context.Context, projectId string, h *Host,
183183
ha := &hostAgg{
184184
PublicId: h.PublicId,
185185
}
186-
if err := r.reader.LookupByPublicId(ctx, ha); err != nil {
186+
if err := r.LookupByPublicId(ctx, ha); err != nil {
187187
return errors.Wrap(ctx, err, op, errors.WithMsg("failed to lookup host after update"))
188188
}
189189
returnedHost.SetIds = ha.getSetIds()

internal/iam/repository_role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (r *Repository) queryRoles(ctx context.Context, whereClause string, args []
326326
for _, retRole := range retRoles {
327327
roleIds = append(roleIds, retRole.PublicId)
328328
}
329-
retRoleGrantScopes, err = r.ListRoleGrantScopes(ctx, roleIds)
329+
retRoleGrantScopes, err = r.ListRoleGrantScopes(ctx, roleIds, WithReaderWriter(rd, w))
330330
if err != nil {
331331
return errors.Wrap(ctx, err, op, errors.WithMsg("failed to query role grant scopes"))
332332
}

internal/iam/repository_role_grant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersi
359359
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
360360
}
361361

362-
currentRoleGrants, err = r.ListRoleGrants(ctx, roleId)
362+
currentRoleGrants, err = r.ListRoleGrants(ctx, roleId, WithReaderWriter(reader, w))
363363
if err != nil {
364364
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to retrieve current role grants after set"))
365365
}

internal/server/options.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"time"
99

10+
"github.com/hashicorp/boundary/internal/db"
1011
"github.com/hashicorp/boundary/version"
1112
"github.com/hashicorp/nodeenrollment/types"
1213
)
@@ -54,6 +55,8 @@ type options struct {
5455
withFeature version.Feature
5556
withDirectlyConnected bool
5657
withWorkerPool []string
58+
WithReader db.Reader
59+
WithWriter db.Writer
5760
}
5861

5962
func getDefaultOptions() options {
@@ -276,3 +279,12 @@ func WithLocalStorageState(state string) Option {
276279
o.withLocalStorageState = state
277280
}
278281
}
282+
283+
// WithReaderWriter is used to share the same database reader
284+
// and writer when executing sql within a transaction.
285+
func WithReaderWriter(r db.Reader, w db.Writer) Option {
286+
return func(o *options) {
287+
o.WithReader = r
288+
o.WithWriter = w
289+
}
290+
}

internal/server/options_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/hashicorp/boundary/internal/db"
1314
"github.com/hashicorp/boundary/version"
1415
"github.com/stretchr/testify/assert"
1516
)
@@ -251,4 +252,19 @@ func Test_GetOpts(t *testing.T) {
251252
testOpts.withNewIdFunc = nil
252253
assert.Equal(t, opts, testOpts)
253254
})
255+
t.Run("WithReaderWriter", func(t *testing.T) {
256+
reader := &db.Db{}
257+
writer := &db.Db{}
258+
testOpts := getDefaultOptions()
259+
assert.Nil(t, testOpts.WithReader)
260+
assert.Nil(t, testOpts.WithWriter)
261+
testOpts.WithReader = reader
262+
testOpts.WithWriter = writer
263+
opts := GetOpts(WithReaderWriter(reader, writer))
264+
opts.withNewIdFunc = nil
265+
testOpts.withNewIdFunc = nil
266+
assert.Equal(t, reader, opts.WithReader)
267+
assert.Equal(t, writer, opts.WithWriter)
268+
assert.Equal(t, opts, testOpts)
269+
})
254270
}

internal/server/repository_worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ func (r *Repository) UpdateWorker(ctx context.Context, worker *Worker, version u
569569
if ret, err = wAgg.toWorker(ctx); err != nil {
570570
return err
571571
}
572-
ret.RemoteStorageStates, err = r.ListWorkerStorageBucketCredentialState(ctx, ret.GetPublicId())
572+
ret.RemoteStorageStates, err = r.ListWorkerStorageBucketCredentialState(ctx, ret.GetPublicId(), WithReaderWriter(reader, w))
573573
if err != nil {
574574
return err
575575
}

0 commit comments

Comments
 (0)