feature (db): Add support for multi-column PKs (#1658)

This commit is contained in:
Jim authored and GitHub committed 2021-11-03 14:04:47 -04:00
1 parent 8d3f666bba
commit 4df3b40def
9 files changed
+526 -69

No files matched your search

+10 -8
View File
@@ -46,6 +46,16 @@ func UpdateFields(i interface{}, fieldMaskPaths []string, setToNullPaths []strin
val := reflect.Indirect(reflect.ValueOf(i))
structTyp := val.Type()
for i := 0; i < structTyp.NumField(); i++ {
if f, ok := maskPaths[strings.ToUpper(structTyp.Field(i).Name)]; ok {
updateFields[f] = val.Field(i).Interface()
found[strings.ToUpper(f)] = struct{}{}
continue
}
if f, ok := nullPaths[strings.ToUpper(structTyp.Field(i).Name)]; ok {
updateFields[f] = gorm.Expr("NULL")
found[strings.ToUpper(f)] = struct{}{}
continue
}
kind := structTyp.Field(i).Type.Kind()
if kind == reflect.Struct || kind == reflect.Ptr {
embType := structTyp.Field(i).Type
@@ -76,14 +86,6 @@ func UpdateFields(i interface{}, fieldMaskPaths []string, setToNullPaths []strin
continue
}
}
if f, ok := maskPaths[strings.ToUpper(structTyp.Field(i).Name)]; ok {
updateFields[f] = val.Field(i).Interface()
found[strings.ToUpper(f)] = struct{}{}
}
if f, ok := nullPaths[strings.ToUpper(structTyp.Field(i).Name)]; ok {
updateFields[f] = gorm.Expr("NULL")
found[strings.ToUpper(f)] = struct{}{}
}
}
if missing := findMissingPaths(setToNullPaths, found); len(missing) != 0 {
+18
View File
@@ -4,9 +4,13 @@ import (
"testing"
"github.com/hashicorp/boundary/internal/db/db_test"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/go-secure-stdlib/base62"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm"
)
@@ -398,6 +402,20 @@ func TestUpdateFields(t *testing.T) {
assert.Equal(tt.want, got)
})
}
t.Run("valid-embedded-timestamp", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
wantTs := &timestamp.Timestamp{
Timestamp: &timestamppb.Timestamp{
Seconds: 1,
Nanos: 1,
},
}
u := testUser(t, "", "")
u.UpdateTime = wantTs
got, err := UpdateFields(u, []string{"UpdateTime"}, nil)
require.NoError(err)
assert.True(proto.Equal(wantTs, got["UpdateTime"].(*timestamp.Timestamp)))
})
}
func testUser(t *testing.T, name, email string) *db_test.TestUser {