1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package configloader_test
import (
"os"
"path/filepath"
"testing"
"git.sr.ht/~eugenetriguba/bolt/internal/bolttest"
"git.sr.ht/~eugenetriguba/bolt/internal/configloader"
"git.sr.ht/~eugenetriguba/checkmate/assert"
"git.sr.ht/~eugenetriguba/checkmate/check"
)
func TestNewConfigDefaults(t *testing.T) {
bolttest.ChangeCwd(t, os.TempDir())
cfg, err := configloader.NewConfig()
assert.Nil(t, err)
check.Equal(t, cfg.Migrations.DirectoryPath, "migrations")
check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleTimestamp)
check.Equal(t, cfg.Connection.MigrationsTable, "bolt_migrations")
}
func TestNewConfigWithInvalidVersionStyle(t *testing.T) {
fileCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "myfancymigrations",
VersionStyle: "invalid",
},
}
bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml")
_, err := configloader.NewConfig()
assert.ErrorIs(t, err, configloader.ErrInvalidVersionStyle)
}
func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
bolttest.UnsetEnv(t, "BOLT_DB_DSN")
bolttest.UnsetEnv(t, "BOLT_DB_DRIVER")
bolttest.UnsetEnv(t, "BOLT_DB_MIGRATIONS_TABLE")
bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH")
bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE")
expectedCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "myfancymigrations",
VersionStyle: configloader.VersionStyleSequential,
},
Connection: configloader.ConnectionConfig{
DSN: "postgresql://testuser:testpassword@testhost:1234/testdb",
Driver: "postgresql",
MigrationsTable: "test_table",
},
}
tmpdir := t.TempDir()
bolttest.ChangeCwd(t, tmpdir)
bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml"))
cfg, err := configloader.NewConfig()
assert.Nil(t, err)
assert.DeepEqual(t, *cfg, expectedCfg)
}
func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
fileCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "cfgmigrations",
VersionStyle: configloader.VersionStyleSequential,
},
Connection: configloader.ConnectionConfig{
DSN: "mysql://testuser:testpassword@testhost:1234/testdb",
Driver: "mysql",
MigrationsTable: "test_table",
},
}
bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml")
envCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "envmigrations",
VersionStyle: configloader.VersionStyleTimestamp,
},
Connection: configloader.ConnectionConfig{
DSN: "postgresql://envtestuser:envtestpassword@envtesthost:4321/envtestdb",
Driver: "postgresql",
MigrationsTable: "different_table",
},
}
t.Setenv("BOLT_MIGRATIONS_VERSION_STYLE", string(envCfg.Migrations.VersionStyle))
t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath)
t.Setenv("BOLT_DB_DSN", envCfg.Connection.DSN)
t.Setenv("BOLT_DB_DRIVER", envCfg.Connection.Driver)
t.Setenv("BOLT_DB_MIGRATIONS_TABLE", envCfg.Connection.MigrationsTable)
cfg, err := configloader.NewConfig()
assert.Nil(t, err)
assert.DeepEqual(t, *cfg, envCfg)
}
func TestNewConfigSearchesParentDirectories(t *testing.T) {
bolttest.UnsetEnv(t, "BOLT_DB_DSN")
bolttest.UnsetEnv(t, "BOLT_DB_DRIVER")
bolttest.UnsetEnv(t, "BOLT_DB_MIGRATIONS_TABLE")
bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH")
bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE")
expectedCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "differentmigrationsdir",
VersionStyle: configloader.VersionStyleSequential,
},
Connection: configloader.ConnectionConfig{
MigrationsTable: "migration_table",
},
}
tmpdir := t.TempDir()
bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml"))
nestedTmpDir := filepath.Join(tmpdir, "nested-dir", "nested-x2-dir")
err := os.MkdirAll(nestedTmpDir, 0755)
assert.Nil(t, err)
bolttest.ChangeCwd(t, nestedTmpDir)
cfg, err := configloader.NewConfig()
assert.Nil(t, err)
assert.DeepEqual(t, *cfg, expectedCfg)
}
|