From 017fa1b5afee5059b5e8f3cdea63f72edc218e29 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 9 Jan 2025 15:42:12 +0100 Subject: [PATCH] fix(restore): restore encoded float values properly Resolves https://kolaente.dev/vikunja/vikunja/issues/2934 --- pkg/modules/dump/restore.go | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/pkg/modules/dump/restore.go b/pkg/modules/dump/restore.go index 3f682f865..608e86a4e 100644 --- a/pkg/modules/dump/restore.go +++ b/pkg/modules/dump/restore.go @@ -199,6 +199,13 @@ func restoreTableData(tables map[string]*zip.File) error { "users": {"frontend_settings"}, } + floatFields := map[string][]string{ + "buckets": {"position"}, + "project_views": {"position"}, + "projects": {"position"}, + "task_positions": {"position"}, + } + // Restore all db data for table, d := range tables { content, err := unmarshalFileToJSON(d) @@ -206,8 +213,7 @@ func restoreTableData(tables map[string]*zip.File) error { return fmt.Errorf("could not read table %s: %w", table, err) } - fields, hasJSONFields := jsonFields[table] - if hasJSONFields { + processFields := func(fields []string, isFloat bool) error { for i := range content { for _, f := range fields { @@ -225,9 +231,32 @@ func restoreTableData(tables map[string]*zip.File) error { decoded = []byte(content[i][f].(string)) } - content[i][f] = string(decoded) + if isFloat { + val, err := strconv.ParseFloat(string(decoded), 64) + if err != nil { + return fmt.Errorf("could not parse double value for field '%s': %w", f, err) + } + content[i][f] = val + } else { + content[i][f] = string(decoded) + } } } + return nil + } + + // Process JSON fields + if fields, hasJSONFields := jsonFields[table]; hasJSONFields { + if err := processFields(fields, false); err != nil { + return err + } + } + + // Process double fields + if fields, hasDoubleFields := floatFields[table]; hasDoubleFields { + if err := processFields(fields, true); err != nil { + return err + } } if err := db.Restore(table, content); err != nil {