From 1811ab64be9b3b0b1e8d875f17686b740abc45b9 Mon Sep 17 00:00:00 2001 From: henrygd Date: Wed, 24 Sep 2025 15:07:11 -0400 Subject: [PATCH] add migration to fix bad cached mem values (#1196) --- .../migrations/1758738789_fix_cached_mem.go | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 internal/migrations/1758738789_fix_cached_mem.go diff --git a/internal/migrations/1758738789_fix_cached_mem.go b/internal/migrations/1758738789_fix_cached_mem.go new file mode 100644 index 00000000..63e2ddc9 --- /dev/null +++ b/internal/migrations/1758738789_fix_cached_mem.go @@ -0,0 +1,50 @@ +package migrations + +import ( + "github.com/henrygd/beszel/internal/entities/system" + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +// This can be deleted after Nov 2025 or so + +func init() { + m.Register(func(app core.App) error { + app.RunInTransaction(func(txApp core.App) error { + var systemIds []string + txApp.DB().NewQuery("SELECT id FROM systems").Column(&systemIds) + + for _, systemId := range systemIds { + var statRecordIds []string + txApp.DB().NewQuery("SELECT id FROM system_stats WHERE system = {:system} AND created > {:created}").Bind(map[string]any{"system": systemId, "created": "2025-09-21"}).Column(&statRecordIds) + + for _, statRecordId := range statRecordIds { + statRecord, err := txApp.FindRecordById("system_stats", statRecordId) + if err != nil { + return err + } + var systemStats system.Stats + err = statRecord.UnmarshalJSONField("stats", &systemStats) + if err != nil { + return err + } + // if mem buff cache is less than total mem, we don't need to fix it + if systemStats.MemBuffCache < systemStats.Mem { + continue + } + systemStats.MemBuffCache = 0 + statRecord.Set("stats", systemStats) + err = txApp.SaveNoValidate(statRecord) + if err != nil { + return err + } + } + } + + return nil + }) + return nil + }, func(app core.App) error { + return nil + }) +}