unified-storage: fixes for sqlkv to work with postgres (#115961)

* unified-storage: fixes for sqlkv to work with postgres
This commit is contained in:
Will Assis
2026-01-08 08:21:35 -05:00
committed by GitHub
parent dff9bea3e8
commit 58e9e4a56d
5 changed files with 15 additions and 7 deletions
@@ -12,7 +12,7 @@ INSERT INTO {{ .Ident .TableName }}
VALUES (
{{ .Arg .GUID }},
{{ .Arg .KeyPath }},
COALESCE({{ .Arg .Value }}, ""),
{{ .Arg .Value }},
{{ .Arg .Group }},
{{ .Arg .Resource }},
{{ .Arg .Namespace }},
@@ -10,7 +10,7 @@ INSERT INTO {{ .Ident "resource_history" }}
{{ .Ident "folder" }}
)
VALUES (
COALESCE({{ .Arg .Value }}, ""),
{{ .Arg .Value }},
{{ .Arg .GUID }},
{{ .Arg .Group }},
{{ .Arg .Resource }},
@@ -5,7 +5,7 @@ INSERT INTO {{ .Ident .TableName }}
)
VALUES (
{{ .Arg .KeyPath }},
COALESCE({{ .Arg .Value }}, "")
{{ .Arg .Value }}
)
{{- if eq .DialectName "mysql" }}
ON DUPLICATE KEY UPDATE {{ .Ident "value" }} = {{ .Arg .Value }}
+9 -4
View File
@@ -349,6 +349,11 @@ func (w *sqlWriteCloser) Close() error {
}
w.closed = true
value := w.buf.Bytes()
if value == nil {
// to prevent NOT NULL constraint violations
value = []byte{}
}
// do regular kv save: simple key_path + value insert with conflict check.
// can only do this on resource_events for now, until we drop the columns in resource_history
@@ -356,7 +361,7 @@ func (w *sqlWriteCloser) Close() error {
_, err := dbutil.Exec(w.ctx, w.kv.db, sqlKVSaveEvent, sqlKVSaveRequest{
SQLTemplate: sqltemplate.New(w.kv.dialect),
sqlKVSectionKey: w.sectionKey,
Value: w.buf.Bytes(),
Value: value,
})
if err != nil {
@@ -380,7 +385,7 @@ func (w *sqlWriteCloser) Close() error {
SQLTemplate: sqltemplate.New(w.kv.dialect),
sqlKVSectionKey: w.sectionKey,
GUID: uuid.New().String(),
Value: w.buf.Bytes(),
Value: value,
})
if err != nil {
@@ -397,7 +402,7 @@ func (w *sqlWriteCloser) Close() error {
_, err = dbutil.Exec(w.ctx, w.kv.db, sqlKVUpdateData, sqlKVSaveRequest{
SQLTemplate: sqltemplate.New(w.kv.dialect),
sqlKVSectionKey: w.sectionKey,
Value: w.buf.Bytes(),
Value: value,
})
if err != nil {
@@ -433,7 +438,7 @@ func (w *sqlWriteCloser) Close() error {
_, err = dbutil.Exec(w.ctx, tx, sqlKVInsertLegacyResourceHistory, sqlKVSaveRequest{
SQLTemplate: sqltemplate.New(w.kv.dialect),
sqlKVSectionKey: w.sectionKey,
Value: w.buf.Bytes(),
Value: value,
GUID: dataKey.GUID,
Group: dataKey.Group,
Resource: dataKey.Resource,
@@ -217,5 +217,8 @@ func initResourceTables(mg *migrator.Migrator) string {
migrator.ConvertUniqueKeyToPrimaryKey(mg, oldResourceVersionUniqueKey, updatedResourceVersionTable)
mg.AddMigration("Change key_path collation of resource_history in postgres", migrator.NewRawSQLMigration("").Postgres(`ALTER TABLE resource_history ALTER COLUMN key_path TYPE VARCHAR(2048) COLLATE "C";`))
mg.AddMigration("Change key_path collation of resource_events in postgres", migrator.NewRawSQLMigration("").Postgres(`ALTER TABLE resource_events ALTER COLUMN key_path TYPE VARCHAR(2048) COLLATE "C";`))
return marker
}