Pull request 2494: AGDNS-3279-configmigrate-slog
Some checks failed
build / test (macOS-latest) (push) Has been cancelled
build / test (ubuntu-latest) (push) Has been cancelled
build / test (windows-latest) (push) Has been cancelled
build / build-release (push) Has been cancelled
build / notify (push) Has been cancelled
lint / go-lint (push) Has been cancelled
lint / eslint (push) Has been cancelled
lint / notify (push) Has been cancelled

Squashed commit of the following:

commit c92b175fb64ce512d60f160d9ab23225e100fa40
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Oct 6 17:32:22 2025 +0300

    configmigrate: fix typo

commit c1fa0ed1ea05b15fa6d506c6d5aab6373c9b5844
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Oct 6 17:00:59 2025 +0300

    all: configmigrate slog
This commit is contained in:
Stanislav Chzhen
2025-10-06 18:08:19 +03:00
parent 43e132888c
commit 68fac0147d
38 changed files with 665 additions and 122 deletions

View File

@@ -0,0 +1,13 @@
package configmigrate
import (
"time"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// testTimeout is the common timeout for tests.
const testTimeout = 1 * time.Second
// testLogger is a logger used in tests.
var testLogger = slogutil.NewDiscardLogger()

View File

@@ -0,0 +1,13 @@
package configmigrate_test
import (
"time"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// testTimeout is the common timeout for tests.
const testTimeout = 1 * time.Second
// testLogger is a logger used in tests.
var testLogger = slogutil.NewDiscardLogger()

View File

@@ -12,17 +12,25 @@ import (
"github.com/stretchr/testify/require"
)
// TODO(e.burkov): Cover all migrations, use a testdata/ dir.
func TestUpgradeSchema1to2(t *testing.T) {
diskConf := testDiskConf(1)
m := New(&Config{
// emptyMigrator is a helper function that returns initialized with empty values
// *Migrator and no-op implementations for tests.
func emptyMigrator() (m *Migrator) {
return New(&Config{
Logger: testLogger,
WorkingDir: "",
DataDir: "",
})
}
err := m.migrateTo2(diskConf)
func TestUpgradeSchema1to2(t *testing.T) {
t.Parallel()
diskConf := testDiskConf(1)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo2(ctx, diskConf)
require.NoError(t, err)
require.Equal(t, diskConf["schema_version"], 2)
@@ -43,9 +51,13 @@ func TestUpgradeSchema1to2(t *testing.T) {
}
func TestUpgradeSchema2to3(t *testing.T) {
t.Parallel()
diskConf := testDiskConf(2)
err := migrateTo3(diskConf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo3(ctx, diskConf)
require.NoError(t, err)
require.Equal(t, diskConf["schema_version"], 3)
@@ -75,6 +87,8 @@ func TestUpgradeSchema2to3(t *testing.T) {
}
func TestUpgradeSchema5to6(t *testing.T) {
t.Parallel()
const newSchemaVer = 6
testCases := []struct {
@@ -156,7 +170,11 @@ func TestUpgradeSchema5to6(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo6(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo6(ctx, tc.in)
testutil.AssertErrorMsg(t, tc.wantErr, err)
assert.Equal(t, tc.want, tc.in)
})
@@ -164,6 +182,8 @@ func TestUpgradeSchema5to6(t *testing.T) {
}
func TestUpgradeSchema7to8(t *testing.T) {
t.Parallel()
const host = "1.2.3.4"
oldConf := yobj{
"dns": yobj{
@@ -172,7 +192,9 @@ func TestUpgradeSchema7to8(t *testing.T) {
"schema_version": 7,
}
err := migrateTo8(oldConf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo8(ctx, oldConf)
require.NoError(t, err)
require.Equal(t, oldConf["schema_version"], 8)
@@ -190,9 +212,13 @@ func TestUpgradeSchema7to8(t *testing.T) {
}
func TestUpgradeSchema8to9(t *testing.T) {
t.Parallel()
const tld = "foo"
t.Run("with_autohost_tld", func(t *testing.T) {
t.Parallel()
oldConf := yobj{
"dns": yobj{
"autohost_tld": tld,
@@ -200,7 +226,9 @@ func TestUpgradeSchema8to9(t *testing.T) {
"schema_version": 8,
}
err := migrateTo9(oldConf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo9(ctx, oldConf)
require.NoError(t, err)
require.Equal(t, oldConf["schema_version"], 9)
@@ -218,12 +246,16 @@ func TestUpgradeSchema8to9(t *testing.T) {
})
t.Run("without_autohost_tld", func(t *testing.T) {
t.Parallel()
oldConf := yobj{
"dns": yobj{},
"schema_version": 8,
}
err := migrateTo9(oldConf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo9(ctx, oldConf)
require.NoError(t, err)
require.Equal(t, oldConf["schema_version"], 9)
@@ -315,6 +347,8 @@ func testDNSConf(schemaVersion int) (dnsConf yobj) {
}
func TestAddQUICPort(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
ups string
@@ -387,6 +421,8 @@ func TestAddQUICPort(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
withPort := addQUICPort(tc.ups, 784)
assert.Equal(t, tc.want, withPort)
@@ -395,6 +431,8 @@ func TestAddQUICPort(t *testing.T) {
}
func TestUpgradeSchema9to10(t *testing.T) {
t.Parallel()
const ultimateAns = 42
testCases := []struct {
@@ -427,7 +465,11 @@ func TestUpgradeSchema9to10(t *testing.T) {
"schema_version": 9,
}
t.Run(tc.name, func(t *testing.T) {
err := migrateTo10(conf)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo10(ctx, conf)
if tc.wantErr != "" {
testutil.AssertErrorMsg(t, tc.wantErr, err)
@@ -452,13 +494,21 @@ func TestUpgradeSchema9to10(t *testing.T) {
}
t.Run("no_dns", func(t *testing.T) {
err := migrateTo10(yobj{})
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo10(ctx, yobj{})
assert.NoError(t, err)
})
t.Run("bad_dns", func(t *testing.T) {
err := migrateTo10(yobj{
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo10(ctx, yobj{
"dns": ultimateAns,
})
@@ -467,10 +517,14 @@ func TestUpgradeSchema9to10(t *testing.T) {
}
func TestUpgradeSchema10to11(t *testing.T) {
t.Parallel()
check := func(t *testing.T, conf yobj) {
rlimit, _ := conf["rlimit_nofile"].(int)
err := migrateTo11(conf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo11(ctx, conf)
require.NoError(t, err)
require.Equal(t, conf["schema_version"], 11)
@@ -498,6 +552,8 @@ func TestUpgradeSchema10to11(t *testing.T) {
const rlimit = 42
t.Run("with_rlimit", func(t *testing.T) {
t.Parallel()
conf := yobj{
"rlimit_nofile": rlimit,
"schema_version": 10,
@@ -506,6 +562,8 @@ func TestUpgradeSchema10to11(t *testing.T) {
})
t.Run("without_rlimit", func(t *testing.T) {
t.Parallel()
conf := yobj{
"schema_version": 10,
}
@@ -514,6 +572,8 @@ func TestUpgradeSchema10to11(t *testing.T) {
}
func TestUpgradeSchema11to12(t *testing.T) {
t.Parallel()
testCases := []struct {
ivl any
want any
@@ -539,7 +599,11 @@ func TestUpgradeSchema11to12(t *testing.T) {
"schema_version": 11,
}
t.Run(tc.name, func(t *testing.T) {
err := migrateTo12(conf)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo12(ctx, conf)
if tc.wantErr != "" {
require.Error(t, err)
@@ -568,13 +632,21 @@ func TestUpgradeSchema11to12(t *testing.T) {
}
t.Run("no_dns", func(t *testing.T) {
err := migrateTo12(yobj{})
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo12(ctx, yobj{})
assert.NoError(t, err)
})
t.Run("bad_dns", func(t *testing.T) {
err := migrateTo12(yobj{
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo12(ctx, yobj{
"dns": 0,
})
@@ -582,11 +654,15 @@ func TestUpgradeSchema11to12(t *testing.T) {
})
t.Run("no_field", func(t *testing.T) {
t.Parallel()
conf := yobj{
"dns": yobj{},
}
err := migrateTo12(conf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo12(ctx, conf)
require.NoError(t, err)
dns, ok := conf["dns"]
@@ -609,6 +685,8 @@ func TestUpgradeSchema11to12(t *testing.T) {
}
func TestUpgradeSchema12to13(t *testing.T) {
t.Parallel()
const newSchemaVer = 13
testCases := []struct {
@@ -646,7 +724,11 @@ func TestUpgradeSchema12to13(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo13(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo13(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -655,6 +737,8 @@ func TestUpgradeSchema12to13(t *testing.T) {
}
func TestUpgradeSchema13to14(t *testing.T) {
t.Parallel()
const newSchemaVer = 14
testClient := yobj{
@@ -728,7 +812,11 @@ func TestUpgradeSchema13to14(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo14(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo14(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -737,6 +825,8 @@ func TestUpgradeSchema13to14(t *testing.T) {
}
func TestUpgradeSchema14to15(t *testing.T) {
t.Parallel()
const newSchemaVer = 15
defaultWantObj := yobj{
@@ -776,7 +866,11 @@ func TestUpgradeSchema14to15(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo15(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo15(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -785,6 +879,8 @@ func TestUpgradeSchema14to15(t *testing.T) {
}
func TestUpgradeSchema15to16(t *testing.T) {
t.Parallel()
const newSchemaVer = 16
defaultWantObj := yobj{
@@ -835,7 +931,11 @@ func TestUpgradeSchema15to16(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo16(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo16(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -844,6 +944,8 @@ func TestUpgradeSchema15to16(t *testing.T) {
}
func TestUpgradeSchema16to17(t *testing.T) {
t.Parallel()
const newSchemaVer = 17
defaultWantObj := yobj{
@@ -896,7 +998,11 @@ func TestUpgradeSchema16to17(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo17(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo17(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -905,6 +1011,8 @@ func TestUpgradeSchema16to17(t *testing.T) {
}
func TestUpgradeSchema17to18(t *testing.T) {
t.Parallel()
const newSchemaVer = 18
defaultWantObj := yobj{
@@ -955,7 +1063,11 @@ func TestUpgradeSchema17to18(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo18(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo18(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -964,6 +1076,8 @@ func TestUpgradeSchema17to18(t *testing.T) {
}
func TestUpgradeSchema18to19(t *testing.T) {
t.Parallel()
const newSchemaVer = 19
defaultWantObj := yobj{
@@ -1039,7 +1153,11 @@ func TestUpgradeSchema18to19(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo19(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo19(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -1048,6 +1166,8 @@ func TestUpgradeSchema18to19(t *testing.T) {
}
func TestUpgradeSchema19to20(t *testing.T) {
t.Parallel()
testCases := []struct {
ivl any
want any
@@ -1078,7 +1198,11 @@ func TestUpgradeSchema19to20(t *testing.T) {
"schema_version": 19,
}
t.Run(tc.name, func(t *testing.T) {
err := migrateTo20(conf)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo20(ctx, conf)
if tc.wantErr != "" {
require.Error(t, err)
@@ -1107,13 +1231,21 @@ func TestUpgradeSchema19to20(t *testing.T) {
}
t.Run("no_stats", func(t *testing.T) {
err := migrateTo20(yobj{})
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo20(ctx, yobj{})
assert.NoError(t, err)
})
t.Run("bad_stats", func(t *testing.T) {
err := migrateTo20(yobj{
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo20(ctx, yobj{
"statistics": 0,
})
@@ -1121,11 +1253,15 @@ func TestUpgradeSchema19to20(t *testing.T) {
})
t.Run("no_field", func(t *testing.T) {
t.Parallel()
conf := yobj{
"statistics": yobj{},
}
err := migrateTo20(conf)
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo20(ctx, conf)
require.NoError(t, err)
statsVal, ok := conf["statistics"]
@@ -1148,6 +1284,8 @@ func TestUpgradeSchema19to20(t *testing.T) {
}
func TestUpgradeSchema20to21(t *testing.T) {
t.Parallel()
const newSchemaVer = 21
testCases := []struct {
@@ -1182,7 +1320,11 @@ func TestUpgradeSchema20to21(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo21(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo21(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -1191,6 +1333,8 @@ func TestUpgradeSchema20to21(t *testing.T) {
}
func TestUpgradeSchema21to22(t *testing.T) {
t.Parallel()
const newSchemaVer = 22
testCases := []struct {
@@ -1252,7 +1396,11 @@ func TestUpgradeSchema21to22(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo22(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo22(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -1261,6 +1409,8 @@ func TestUpgradeSchema21to22(t *testing.T) {
}
func TestUpgradeSchema22to23(t *testing.T) {
t.Parallel()
const newSchemaVer = 23
testCases := []struct {
@@ -1305,7 +1455,11 @@ func TestUpgradeSchema22to23(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo23(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo23(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -1314,6 +1468,8 @@ func TestUpgradeSchema22to23(t *testing.T) {
}
func TestUpgradeSchema23to24(t *testing.T) {
t.Parallel()
const newSchemaVer = 24
testCases := []struct {
@@ -1372,7 +1528,11 @@ func TestUpgradeSchema23to24(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo24(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo24(ctx, tc.in)
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
assert.Equal(t, tc.want, tc.in)
@@ -1381,6 +1541,8 @@ func TestUpgradeSchema23to24(t *testing.T) {
}
func TestUpgradeSchema24to25(t *testing.T) {
t.Parallel()
const newSchemaVer = 25
testCases := []struct {
@@ -1459,7 +1621,11 @@ func TestUpgradeSchema24to25(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo25(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo25(ctx, tc.in)
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
assert.Equal(t, tc.want, tc.in)
@@ -1468,6 +1634,8 @@ func TestUpgradeSchema24to25(t *testing.T) {
}
func TestUpgradeSchema25to26(t *testing.T) {
t.Parallel()
const newSchemaVer = 26
testCases := []struct {
@@ -1558,7 +1726,11 @@ func TestUpgradeSchema25to26(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo26(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo26(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -1567,6 +1739,8 @@ func TestUpgradeSchema25to26(t *testing.T) {
}
func TestUpgradeSchema26to27(t *testing.T) {
t.Parallel()
const newSchemaVer = 27
testCases := []struct {
@@ -1641,7 +1815,11 @@ func TestUpgradeSchema26to27(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo27(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo27(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)
@@ -1650,6 +1828,8 @@ func TestUpgradeSchema26to27(t *testing.T) {
}
func TestUpgradeSchema27to28(t *testing.T) {
t.Parallel()
const newSchemaVer = 28
testCases := []struct {
@@ -1722,7 +1902,11 @@ func TestUpgradeSchema27to28(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo28(tc.in)
t.Parallel()
m := emptyMigrator()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := m.migrateTo28(ctx, tc.in)
require.NoError(t, err)
assert.Equal(t, tc.want, tc.in)

View File

@@ -2,14 +2,19 @@ package configmigrate
import (
"bytes"
"context"
"fmt"
"log/slog"
"github.com/AdguardTeam/golibs/log"
yaml "go.yaml.in/yaml/v4"
)
// Config is a the configuration for initializing a [Migrator].
type Config struct {
// Logger is used to log the operation of configuration migrator. It must
// not be nil.
Logger *slog.Logger
// WorkingDir is the absolute path to the working directory of AdGuardHome.
WorkingDir string
@@ -19,6 +24,7 @@ type Config struct {
// Migrator performs the YAML configuration file migrations.
type Migrator struct {
logger *slog.Logger
workingDir string
dataDir string
}
@@ -26,6 +32,7 @@ type Migrator struct {
// New creates a new Migrator.
func New(c *Config) (m *Migrator) {
return &Migrator{
logger: c.Logger,
workingDir: c.WorkingDir,
dataDir: c.DataDir,
}
@@ -35,7 +42,11 @@ func New(c *Config) (m *Migrator) {
// schema version, if needed. It returns the body of the upgraded config file,
// whether the file was upgraded, and an error, if any. If upgraded is false,
// the body is the same as the input.
func (m *Migrator) Migrate(body []byte, target uint) (newBody []byte, upgraded bool, err error) {
func (m *Migrator) Migrate(
ctx context.Context,
body []byte,
target uint,
) (newBody []byte, upgraded bool, err error) {
diskConf := yobj{}
err = yaml.Unmarshal(body, &diskConf)
if err != nil {
@@ -49,7 +60,7 @@ func (m *Migrator) Migrate(body []byte, target uint) (newBody []byte, upgraded b
}
current := uint(currentInt)
log.Debug("got schema version %v", current)
m.logger.DebugContext(ctx, "got", "schema_version", current)
if err = validateVersion(current, target); err != nil {
// Don't wrap the error, since it's informative enough as is.
@@ -58,7 +69,7 @@ func (m *Migrator) Migrate(body []byte, target uint) (newBody []byte, upgraded b
return body, false, nil
}
if err = m.upgradeConfigSchema(current, target, diskConf); err != nil {
if err = m.upgradeConfigSchema(ctx, current, target, diskConf); err != nil {
// Don't wrap the error, since it's informative enough as is.
return body, false, err
}
@@ -89,41 +100,45 @@ func validateVersion(current, target uint) (err error) {
}
// migrateFunc is a function that upgrades a config and returns an error.
type migrateFunc = func(diskConf yobj) (err error)
type migrateFunc = func(ctx context.Context, diskConf yobj) (err error)
// upgradeConfigSchema upgrades the configuration schema in diskConf from
// current to target version. current must be less than target, and both must
// be non-negative and less or equal to [LastSchemaVersion].
func (m *Migrator) upgradeConfigSchema(current, target uint, diskConf yobj) (err error) {
func (m *Migrator) upgradeConfigSchema(
ctx context.Context,
current, target uint,
diskConf yobj,
) (err error) {
upgrades := [LastSchemaVersion]migrateFunc{
0: m.migrateTo1,
1: m.migrateTo2,
2: migrateTo3,
3: migrateTo4,
4: migrateTo5,
5: migrateTo6,
6: migrateTo7,
7: migrateTo8,
8: migrateTo9,
9: migrateTo10,
10: migrateTo11,
11: migrateTo12,
12: migrateTo13,
13: migrateTo14,
14: migrateTo15,
15: migrateTo16,
16: migrateTo17,
17: migrateTo18,
18: migrateTo19,
19: migrateTo20,
20: migrateTo21,
21: migrateTo22,
22: migrateTo23,
23: migrateTo24,
24: migrateTo25,
25: migrateTo26,
26: migrateTo27,
27: migrateTo28,
2: m.migrateTo3,
3: m.migrateTo4,
4: m.migrateTo5,
5: m.migrateTo6,
6: m.migrateTo7,
7: m.migrateTo8,
8: m.migrateTo9,
9: m.migrateTo10,
10: m.migrateTo11,
11: m.migrateTo12,
12: m.migrateTo13,
13: m.migrateTo14,
14: m.migrateTo15,
15: m.migrateTo16,
16: m.migrateTo17,
17: m.migrateTo18,
18: m.migrateTo19,
19: m.migrateTo20,
20: m.migrateTo21,
21: m.migrateTo22,
22: m.migrateTo23,
23: m.migrateTo24,
24: m.migrateTo25,
25: m.migrateTo26,
26: m.migrateTo27,
27: m.migrateTo28,
28: m.migrateTo29,
29: m.migrateTo30,
}
@@ -132,9 +147,9 @@ func (m *Migrator) upgradeConfigSchema(current, target uint, diskConf yobj) (err
cur := current + uint(i)
next := current + uint(i) + 1
log.Printf("Upgrade yaml: %d to %d", cur, next)
m.logger.InfoContext(ctx, "upgrade yaml", "from", cur, "to", next)
if err = migrate(diskConf); err != nil {
if err = migrate(ctx, diskConf); err != nil {
return fmt.Errorf("migrating schema %d to %d: %w", cur, next, err)
}
}

View File

@@ -16,10 +16,6 @@ import (
"golang.org/x/crypto/bcrypt"
)
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
// testdata is a virtual filesystem containing test data.
var testdata = os.DirFS("testdata")
@@ -195,6 +191,10 @@ func TestMigrateConfig_Migrate(t *testing.T) {
yamlEqFunc: require.YAMLEq,
name: "v27",
targetVersion: 27,
}, {
yamlEqFunc: require.YAMLEq,
name: "v28",
targetVersion: 28,
}, {
yamlEqFunc: require.YAMLEq,
name: "v30",
@@ -212,10 +212,12 @@ func TestMigrateConfig_Migrate(t *testing.T) {
require.NoError(t, err)
migrator := configmigrate.New(&configmigrate.Config{
Logger: testLogger,
WorkingDir: t.Name(),
DataDir: filepath.Join(t.Name(), "data"),
})
newBody, upgraded, err := migrator.Migrate(body, tc.targetVersion)
ctx := testutil.ContextWithTimeout(t, testTimeout)
newBody, upgraded, err := migrator.Migrate(ctx, body, tc.targetVersion)
require.NoError(t, err)
require.True(t, upgraded)
@@ -226,6 +228,8 @@ func TestMigrateConfig_Migrate(t *testing.T) {
// TODO(a.garipov): Consider ways of merging into the previous one.
func TestMigrateConfig_Migrate_v29(t *testing.T) {
t.Parallel()
const (
pathUnix = `/path/to/file.txt`
userDirPatUnix = `TestMigrateConfig_Migrate/v29/data/userfilters/*`
@@ -253,11 +257,13 @@ func TestMigrateConfig_Migrate_v29(t *testing.T) {
wantBody = bytes.ReplaceAll(wantBody, []byte("USERFILTERSPATH"), []byte(patternToReplace))
migrator := configmigrate.New(&configmigrate.Config{
Logger: testLogger,
WorkingDir: t.Name(),
DataDir: "TestMigrateConfig_Migrate/v29/data",
})
newBody, upgraded, err := migrator.Migrate(body, 29)
ctx := testutil.ContextWithTimeout(t, testTimeout)
newBody, upgraded, err := migrator.Migrate(ctx, body, 29)
require.NoError(t, err)
require.True(t, upgraded)

View File

@@ -0,0 +1,124 @@
http:
address: 127.0.0.1:3000
session_ttl: 3h
pprof:
enabled: true
port: 6060
users:
- name: testuser
password: testpassword
dns:
bind_hosts:
- 127.0.0.1
port: 53
parental_sensitivity: 0
upstream_dns:
- tls://1.1.1.1
- tls://1.0.0.1
- quic://8.8.8.8:784
bootstrap_dns:
- 8.8.8.8:53
all_servers: true
edns_client_subnet:
enabled: true
use_custom: false
custom_ip: ""
filtering:
filtering_enabled: true
parental_enabled: false
safebrowsing_enabled: false
safe_search:
enabled: false
bing: true
duckduckgo: true
google: true
pixabay: true
yandex: true
youtube: true
protection_enabled: true
blocked_services:
schedule:
time_zone: Local
ids:
- 500px
blocked_response_ttl: 10
filters:
- url: https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt
name: ""
enabled: true
- url: https://adaway.org/hosts.txt
name: AdAway
enabled: false
- url: https://hosts-file.net/ad_servers.txt
name: hpHosts - Ad and Tracking servers only
enabled: false
- url: http://www.malwaredomainlist.com/hostslist/hosts.txt
name: MalwareDomainList.com Hosts List
enabled: false
clients:
persistent:
- name: localhost
ids:
- 127.0.0.1
- aa:aa:aa:aa:aa:aa
use_global_settings: true
use_global_blocked_services: true
filtering_enabled: false
parental_enabled: false
safebrowsing_enabled: false
safe_search:
enabled: true
bing: true
duckduckgo: true
google: true
pixabay: true
yandex: true
youtube: true
blocked_services:
schedule:
time_zone: Local
ids:
- 500px
runtime_sources:
whois: true
arp: true
rdns: true
dhcp: true
hosts: true
dhcp:
enabled: false
interface_name: vboxnet0
local_domain_name: local
dhcpv4:
gateway_ip: 192.168.0.1
subnet_mask: 255.255.255.0
range_start: 192.168.0.10
range_end: 192.168.0.250
lease_duration: 1234
icmp_timeout_msec: 10
schema_version: 27
user_rules: []
querylog:
enabled: true
file_enabled: true
interval: 720h
size_memory: 1000
ignored:
- '|.^'
statistics:
enabled: true
interval: 240h
ignored:
- '|.^'
os:
group: ''
rlimit_nofile: 123
user: ''
log:
file: ""
max_backups: 0
max_size: 100
max_age: 3
compress: true
local_time: false
verbose: true

View File

@@ -0,0 +1,124 @@
http:
address: 127.0.0.1:3000
session_ttl: 3h
pprof:
enabled: true
port: 6060
users:
- name: testuser
password: testpassword
dns:
bind_hosts:
- 127.0.0.1
port: 53
parental_sensitivity: 0
upstream_dns:
- tls://1.1.1.1
- tls://1.0.0.1
- quic://8.8.8.8:784
bootstrap_dns:
- 8.8.8.8:53
upstream_mode: parallel
edns_client_subnet:
enabled: true
use_custom: false
custom_ip: ""
filtering:
filtering_enabled: true
parental_enabled: false
safebrowsing_enabled: false
safe_search:
enabled: false
bing: true
duckduckgo: true
google: true
pixabay: true
yandex: true
youtube: true
protection_enabled: true
blocked_services:
schedule:
time_zone: Local
ids:
- 500px
blocked_response_ttl: 10
filters:
- url: https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt
name: ""
enabled: true
- url: https://adaway.org/hosts.txt
name: AdAway
enabled: false
- url: https://hosts-file.net/ad_servers.txt
name: hpHosts - Ad and Tracking servers only
enabled: false
- url: http://www.malwaredomainlist.com/hostslist/hosts.txt
name: MalwareDomainList.com Hosts List
enabled: false
clients:
persistent:
- name: localhost
ids:
- 127.0.0.1
- aa:aa:aa:aa:aa:aa
use_global_settings: true
use_global_blocked_services: true
filtering_enabled: false
parental_enabled: false
safebrowsing_enabled: false
safe_search:
enabled: true
bing: true
duckduckgo: true
google: true
pixabay: true
yandex: true
youtube: true
blocked_services:
schedule:
time_zone: Local
ids:
- 500px
runtime_sources:
whois: true
arp: true
rdns: true
dhcp: true
hosts: true
dhcp:
enabled: false
interface_name: vboxnet0
local_domain_name: local
dhcpv4:
gateway_ip: 192.168.0.1
subnet_mask: 255.255.255.0
range_start: 192.168.0.10
range_end: 192.168.0.250
lease_duration: 1234
icmp_timeout_msec: 10
schema_version: 28
user_rules: []
querylog:
enabled: true
file_enabled: true
interval: 720h
size_memory: 1000
ignored:
- '|.^'
statistics:
enabled: true
interval: 240h
ignored:
- '|.^'
os:
group: ''
rlimit_nofile: 123
user: ''
log:
file: ""
max_backups: 0
max_size: 100
max_age: 3
compress: true
local_time: false
verbose: true

View File

@@ -1,11 +1,12 @@
package configmigrate
import (
"context"
"os"
"path/filepath"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// migrateTo1 performs the following changes:
@@ -19,14 +20,14 @@ import (
//
// It also deletes the unused dnsfilter.txt file, since the following versions
// store filters in data/filters/.
func (m *Migrator) migrateTo1(diskConf yobj) (err error) {
func (m *Migrator) migrateTo1(ctx context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 1
dnsFilterPath := filepath.Join(m.workingDir, "dnsfilter.txt")
log.Printf("deleting %s as we don't need it anymore", dnsFilterPath)
m.logger.InfoContext(ctx, "deleting file as we do not need it anymore", "path", dnsFilterPath)
err = os.Remove(dnsFilterPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Info("warning: %s", err)
m.logger.InfoContext(ctx, "failed to delete", slogutil.KeyError, err)
// Go on.
}

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"fmt"
"net/url"
"strconv"
@@ -30,7 +31,7 @@ import (
// - 'quic://some-upstream.com:784'
// # …
// # …
func migrateTo10(diskConf yobj) (err error) {
func (m *Migrator) migrateTo10(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 10
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo11 performs the following changes:
//
// # BEFORE:
@@ -14,7 +16,7 @@ package configmigrate
// 'rlimit_nofile': 42
// 'user': ''
// # …
func migrateTo11(diskConf yobj) (err error) {
func (m *Migrator) migrateTo11(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 11
rlimit, _, err := fieldVal[int](diskConf, "rlimit_nofile")

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"time"
"github.com/AdguardTeam/golibs/timeutil"
@@ -17,7 +18,7 @@ import (
// 'schema_version': 12
// 'querylog_interval': '2160h'
// # …
func migrateTo12(diskConf yobj) (err error) {
func (m *Migrator) migrateTo12(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 12
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo13 performs the following changes:
//
// # BEFORE:
@@ -15,7 +17,7 @@ package configmigrate
// 'local_domain_name': 'lan'
// # …
// # …
func migrateTo13(diskConf yobj) (err error) {
func (m *Migrator) migrateTo13(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 13
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo14 performs the following changes:
//
// # BEFORE:
@@ -27,7 +29,7 @@ package configmigrate
// 'dhcp': true
// 'hosts': true
// # …
func migrateTo14(diskConf yobj) (err error) {
func (m *Migrator) migrateTo14(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 14
persistent, ok, err := fieldVal[yarr](diskConf, "clients")

View File

@@ -1,6 +1,10 @@
package configmigrate
import "github.com/AdguardTeam/golibs/errors"
import (
"context"
"github.com/AdguardTeam/golibs/errors"
)
// migrateTo15 performs the following changes:
//
@@ -28,7 +32,7 @@ import "github.com/AdguardTeam/golibs/errors"
// 'ignored': []
// # …
// # …
func migrateTo15(diskConf yobj) (err error) {
func (m *Migrator) migrateTo15(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 15
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo16 performs the following changes:
//
// # BEFORE:
@@ -43,7 +45,7 @@ package configmigrate
// 'ignored': []
// # …
// # …
func migrateTo16(diskConf yobj) (err error) {
func (m *Migrator) migrateTo16(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 16
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo17 performs the following changes:
//
// # BEFORE:
@@ -18,7 +20,7 @@ package configmigrate
// 'custom_ip': ""
// # …
// # …
func migrateTo17(diskConf yobj) (err error) {
func (m *Migrator) migrateTo17(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 17
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo18 performs the following changes:
//
// # BEFORE:
@@ -22,7 +24,7 @@ package configmigrate
// 'youtube': true
// # …
// # …
func migrateTo18(diskConf yobj) (err error) {
func (m *Migrator) migrateTo18(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 18
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,6 +1,10 @@
package configmigrate
import "github.com/AdguardTeam/golibs/log"
import (
"context"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// migrateTo19 performs the following changes:
//
@@ -30,7 +34,7 @@ import "github.com/AdguardTeam/golibs/log"
// # …
// # …
// # …
func migrateTo19(diskConf yobj) (err error) {
func (m *Migrator) migrateTo19(ctx context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 19
clients, ok, err := fieldVal[yobj](diskConf, "clients")
@@ -62,7 +66,7 @@ func migrateTo19(diskConf yobj) (err error) {
err = moveVal[bool](c, safeSearch, "safesearch_enabled", "enabled")
if err != nil {
log.Debug("migrating to version 19: %s", err)
m.logger.DebugContext(ctx, "migrating to", "version", 19, slogutil.KeyError, err)
}
c["safe_search"] = safeSearch

View File

@@ -1,11 +1,12 @@
package configmigrate
import (
"context"
"os"
"path/filepath"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// migrateTo2 performs the following changes:
@@ -21,14 +22,14 @@ import (
// # …
//
// It also deletes the Corefile file, since it isn't used anymore.
func (m *Migrator) migrateTo2(diskConf yobj) (err error) {
func (m *Migrator) migrateTo2(ctx context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 2
coreFilePath := filepath.Join(m.workingDir, "Corefile")
log.Printf("deleting %s as we don't need it anymore", coreFilePath)
m.logger.InfoContext(ctx, "deleting file as we do not need it anymore", "path", coreFilePath)
err = os.Remove(coreFilePath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Info("warning: %s", err)
m.logger.WarnContext(ctx, "failed to delete", slogutil.KeyError, err)
// Go on.
}

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"time"
"github.com/AdguardTeam/golibs/timeutil"
@@ -21,7 +22,7 @@ import (
// 'interval': 24h
// # …
// # …
func migrateTo20(diskConf yobj) (err error) {
func (m *Migrator) migrateTo20(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 20
stats, ok, err := fieldVal[yobj](diskConf, "statistics")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo21 performs the following changes:
//
// # BEFORE:
@@ -22,7 +24,7 @@ package configmigrate
// 'time_zone': 'Local'
// # …
// # …
func migrateTo21(diskConf yobj) (err error) {
func (m *Migrator) migrateTo21(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 21
const field = "blocked_services"

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"fmt"
)
@@ -30,7 +31,7 @@ import (
// # …
// # …
// # …
func migrateTo22(diskConf yobj) (err error) {
func (m *Migrator) migrateTo22(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 22
const field = "blocked_services"

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"fmt"
"net/netip"
"time"
@@ -23,7 +24,7 @@ import (
// 'address': '1.2.3.4:8080'
// 'session_ttl': '720h'
// # …
func migrateTo23(diskConf yobj) (err error) {
func (m *Migrator) migrateTo23(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 23
bindHost, ok, err := fieldVal[string](diskConf, "bind_host")

View File

@@ -1,6 +1,10 @@
package configmigrate
import "github.com/AdguardTeam/golibs/errors"
import (
"context"
"github.com/AdguardTeam/golibs/errors"
)
// migrateTo24 performs the following changes:
//
@@ -26,7 +30,7 @@ import "github.com/AdguardTeam/golibs/errors"
// 'local_time': false
// 'verbose': false
// # …
func migrateTo24(diskConf yobj) (err error) {
func (m *Migrator) migrateTo24(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 24
logObj := yobj{}

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo25 performs the following changes:
//
// # BEFORE:
@@ -14,7 +16,7 @@ package configmigrate
// 'enabled': true
// 'port': 6060
// # …
func migrateTo25(diskConf yobj) (err error) {
func (m *Migrator) migrateTo25(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 25
httpObj, ok, err := fieldVal[yobj](diskConf, "http")

View File

@@ -1,6 +1,10 @@
package configmigrate
import "github.com/AdguardTeam/golibs/errors"
import (
"context"
"github.com/AdguardTeam/golibs/errors"
)
// migrateTo26 performs the following changes:
//
@@ -71,7 +75,7 @@ import "github.com/AdguardTeam/golibs/errors"
// 'dns'
// # …
// # …
func migrateTo26(diskConf yobj) (err error) {
func (m *Migrator) migrateTo26(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 26
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo27 performs the following changes:
//
// # BEFORE:
@@ -27,7 +29,7 @@ package configmigrate
// - # …
// # …
// # …
func migrateTo27(diskConf yobj) (err error) {
func (m *Migrator) migrateTo27(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 27
keys := []string{"querylog", "statistics"}

View File

@@ -1,6 +1,8 @@
package configmigrate
import (
"context"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
)
@@ -18,7 +20,7 @@ import (
// 'upstream_mode': 'parallel'
// # …
// # …
func migrateTo28(diskConf yobj) (err error) {
func (m *Migrator) migrateTo28(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 28
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"fmt"
"path/filepath"
)
@@ -26,7 +27,7 @@ import (
// - '/opt/AdGuardHome/data/userfilters/*'
// - '/path/to/file.txt'
// # …
func (m Migrator) migrateTo29(diskConf yobj) (err error) {
func (m Migrator) migrateTo29(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 29
filterVals, ok, err := fieldVal[[]any](diskConf, "filters")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo3 performs the following changes:
//
// # BEFORE:
@@ -14,7 +16,7 @@ package configmigrate
// 'bootstrap_dns':
// - '1.1.1.1'
// # …
func migrateTo3(diskConf yobj) (err error) {
func (m *Migrator) migrateTo3(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 3
dnsConfig, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo30 performs the following changes:
//
// # BEFORE:
@@ -14,7 +16,7 @@ package configmigrate
// # …
//
// If cache_size is zero, then cache_enabled should be false.
func (m Migrator) migrateTo30(diskConf yobj) (err error) {
func (m Migrator) migrateTo30(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 30
dnsConf, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo4 performs the following changes:
//
// # BEFORE:
@@ -14,7 +16,7 @@ package configmigrate
// - 'use_global_blocked_services': true
// # …
// # …
func migrateTo4(diskConf yobj) (err error) {
func (m *Migrator) migrateTo4(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 4
clients, ok, _ := fieldVal[yarr](diskConf, "clients")

View File

@@ -1,6 +1,7 @@
package configmigrate
import (
"context"
"fmt"
"golang.org/x/crypto/bcrypt"
@@ -20,7 +21,7 @@ import (
// - 'name': …
// 'password': <hashed>
// # …
func migrateTo5(diskConf yobj) (err error) {
func (m *Migrator) migrateTo5(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 5
user := yobj{}

View File

@@ -1,6 +1,9 @@
package configmigrate
import "fmt"
import (
"context"
"fmt"
)
// migrateTo6 performs the following changes:
//
@@ -24,7 +27,7 @@ import "fmt"
// - 'AA:AA:AA:AA:AA:AA'
// # …
// # …
func migrateTo6(diskConf yobj) (err error) {
func (m *Migrator) migrateTo6(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 6
clients, ok, err := fieldVal[yarr](diskConf, "clients")

View File

@@ -1,6 +1,10 @@
package configmigrate
import "github.com/AdguardTeam/golibs/errors"
import (
"context"
"github.com/AdguardTeam/golibs/errors"
)
// migrateTo7 performs the following changes:
//
@@ -30,7 +34,7 @@ import "github.com/AdguardTeam/golibs/errors"
// 'lease_duration': 86400
// 'icmp_timeout_msec': 1000
// # …
func migrateTo7(diskConf yobj) (err error) {
func (m *Migrator) migrateTo7(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 7
dhcp, ok, _ := fieldVal[yobj](diskConf, "dhcp")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo8 performs the following changes:
//
// # BEFORE:
@@ -16,7 +18,7 @@ package configmigrate
// - '127.0.0.1'
// # …
// # …
func migrateTo8(diskConf yobj) (err error) {
func (m *Migrator) migrateTo8(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 8
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -1,5 +1,7 @@
package configmigrate
import "context"
// migrateTo9 performs the following changes:
//
// # BEFORE:
@@ -15,7 +17,7 @@ package configmigrate
// 'local_domain_name': 'lan'
// # …
// # …
func migrateTo9(diskConf yobj) (err error) {
func (m *Migrator) migrateTo9(_ context.Context, diskConf yobj) (err error) {
diskConf["schema_version"] = 9
dns, ok, err := fieldVal[yobj](diskConf, "dns")

View File

@@ -638,12 +638,14 @@ func parseConfig(ctx context.Context, l *slog.Logger) (err error) {
}
migrator := configmigrate.New(&configmigrate.Config{
Logger: l.With(slogutil.KeyPrefix, "config_migrator"),
WorkingDir: globalContext.workDir,
DataDir: globalContext.getDataDir(),
})
var upgraded bool
config.fileData, upgraded, err = migrator.Migrate(
ctx,
config.fileData,
configmigrate.LastSchemaVersion,
)