diff --git a/internal/db/read_writer.go b/internal/db/read_writer.go index 742a67b36..df26830cf 100644 --- a/internal/db/read_writer.go +++ b/internal/db/read_writer.go @@ -249,6 +249,12 @@ func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string return NoRowsAffected, fmt.Errorf("update: primary key is not set") } + for _, f := range scope.PrimaryFields() { + if contains(fieldMaskPaths, f.Name) { + return NoRowsAffected, fmt.Errorf("update: not allowed on primary key field %s: %w", f.Name, ErrInvalidFieldMask) + } + } + opts := GetOpts(opt...) withDebug := opts.withDebug withOplog := opts.withOplog @@ -594,3 +600,12 @@ func isNil(i interface{}) bool { } return false } + +func contains(ss []string, t string) bool { + for _, s := range ss { + if strings.EqualFold(s, t) { + return true + } + } + return false +} diff --git a/internal/db/read_writer_test.go b/internal/db/read_writer_test.go index ac864db63..d4fd24ebf 100644 --- a/internal/db/read_writer_test.go +++ b/internal/db/read_writer_test.go @@ -117,6 +117,26 @@ func TestDb_Update(t *testing.T) { wantEmail: "", wantPhoneNumber: "updated" + id, }, + { + name: "primary-key", + args: args{ + i: &db_test.TestUser{ + StoreTestUser: &db_test.StoreTestUser{ + Name: "primary-key" + id, + Email: "updated" + id, + PhoneNumber: "updated" + id, + PublicId: publicId, + CreateTime: now, + UpdateTime: now, + }, + }, + fieldMaskPaths: []string{"Id"}, + setToNullPaths: []string{"Email"}, + }, + want: 0, + wantErr: true, + wantErrMsg: "update: not allowed on primary key field Id: invalid field mask", + }, { name: "both are missing", args: args{