-
Notifications
You must be signed in to change notification settings - Fork 522
/
fix_test.go
82 lines (71 loc) · 1.94 KB
/
fix_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
package goose
import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestFix(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skip long running test")
}
dir := t.TempDir()
defer os.Remove("./bin/fix-goose") // clean up
commands := []string{
"go build -o ./bin/fix-goose ./cmd/goose",
fmt.Sprintf("./bin/fix-goose -dir=%s create create_table", dir),
fmt.Sprintf("./bin/fix-goose -dir=%s create add_users", dir),
fmt.Sprintf("./bin/fix-goose -dir=%s create add_indices", dir),
fmt.Sprintf("./bin/fix-goose -dir=%s create update_users", dir),
fmt.Sprintf("./bin/fix-goose -dir=%s fix", dir),
}
for _, cmd := range commands {
args := strings.Split(cmd, " ")
time.Sleep(1 * time.Second)
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)
}
}
files, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
// check that the files are in order
for i, f := range files {
expected := fmt.Sprintf("%05v", i+1)
if !strings.HasPrefix(f.Name(), expected) {
t.Errorf("failed to find %s prefix in %s", expected, f.Name())
}
}
// add more migrations and then fix it
commands = []string{
fmt.Sprintf("./bin/fix-goose -dir=%s create remove_column", dir),
fmt.Sprintf("./bin/fix-goose -dir=%s create create_books_table", dir),
fmt.Sprintf("./bin/fix-goose -dir=%s fix", dir),
}
for _, cmd := range commands {
args := strings.Split(cmd, " ")
time.Sleep(1 * time.Second)
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
}
files, err = os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
// check that the files still in order
for i, f := range files {
expected := fmt.Sprintf("%05v", i+1)
if !strings.HasPrefix(f.Name(), expected) {
t.Errorf("failed to find %s prefix in %s", expected, f.Name())
}
}
}