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
129
130
131
132
133
134
135
136
137
138
139
140
141
|
package configloader_test
import (
"os"
"path/filepath"
"testing"
"github.com/eugenetriguba/bolt/internal/bolttest"
"github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/checkmate/assert"
"github.com/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)
}
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_HOST")
bolttest.UnsetEnv(t, "BOLT_DB_PORT")
bolttest.UnsetEnv(t, "BOLT_DB_USER")
bolttest.UnsetEnv(t, "BOLT_DB_PASSWORD")
bolttest.UnsetEnv(t, "BOLT_DB_NAME")
bolttest.UnsetEnv(t, "BOLT_DB_DRIVER")
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{
Host: "testhost",
Port: "1234",
User: "testuser",
Password: "testpassword",
DBName: "testdb",
Driver: "postgresql",
},
}
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{
Host: "testhost",
Port: "1234",
User: "testuser",
Password: "testpassword",
DBName: "testdb",
Driver: "mysql",
},
}
bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml")
envCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "envmigrations",
VersionStyle: configloader.VersionStyleTimestamp,
},
Connection: configloader.ConnectionConfig{
Host: "envtesthost",
Port: "4321",
User: "envtestuser",
Password: "envtestpassword",
DBName: "envtestdb",
Driver: "postgresql",
},
}
t.Setenv("BOLT_MIGRATIONS_VERSION_STYLE", string(envCfg.Migrations.VersionStyle))
t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath)
t.Setenv("BOLT_DB_HOST", envCfg.Connection.Host)
t.Setenv("BOLT_DB_PORT", envCfg.Connection.Port)
t.Setenv("BOLT_DB_USER", envCfg.Connection.User)
t.Setenv("BOLT_DB_PASSWORD", envCfg.Connection.Password)
t.Setenv("BOLT_DB_NAME", envCfg.Connection.DBName)
t.Setenv("BOLT_DB_DRIVER", envCfg.Connection.Driver)
cfg, err := configloader.NewConfig()
assert.Nil(t, err)
assert.DeepEqual(t, *cfg, envCfg)
}
func TestNewConfigSearchesParentDirectories(t *testing.T) {
bolttest.UnsetEnv(t, "BOLT_DB_HOST")
bolttest.UnsetEnv(t, "BOLT_DB_PORT")
bolttest.UnsetEnv(t, "BOLT_DB_USER")
bolttest.UnsetEnv(t, "BOLT_DB_PASSWORD")
bolttest.UnsetEnv(t, "BOLT_DB_NAME")
bolttest.UnsetEnv(t, "BOLT_DB_DRIVER")
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,
},
}
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)
}
|