-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_test.go
228 lines (179 loc) · 5.29 KB
/
mysql_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
package dbmate
import (
"database/sql"
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
func mySQLTestURL(t *testing.T) *url.URL {
u, err := url.Parse("mysql://root:root@mysql/dbmate")
require.Nil(t, err)
return u
}
func prepTestMySQLDB(t *testing.T) *sql.DB {
drv := MySQLDriver{}
u := mySQLTestURL(t)
// drop any existing database
err := drv.DropDatabase(u)
require.Nil(t, err)
// create database
err = drv.CreateDatabase(u)
require.Nil(t, err)
// connect database
db, err := drv.Open(u)
require.Nil(t, err)
return db
}
func TestNormalizeMySQLURLDefaults(t *testing.T) {
u, err := url.Parse("mysql://host/foo")
require.Nil(t, err)
require.Equal(t, "", u.Port())
s := normalizeMySQLURL(u)
require.Equal(t, "tcp(host:3306)/foo?multiStatements=true", s)
}
func TestNormalizeMySQLURLCustom(t *testing.T) {
u, err := url.Parse("mysql://bob:secret@host:123/foo?flag=on")
require.Nil(t, err)
require.Equal(t, "123", u.Port())
s := normalizeMySQLURL(u)
require.Equal(t, "bob:secret@tcp(host:123)/foo?flag=on&multiStatements=true", s)
}
func TestMySQLCreateDropDatabase(t *testing.T) {
drv := MySQLDriver{}
u := mySQLTestURL(t)
// drop any existing database
err := drv.DropDatabase(u)
require.Nil(t, err)
// create database
err = drv.CreateDatabase(u)
require.Nil(t, err)
// check that database exists and we can connect to it
func() {
db, err := drv.Open(u)
require.Nil(t, err)
defer mustClose(db)
err = db.Ping()
require.Nil(t, err)
}()
// drop the database
err = drv.DropDatabase(u)
require.Nil(t, err)
// check that database no longer exists
func() {
db, err := drv.Open(u)
require.Nil(t, err)
defer mustClose(db)
err = db.Ping()
require.NotNil(t, err)
require.Regexp(t, "Unknown database 'dbmate'", err.Error())
}()
}
func TestMySQLDatabaseExists(t *testing.T) {
drv := MySQLDriver{}
u := mySQLTestURL(t)
// drop any existing database
err := drv.DropDatabase(u)
require.Nil(t, err)
// DatabaseExists should return false
exists, err := drv.DatabaseExists(u)
require.Nil(t, err)
require.Equal(t, false, exists)
// create database
err = drv.CreateDatabase(u)
require.Nil(t, err)
// DatabaseExists should return true
exists, err = drv.DatabaseExists(u)
require.Nil(t, err)
require.Equal(t, true, exists)
}
func TestMySQLDatabaseExists_Error(t *testing.T) {
drv := MySQLDriver{}
u := mySQLTestURL(t)
u.User = url.User("invalid")
exists, err := drv.DatabaseExists(u)
require.Regexp(t, "Access denied for user 'invalid'@", err.Error())
require.Equal(t, false, exists)
}
func TestMySQLCreateMigrationsTable(t *testing.T) {
drv := MySQLDriver{}
db := prepTestMySQLDB(t)
defer mustClose(db)
// migrations table should not exist
count := 0
err := db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Regexp(t, "Table 'dbmate.schema_migrations' doesn't exist", err.Error())
// create table
err = drv.CreateMigrationsTable(db)
require.Nil(t, err)
// migrations table should exist
err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
// create table should be idempotent
err = drv.CreateMigrationsTable(db)
require.Nil(t, err)
}
func TestMySQLSelectMigrations(t *testing.T) {
drv := MySQLDriver{}
db := prepTestMySQLDB(t)
defer mustClose(db)
err := drv.CreateMigrationsTable(db)
require.Nil(t, err)
_, err = db.Exec(`insert into schema_migrations (version)
values ('abc2'), ('abc1'), ('abc3')`)
require.Nil(t, err)
migrations, err := drv.SelectMigrations(db, -1, "default")
require.Nil(t, err)
require.Equal(t, true, migrations["abc1"])
require.Equal(t, true, migrations["abc2"])
require.Equal(t, true, migrations["abc2"])
// test limit param
migrations, err = drv.SelectMigrations(db, 1, "default")
require.Nil(t, err)
require.Equal(t, true, migrations["abc3"])
require.Equal(t, false, migrations["abc1"])
require.Equal(t, false, migrations["abc2"])
// test different project
_, err = db.Exec(`insert into schema_migrations (version, project)
values ('bcd1', 'app2')`)
require.Nil(t, err)
migrations, err = drv.SelectMigrations(db, -1, "app2")
require.Nil(t, err)
require.Equal(t, true, migrations["bcd1"])
require.Equal(t, false, migrations["abc1"])
require.Equal(t, false, migrations["abc2"])
require.Equal(t, false, migrations["abc3"])
}
func TestMySQLInsertMigration(t *testing.T) {
drv := MySQLDriver{}
db := prepTestMySQLDB(t)
defer mustClose(db)
err := drv.CreateMigrationsTable(db)
require.Nil(t, err)
count := 0
err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
require.Equal(t, 0, count)
// insert migration
err = drv.InsertMigration(db, "abc1", "default")
require.Nil(t, err)
err = db.QueryRow("select count(*) from schema_migrations where version = 'abc1'").
Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}
func TestMySQLDeleteMigration(t *testing.T) {
drv := MySQLDriver{}
db := prepTestMySQLDB(t)
defer mustClose(db)
err := drv.CreateMigrationsTable(db)
require.Nil(t, err)
_, err = db.Exec(`insert into schema_migrations (version)
values ('abc1'), ('abc2')`)
require.Nil(t, err)
err = drv.DeleteMigration(db, "abc2")
require.Nil(t, err)
count := 0
err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}