Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pg): store nil map as empty {} #6929

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion central/cluster/store/cluster/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions central/deployment/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions central/namespace/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions central/rbac/k8srole/internal/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions central/rbac/k8srolebinding/internal/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions central/serviceaccount/internal/store/postgres/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/postgres/pgutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func NilOrUUID(value string) *uuid.UUID {
return &id
}

// EmptyOrMap allows for map to be stored explicit as an empty object ({}) rather than null.
func EmptyOrMap[K comparable, V any, M map[K]V](m M) interface{} {
if m == nil {
return make(M)
}
return m
}

// CreateTableFromModel executes input create statement using the input connection.
func CreateTableFromModel(ctx context.Context, db *gorm.DB, createStmt *postgres.CreateStmts) {
// Partitioned tables are not supported by Gorm migration or models
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion tools/generate-helpers/pg-table-bindings/store.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func {{ template "insertFunctionName" $schema }}({{ if eq (len $schema.Children)
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the copyFrom template needs this change too (at least to be future-proof).
Maybe the {{- range $field := $schema.DBColumnFields -}} ... {{- end }} pattern could be extracted into another template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, good point. I'll try to add a test for it too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{- else }}
{{$field.Getter "obj"}},{{end}}
{{- end}}
Expand Down Expand Up @@ -267,7 +269,9 @@ func {{ template "copyFunctionName" $schema }}(ctx context.Context, s pgSearch.D
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else}}
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
{{- else }}
{{$field.Getter "obj"}},{{end}}
{{- end}}
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package postgres

import (
"context"
"fmt"

"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/sac"
Expand All @@ -19,5 +20,25 @@ func (s *TestSingleKeyStructsStoreSuite) TestStoreNilMap() {
row := s.testDB.QueryRow(ctx, "select labels from test_single_key_structs")
err := row.Scan(&val)
s.NoError(err)
s.Equal("null", val)
s.Equal("{}", val)
}

func (s *TestSingleKeyStructsStoreSuite) TestStoreNilMapUpsertMany() {
ctx := sac.WithAllAccess(context.Background())

const batchSize = 10000
testSingleKeyStructs := make([]*storage.TestSingleKeyStruct, batchSize)
for i := range testSingleKeyStructs {
testSingleKeyStructs[i] = &storage.TestSingleKeyStruct{
Key: fmt.Sprintf("%d", i),
Name: fmt.Sprintf("%d", i),
}
}
s.NoError(s.store.UpsertMany(ctx, testSingleKeyStructs))

var val string
row := s.testDB.QueryRow(ctx, "select labels from test_single_key_structs limit 1")
err := row.Scan(&val)
s.NoError(err)
s.Equal("{}", val)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.