This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goose_test.go
330 lines (287 loc) · 8.36 KB
/
goose_test.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package goose
import (
"database/sql"
"embed"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/pressly/goose/v3/internal/check"
_ "modernc.org/sqlite"
)
const (
binName = "goose-test"
)
func TestMain(m *testing.M) {
if runtime.GOOS == "windows" {
log.Fatalf("this test is not supported on Windows")
}
dir, err := os.Getwd()
if err != nil {
log.Fatalf("%v", err)
}
args := []string{
"build",
"-ldflags=-s -w",
// disable all drivers except sqlite3
"-tags=no_clickhouse no_mssql no_mysql no_vertica no_postgres",
"-o", binName,
"./cmd/goose",
}
build := exec.Command("go", args...)
out, err := build.CombinedOutput()
if err != nil {
log.Fatalf("failed to build %s binary: %v: %s", binName, err, string(out))
}
result := m.Run()
defer func() { os.Exit(result) }()
if err := os.Remove(filepath.Join(dir, binName)); err != nil {
log.Printf("failed to remove binary: %v", err)
}
}
func TestDefaultBinary(t *testing.T) {
t.Parallel()
commands := []string{
"go build -o ./bin/goose ./cmd/goose",
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db up",
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db version",
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db down",
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db status",
"./bin/goose --version",
}
t.Cleanup(func() {
if err := os.Remove("./bin/goose"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
if err := os.Remove("./sql.db"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
})
for _, cmd := range commands {
args := strings.Split(cmd, " ")
command := args[0]
var params []string
if len(args) > 1 {
params = args[1:]
}
cmd := exec.Command(command, params...)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
}
}
func TestIssue532(t *testing.T) {
t.Parallel()
migrationsDir := filepath.Join("examples", "sql-migrations")
count := countSQLFiles(t, migrationsDir)
check.Number(t, count, 3)
tempDir := t.TempDir()
dirFlag := "--dir=" + migrationsDir
tt := []struct {
command string
output string
}{
{"up", ""},
{"up", "goose: no migrations to run. current version: 3"},
{"version", "goose: version 3"},
}
for _, tc := range tt {
params := []string{dirFlag, "sqlite3", filepath.Join(tempDir, "sql.db"), tc.command}
got, err := runGoose(params...)
check.NoError(t, err)
if tc.output == "" {
continue
}
if !strings.Contains(strings.TrimSpace(got), tc.output) {
t.Logf("output mismatch for command: %q", tc.command)
t.Logf("got\n%s", strings.TrimSpace(got))
t.Log("====")
t.Logf("want\n%s", tc.output)
t.FailNow()
}
}
}
func TestIssue293(t *testing.T) {
t.Parallel()
// https://github.com/pressly/goose/issues/293
commands := []string{
"go build -o ./bin/goose293 ./cmd/goose",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db up",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db version",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db down",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db down",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db version",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db up",
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db status",
}
t.Cleanup(func() {
if err := os.Remove("./bin/goose293"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
if err := os.Remove("./issue_293.db"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
})
for _, cmd := range commands {
args := strings.Split(cmd, " ")
command := args[0]
var params []string
if len(args) > 1 {
params = args[1:]
}
cmd := exec.Command(command, params...)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
}
}
func TestIssue336(t *testing.T) {
t.Parallel()
// error when no migrations are found
// https://github.com/pressly/goose/issues/336
tempDir := t.TempDir()
params := []string{"--dir=" + tempDir, "sqlite3", filepath.Join(tempDir, "sql.db"), "up"}
_, err := runGoose(params...)
check.HasError(t, err)
check.Contains(t, err.Error(), "no migration files found")
}
func TestLiteBinary(t *testing.T) {
t.Parallel()
dir := t.TempDir()
t.Cleanup(func() {
if err := os.Remove("./bin/lite-goose"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
})
// this has to be done outside of the loop
// since go only supports space separated tags list.
cmd := exec.Command("go", "build", "-tags='no_postgres no_mysql no_sqlite3'", "-o", "./bin/lite-goose", "./cmd/goose")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
commands := []string{
fmt.Sprintf("./bin/lite-goose -dir=%s create user_indices sql", dir),
fmt.Sprintf("./bin/lite-goose -dir=%s fix", dir),
}
for _, cmd := range commands {
args := strings.Split(cmd, " ")
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
}
}
func TestCustomBinary(t *testing.T) {
t.Parallel()
commands := []string{
"go build -o ./bin/custom-goose ./examples/go-migrations",
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db up",
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db version",
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db down",
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db status",
}
t.Cleanup(func() {
if err := os.Remove("./go.db"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
})
for _, cmd := range commands {
args := strings.Split(cmd, " ")
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
}
}
//go:embed examples/sql-migrations/*.sql
var migrations embed.FS
func TestEmbeddedMigrations(t *testing.T) {
// not using t.Parallel here to avoid races
db, err := sql.Open("sqlite", "sql_embed.db")
if err != nil {
t.Fatalf("Database open failed: %s", err)
}
t.Cleanup(func() {
if err := os.Remove("./sql_embed.db"); err != nil {
t.Logf("failed to remove %s resources: %v", t.Name(), err)
}
})
db.SetMaxOpenConns(1)
// decouple from existing structure
fsys, err := fs.Sub(migrations, "examples/sql-migrations")
if err != nil {
t.Fatalf("SubFS make failed: %s", err)
}
SetBaseFS(fsys)
check.NoError(t, SetDialect("sqlite3"))
t.Cleanup(func() { SetBaseFS(nil) })
t.Run("Migration cycle", func(t *testing.T) {
if err := Up(db, "."); err != nil {
t.Errorf("Failed to run 'up' migrations: %s", err)
}
ver, err := GetDBVersion(db)
if err != nil {
t.Fatalf("Failed to get migrations version: %s", err)
}
if ver != 3 {
t.Errorf("Expected version 3 after 'up', got %d", ver)
}
if err := Reset(db, "."); err != nil {
t.Errorf("Failed to run 'down' migrations: %s", err)
}
ver, err = GetDBVersion(db)
if err != nil {
t.Fatalf("Failed to get migrations version: %s", err)
}
if ver != 0 {
t.Errorf("Expected version 0 after 'reset', got %d", ver)
}
})
t.Run("Create uses os fs", func(t *testing.T) {
tmpDir := t.TempDir()
if err := Create(db, tmpDir, "test", "sql"); err != nil {
t.Errorf("Failed to create migration: %s", err)
}
paths, _ := filepath.Glob(filepath.Join(tmpDir, "*test.sql"))
if len(paths) == 0 {
t.Errorf("Failed to find created migration")
}
if err := Fix(tmpDir); err != nil {
t.Errorf("Failed to 'fix' migrations: %s", err)
}
_, err = os.Stat(filepath.Join(tmpDir, "00001_test.sql"))
if err != nil {
t.Errorf("Failed to locate fixed migration: %s", err)
}
})
}
func runGoose(params ...string) (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
cmdPath := filepath.Join(dir, binName)
cmd := exec.Command(cmdPath, params...)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v\n%v", err, string(out))
}
return string(out), nil
}
func countSQLFiles(t *testing.T, dir string) int {
t.Helper()
files, err := filepath.Glob(filepath.Join(dir, "*.sql"))
check.NoError(t, err)
return len(files)
}