Convert option db.WithVersion to *uint32 (#195)

This commit is contained in:
Jim authored and GitHub committed 2020-07-15 13:12:15 -04:00
1 parent 28dbcd84bd
commit 1bebe7a261
10 files changed
+390 -57

No files matched your search

+4 -4
View File
@@ -32,8 +32,8 @@ type Options struct {
newOplogMsg *oplog.Message
newOplogMsgs *[]*oplog.Message
// WithVersion must be accessible from other packages
WithVersion uint32
// WithVersion must be accessible from other packages.
WithVersion *uint32
withSkipVetForWrite bool
}
@@ -54,7 +54,7 @@ func getDefaultOptions() Options {
WithFieldMaskPaths: []string{},
WithNullPaths: []string{},
WithLimit: 0,
WithVersion: 0,
WithVersion: nil,
}
}
@@ -121,7 +121,7 @@ func WithLimit(limit int) Option {
}
// WithVersion provides an option version number for update operations.
func WithVersion(version uint32) Option {
func WithVersion(version *uint32) Option {
return func(o *Options) {
o.WithVersion = version
}
+4 -3
View File
@@ -125,11 +125,12 @@ func Test_getOpts(t *testing.T) {
// test default of 0
opts := GetOpts()
testOpts := getDefaultOptions()
testOpts.WithVersion = 0
testOpts.WithVersion = nil
assert.Equal(opts, testOpts)
opts = GetOpts(WithVersion(2))
versionTwo := uint32(2)
opts = GetOpts(WithVersion(&versionTwo))
testOpts = getDefaultOptions()
testOpts.WithVersion = 2
testOpts.WithVersion = &versionTwo
assert.Equal(opts, testOpts)
})
t.Run("WithSkipVetForWrite", func(t *testing.T) {
+6 -2
View File
@@ -357,7 +357,8 @@ func (rw *Db) CreateItems(ctx context.Context, createItems []interface{}, opt ..
// used together. If WithVersion is used, then the update will include the
// version number in the update where clause, which basically makes the update
// use optimistic locking and the update will only succeed if the existing rows
// version matches the WithVersion option.
// version matches the WithVersion option. Zero is not a valid value for the
// WithVersion option and will return an error.
func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string, setToNullPaths []string, opt ...Option) (int, error) {
if rw.underlying == nil {
return NoRowsAffected, fmt.Errorf("update: missing underlying db %w", ErrNilParameter)
@@ -426,7 +427,10 @@ func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string
}
var underlying *gorm.DB
switch {
case opts.WithVersion > 0:
case opts.WithVersion != nil:
if *opts.WithVersion == 0 {
return NoRowsAffected, fmt.Errorf("update: with version option is zero: %w", ErrInvalidParameter)
}
if _, ok := scope.FieldByName("version"); !ok {
return NoRowsAffected, fmt.Errorf("update: %s does not have a version field", scope.TableName())
}
+27 -3
View File
@@ -29,6 +29,11 @@ func TestDb_Update(t *testing.T) {
publicId, err := NewPublicId("testuser")
require.NoError(t, err)
id := testId(t)
badVersion := uint32(22)
versionOne := uint32(1)
versionZero := uint32(0)
type args struct {
i *db_test.TestUser
fieldMaskPaths []string
@@ -78,12 +83,30 @@ func TestDb_Update(t *testing.T) {
},
fieldMaskPaths: []string{"Name", "PhoneNumber"},
setToNullPaths: []string{"Email"},
opt: []Option{WithVersion(22)},
opt: []Option{WithVersion(&badVersion)},
},
want: 0,
wantErr: false,
wantErrMsg: "",
},
{
name: "simple-with-zero-version",
args: args{
i: &db_test.TestUser{
StoreTestUser: &db_test.StoreTestUser{
Name: "simple-with-bad-version" + id,
Email: "updated" + id,
PhoneNumber: "updated" + id,
},
},
fieldMaskPaths: []string{"Name", "PhoneNumber"},
setToNullPaths: []string{"Email"},
opt: []Option{WithVersion(&versionZero)},
},
want: 0,
wantErr: true,
wantErrMsg: "update: with version option is zero: invalid parameter",
},
{
name: "simple-with-version",
args: args{
@@ -96,7 +119,7 @@ func TestDb_Update(t *testing.T) {
},
fieldMaskPaths: []string{"Name", "PhoneNumber"},
setToNullPaths: []string{"Email"},
opt: []Option{WithVersion(1)},
opt: []Option{WithVersion(&versionOne)},
},
want: 1,
wantErr: false,
@@ -270,7 +293,8 @@ func TestDb_Update(t *testing.T) {
car := testCar(t, db, "foo-"+id, id, int32(100))
car.Name = "friendly-" + id
rowsUpdated, err := w.Update(context.Background(), car, []string{"Name"}, nil, WithVersion(1))
versionOne := uint32(1)
rowsUpdated, err := w.Update(context.Background(), car, []string{"Name"}, nil, WithVersion(&versionOne))
assert.Error(err)
assert.Equal(0, rowsUpdated)
})