Skip to content

Commit

Permalink
refactor(pg): store empty map as {} not null
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Jul 25, 2023
1 parent 51f5077 commit 7d1d2f6
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 31 deletions.
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
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
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"}}),
{{- 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.

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.

0 comments on commit 7d1d2f6

Please sign in to comment.