Pull request: home: fix 8 to 9 migration

Updates #2988.

Squashed commit of the following:

commit 1b9f145be0dbcca9848e02942cea294315baa9cc
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 20 18:41:05 2021 +0300

    home: fix 8 to 9 migration
This commit is contained in:
Ainar Garipov 2021-04-20 18:56:15 +03:00
parent 93638a1936
commit 55cd4ae254
2 changed files with 52 additions and 17 deletions

View file

@ -490,7 +490,16 @@ func upgradeSchema8to9(diskConf yobj) (err error) {
return fmt.Errorf("unexpected type of dns: %T", dnsVal)
}
autohostTLDVal := dns["autohost_tld"]
autohostTLDVal, ok := dns["autohost_tld"]
if !ok {
// This happens when upgrading directly from v0.105.2, because
// dns.autohost_tld was never set to any value. Go on and leave
// it that way.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2988.
return nil
}
autohostTLD, ok := autohostTLDVal.(string)
if !ok {
return fmt.Errorf("undexpected type of dns.autohost_tld: %T", autohostTLDVal)

View file

@ -92,28 +92,54 @@ func TestUpgradeSchema7to8(t *testing.T) {
func TestUpgradeSchema8to9(t *testing.T) {
const tld = "foo"
oldConf := yobj{
"dns": yobj{
"autohost_tld": tld,
},
"schema_version": 8,
}
err := upgradeSchema8to9(oldConf)
require.NoError(t, err)
t.Run("with_autohost_tld", func(t *testing.T) {
oldConf := yobj{
"dns": yobj{
"autohost_tld": tld,
},
"schema_version": 8,
}
require.Equal(t, oldConf["schema_version"], 9)
err := upgradeSchema8to9(oldConf)
require.NoError(t, err)
dnsVal, ok := oldConf["dns"]
require.True(t, ok)
require.Equal(t, oldConf["schema_version"], 9)
newDNSConf, ok := dnsVal.(yobj)
require.True(t, ok)
dnsVal, ok := oldConf["dns"]
require.True(t, ok)
localDomainName, ok := newDNSConf["local_domain_name"].(string)
require.True(t, ok)
newDNSConf, ok := dnsVal.(yobj)
require.True(t, ok)
assert.Equal(t, tld, localDomainName)
localDomainName, ok := newDNSConf["local_domain_name"].(string)
require.True(t, ok)
assert.Equal(t, tld, localDomainName)
})
t.Run("without_autohost_tld", func(t *testing.T) {
oldConf := yobj{
"dns": yobj{},
"schema_version": 8,
}
err := upgradeSchema8to9(oldConf)
require.NoError(t, err)
require.Equal(t, oldConf["schema_version"], 9)
dnsVal, ok := oldConf["dns"]
require.True(t, ok)
newDNSConf, ok := dnsVal.(yobj)
require.True(t, ok)
// Should be nil in order to be set to the default value by the
// following config rewrite.
_, ok = newDNSConf["local_domain_name"]
require.False(t, ok)
})
}
// assertEqualExcept removes entries from configs and compares them.