-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitschemalex_test.go
129 lines (105 loc) · 2.75 KB
/
gitschemalex_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
package gitschemalex
import (
"context"
"database/sql"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"testing"
_ "github.com/go-sql-driver/mysql"
"github.com/lestrrat/go-test-mysqld"
)
func TestRunner(t *testing.T) {
var dsn = "root:@tcp(127.0.0.1:3306)/mysql"
if ok, _ := strconv.ParseBool(os.Getenv("TRAVIS")); !ok {
mysqld, err := mysqltest.NewMysqld(nil)
if err != nil {
t.Fatal(err)
}
defer mysqld.Stop()
dsn = mysqld.DSN()
}
db, err := sql.Open("mysql", dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec("CREATE DATABASE IF NOT EXISTS `test`"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("USE `test`"); err != nil {
t.Fatal(err)
}
dir, err := ioutil.TempDir("", "gitschemalex")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "init").Run(); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "config", "user.email", "hoge@example.com").Run(); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "config", "user.name", "hoge").Run(); err != nil {
t.Fatal(err)
}
schema, err := os.Create(filepath.Join(dir, "schema.sql"))
if err != nil {
t.Fatal(err)
}
// first table
if _, err := schema.WriteString("CREATE TABLE hoge ( `id` INTEGER NOT NULL, `c` VARCHAR(20) );\n"); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "add", "schema.sql").Run(); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "commit", "-m", "initial commit").Run(); err != nil {
t.Fatal(err)
}
// This is a silly hack, but we need to change the DSN from "mysql" or
// whatever to "test"
re := regexp.MustCompile(`/[^/]+$`)
dsn = re.ReplaceAllString(dsn, `/test`)
r := &Runner{
Workspace: dir,
Deploy: true,
DSN: dsn,
Table: "git_schemalex_version",
Schema: "schema.sql",
}
if err := r.Run(context.TODO()); err != nil {
t.Fatal(err)
}
// deployed
if _, err := db.Exec("INSERT INTO `hoge` (`id`, `c`) VALUES (1, '2')"); err != nil {
t.Fatal(err)
}
// second table
if _, err := schema.WriteString("CREATE TABLE fuga ( `id` INTEGER NOT NULL, `c` VARCHAR(20) );\n"); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "add", "schema.sql").Run(); err != nil {
t.Fatal(err)
}
if err := exec.Command("git", "commit", "--author", "hoge <hoge@example.com>", "-m", "second commit").Run(); err != nil {
t.Fatal(err)
}
if err := r.Run(context.TODO()); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO `fuga` (`id`, `c`) VALUES (1, '2')"); err != nil {
t.Fatal(err)
}
// equal version
if err := r.Run(context.TODO()); err != ErrEqualVersion {
t.Fatal("should %v got %v", err, ErrEqualVersion)
}
}