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 14, 2023
1 parent 2ad6341 commit bea289d
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 15 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.

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

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

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

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

4 changes: 2 additions & 2 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.

4 changes: 2 additions & 2 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.

4 changes: 2 additions & 2 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 'null' (string) rather than null or empty object.
func EmptyOrMap[K comparable, V any, M map[K]V](m M) interface{} {
if m == nil {
return "null"
}
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.

2 changes: 2 additions & 0 deletions tools/generate-helpers/pg-table-bindings/store.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,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

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

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

0 comments on commit bea289d

Please sign in to comment.