Pull request 1781: 5627-fix-migration

Updates #5627.

Squashed commit of the following:

commit 018ead11959efeaace5312b5990ad82b6fa1f72d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Mar 24 12:48:25 2023 +0300

    home: fix more

commit 4bc57a07412bfb7fb7e6c88c14037c567c94d373
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Mar 24 12:32:57 2023 +0300

    home: fix statistics migration
This commit is contained in:
Ainar Garipov 2023-03-24 13:17:57 +03:00
parent 8de994d077
commit 0bc3ef89ea
2 changed files with 41 additions and 5 deletions

View File

@ -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}

View File

@ -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,