diff --git a/internal/home/upgrade.go b/internal/home/upgrade.go index 79c71673..a97386a7 100644 --- a/internal/home/upgrade.go +++ b/internal/home/upgrade.go @@ -839,9 +839,9 @@ func upgradeSchema14to15(diskConf yobj) (err error) { } type temp struct { + val any from string to string - val any } replaces := []temp{ {from: "querylog_enabled", to: "enabled", val: true}, @@ -876,6 +876,18 @@ func upgradeSchema14to15(diskConf yobj) (err error) { // 'enabled': true // 'interval': 1 // 'ignored': [] +// +// If statistics were disabled: +// +// # BEFORE: +// 'dns': +// 'statistics_interval': 0 +// +// # AFTER: +// 'statistics': +// 'enabled': false +// 'interval': 1 +// 'ignored': [] func upgradeSchema15to16(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 15 to 16") diskConf["schema_version"] = 16 @@ -897,10 +909,23 @@ func upgradeSchema15to16(diskConf yobj) (err error) { } const field = "statistics_interval" - v, has := dns[field] + statsIvlVal, has := dns[field] if has { - stats["enabled"] = v != 0 - stats["interval"] = v + var statsIvl int + statsIvl, ok = statsIvlVal.(int) + if !ok { + return fmt.Errorf("unexpected type of dns.statistics_interval: %T", statsIvlVal) + } + + if statsIvl == 0 { + // Set the interval to the default value of one day to make sure + // that it passes the validations. + stats["interval"] = 1 + stats["enabled"] = false + } else { + stats["interval"] = statsIvl + stats["enabled"] = true + } } delete(dns, field) @@ -1099,6 +1124,12 @@ func upgradeSchema19to20(diskConf yobj) (err error) { if !ok { return fmt.Errorf("unexpected type of %s: %T", field, statsIvlVal) } + + // The initial version of upgradeSchema16to17 did not set the zero + // interval to a non-zero one. So, reset it now. + if statsIvl == 0 { + statsIvl = 1 + } } stats[field] = timeutil.Duration{Duration: time.Duration(statsIvl) * timeutil.Day} diff --git a/internal/home/upgrade_test.go b/internal/home/upgrade_test.go index 193451c0..f4091e84 100644 --- a/internal/home/upgrade_test.go +++ b/internal/home/upgrade_test.go @@ -729,7 +729,7 @@ func TestUpgradeSchema15to16(t *testing.T) { want: yobj{ "statistics": map[string]any{ "enabled": false, - "interval": 0, + "interval": 1, "ignored": []any{}, }, "dns": map[string]any{}, @@ -963,6 +963,11 @@ func TestUpgradeSchema19to20(t *testing.T) { want: timeutil.Duration{Duration: timeutil.Day}, wantErr: "", name: "success", + }, { + ivl: 0, + want: timeutil.Duration{Duration: timeutil.Day}, + wantErr: "", + name: "success", }, { ivl: 0.25, want: 0,