-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlxm_test.go
512 lines (452 loc) · 11.2 KB
/
sqlxm_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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package sqlxm
import (
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/danielmorell/sqlxm/backends"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
_ "modernc.org/sqlite"
)
// Test Types / Fixtures -------------------------------------------------------
// backends.Backend implementation
type back struct {
db *sqlx.DB
table string
tableSchema string
}
func (b *back) Setup(db *sqlx.DB, table string, tableSchema string) {
}
func (b *back) InsertRecord(tx *sqlx.Tx, name string, hash string, comment string) error {
return nil
}
func (b *back) HasMigrationTable() (bool, error) {
return false, nil
}
func (b *back) QueryPrevious() (map[string]string, error) {
return make(map[string]string), nil
}
func (b *back) CreateMigrationTable() (string, error) {
return "", nil
}
func (b *back) RepairHashes(tx *sqlx.Tx, hashes map[string]string) error {
return nil
}
type testDBMS struct {
title string
name string
tableSchema string
}
// Unit Tests ------------------------------------------------------------------
func TestBackendType(t *testing.T) {
// 0: backend, 1: driver
drivers := [][2]string{
{"postgres", "postgres"},
{"postgres", "cockroach"},
{"mysql", "mysql"},
{"sqlite", "sqlite3"},
{"oracle", "godror"},
{"sqlserver", "sqlserver"},
}
t.Run("KnownBackends", func(t *testing.T) {
for i, d := range drivers {
backend := BackendType(d[1])
if backend != d[0] {
t.Errorf("backend type #%d incorrect expected '%s', actuall '%s'", i, d[0], backend)
}
}
})
t.Run("UnknownBackend", func(t *testing.T) {
backend := BackendType("not-a-real-driver")
if backend != "unknown" {
t.Error("backend should return 'unknown'")
}
})
}
func TestRegisterBackend(t *testing.T) {
t.Run("ExistingBackend", func(t *testing.T) {
err := RegisterBackend("postgres", &back{})
if err == nil {
t.Error("backend exists: an error should be returned")
}
})
t.Run("NewBackend", func(t *testing.T) {
err := RegisterBackend("mydb", &back{})
if err != nil {
t.Error("backend does not exist: an error should not be returned")
}
})
}
// End-to-End Tests ------------------------------------------------------------
func dropTables(db *sqlx.DB, tables []string) {
for _, t := range tables {
db.Exec(fmt.Sprintf("DROP TABLE %s;", t))
}
}
func getEnv() map[string]string {
var env map[string]string
env, err := godotenv.Read()
if err != nil {
panic(err)
}
return env
}
func postgresDSN(env map[string]string) string {
return fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
env["POSTGRES_HOST"],
env["POSTGRES_PORT"],
env["POSTGRES_USER"],
env["POSTGRES_PASSWORD"],
env["POSTGRES_DB"],
)
}
func mysqlDSN(env map[string]string) string {
// username:password@protocol(address)/dbname?param=value
return fmt.Sprintf(
"%s:%s@(%s:%s)/%s",
env["MYSQL_USER"],
env["MYSQL_PASSWORD"],
env["MYSQL_HOST"],
env["MYSQL_PORT"],
env["MYSQL_DB"],
)
}
func connectToDB(dbms string) (*sqlx.DB, func(drop ...string)) {
env := getEnv()
var sourceData string
switch dbms {
case "mysql":
sourceData = mysqlDSN(env)
case "postgres":
sourceData = postgresDSN(env)
case "sqlite":
sourceData = env["SQLITE_PATH"]
}
db, err := sqlx.Open(dbms, sourceData)
if err != nil {
log.Fatalf("DB connection %s: %s\n", dbms, err)
}
return db, func(drop ...string) {
dropTables(db, drop)
db.Close()
if dbms == "sqlite" {
os.Remove(env["SQLITE_PATH"])
}
}
}
func TestMainE2E(t *testing.T) {
env := getEnv()
dbms := []testDBMS{
{
title: "MySQL",
name: "mysql",
tableSchema: env["MYSQL_DB"],
},
{
title: "Postgres",
name: "postgres",
tableSchema: "public",
},
{
title: "SQLite",
name: "sqlite",
tableSchema: "",
},
}
for _, d := range dbms {
t.Run(fmt.Sprintf("%sTestE2E", d.title), func(t *testing.T) {
testE2E(t, d)
})
t.Run(fmt.Sprintf("%stestSafe", d.title), func(t *testing.T) {
testSafe(t, d)
})
t.Run(fmt.Sprintf("%stestDuplicate", d.title), func(t *testing.T) {
testDuplicate(t, d)
})
t.Run(fmt.Sprintf("%stestUseBackend", d.title), func(t *testing.T) {
testUseBackend(t, d)
})
}
}
func testE2E(t *testing.T, dbms testDBMS) {
db, done := connectToDB(dbms.name)
defer done("migrations", "users")
migrator, err := New(db, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
err = migrator.AddMigration(
"create_user_table",
"Add the initial user table",
// language=SQL
`CREATE TABLE users (
id INT,
name VARCHAR(64) NOT NULL,
password VARCHAR(128) NOT NULL
);`,
)
if err != nil {
t.Error(err)
}
err = migrator.AddMigration(
"add_user_username_column",
"Add the user username column",
// language=SQL
`ALTER TABLE users ADD username VARCHAR(64) NOT NULL;`,
)
if err != nil {
t.Error(err)
}
err = migrator.AddMigration(
"add_user_username_uindex",
"Unique index the users table username column",
// language=SQL
`CREATE UNIQUE INDEX users_username_uindex ON users (username);`,
)
if err != nil {
t.Error(err)
}
l, err := migrator.Run()
if err != nil {
t.Errorf("migrator run error: %s", err)
}
t.Run("MigrationLogCount", func(t *testing.T) {
if len(l) != 4 {
t.Errorf("migration log count incorrect: expcted '4', got '%d'", len(l))
}
})
// Make sure user table exists.
t.Run("UserTableCreated", func(t *testing.T) {
exists := false
if dbms.name == "sqlite" {
err = db.Get(&exists, `SELECT count(name)
FROM sqlite_master
WHERE type='table'
AND name = 'users';`)
} else {
err = db.Get(&exists, fmt.Sprintf(`SELECT EXISTS(
SELECT * FROM information_schema.tables
WHERE table_schema = '%s'
AND table_name = 'migrations'
);`, dbms.tableSchema))
}
if err != nil {
t.Error("error querying user table")
}
if !exists {
t.Error("'users' table was not created.")
}
})
// Make sure user table has the right columns
t.Run("UserTableColumnsCorrect", func(t *testing.T) {
cols := make([]string, 0, 4)
if dbms.name == "sqlite" {
err = db.Select(&cols, `SELECT name as column_name FROM pragma_table_info(('users'));`)
} else {
err = db.Select(&cols, fmt.Sprintf(`SELECT column_name
FROM information_schema.columns
WHERE table_schema = '%s'
AND table_name = 'users';`, dbms.tableSchema))
}
if err != nil {
t.Error("error querying user table columns")
}
expectedCols := map[string]bool{
"id": false,
"name": false,
"password": false,
"username": false,
}
for _, col := range cols {
_, exists := expectedCols[col]
if !exists {
t.Errorf("unexpected column '%s'", col)
continue
}
expectedCols[col] = true
}
for col, exists := range expectedCols {
if !exists {
t.Errorf("column not created '%s'", col)
}
}
})
}
// Test to make sure that migrations with the same name will
func testSafe(t *testing.T, dbms testDBMS) {
pg, done := connectToDB(dbms.name)
defer done("migrations", "users")
migrator1, err := New(pg, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
err = migrator1.AddMigration(
"create_user_table",
"Add the initial user table",
// language=PostgreSQL
`CREATE TABLE users (
id INT,
name VARCHAR(64) NOT NULL,
password VARCHAR(128) NOT NULL
);`,
)
if err != nil {
t.Error(err)
}
_, err = migrator1.Run()
if err != nil {
t.Errorf("migrator run error: %s", err)
}
// In safe mode the second run will return an error.
t.Run("RunSafe", func(t *testing.T) {
migrator2, err := New(pg, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
err = migrator2.AddMigration(
"create_user_table",
"Add the initial user table",
// language=PostgreSQL
`CREATE TABLE users (
id INT,
name VARCHAR(64) NOT NULL,
password VARCHAR(128) NOT NULL
);`,
)
if err != nil {
t.Error(err)
}
l, err := migrator2.Run()
if err == nil || ERROR_HASH != l[len(l)-1].Status {
t.Error("migrator run safe error: hash mismatch check failed")
}
})
t.Run("RunLoose", func(t *testing.T) {
migrator3, err := New(pg, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
err = migrator3.AddMigration(
"create_user_table",
"Add the initial user table",
// language=PostgreSQL
`CREATE TABLE users (
id INT,
name VARCHAR(64) NOT NULL,
password VARCHAR(128) NOT NULL
);`,
)
if err != nil {
t.Error(err)
}
l, err := migrator3.RunUnsafe()
lastLog := l[len(l)-1]
if err != nil {
t.Error("migrator run loose error: run failed")
}
if PREVIOUS != lastLog.Status {
t.Error("migrator run loose error: previous status not set")
}
if !strings.Contains(lastLog.Details, "hash mismatch") {
t.Error("migrator run loose error: hash message missing")
}
})
t.Run("RepairHash", func(t *testing.T) {
migrator4, err := New(pg, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
err = migrator4.AddMigration(
"create_user_table",
"Add the initial user table",
// language=PostgreSQL
`CREATE TABLE users (
id INT,
name VARCHAR(64) NOT NULL,
password VARCHAR(128) NOT NULL
);`,
)
if err != nil {
t.Error(err)
}
migrator4.RepairHash("create_user_table")
l, err := migrator4.Run()
var lastLog = MigrationLog{}
if len(l) > 0 {
lastLog = l[len(l)-1]
}
if err != nil {
t.Error("migrator run hash repair error: run failed")
}
if PREVIOUS != lastLog.Status {
t.Error("migrator run hash repair error: previous status not set")
}
})
}
// Test to make sure that migrations with the same name will fail
func testDuplicate(t *testing.T, dbms testDBMS) {
pg, done := connectToDB(dbms.name)
defer done()
migrator, err := New(pg, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
err = migrator.AddMigration("foo", "Foo", "")
if err != nil {
t.Errorf("add migration failed: %s", err)
}
err = migrator.AddMigration("bar", "Bar", "")
if err != nil {
t.Errorf("add migration failed: %s", err)
}
// This should return an error since the name already exists.
err = migrator.AddMigration("foo", "Foo", "")
if err == nil {
t.Errorf("add migration succeeded: %s", err)
}
}
func testUseBackend(t *testing.T, dbms testDBMS) {
pg, done := connectToDB(dbms.name)
defer done("migrations", "users")
// For simplicity, we will just register the Postgres backend under a
// different name.
err := RegisterBackend("mydb", &backends.Postgres{})
migrator, err := New(pg, "migrations", dbms.tableSchema)
if err != nil {
t.Error(err)
}
t.Run("UnknownBackend", func(t *testing.T) {
err = migrator.UseBackend("nope")
if err == nil {
t.Error("backend should not exist")
}
})
t.Run("KnownBackend", func(t *testing.T) {
err = migrator.UseBackend("mydb")
if err != nil {
t.Error(err)
}
err = migrator.AddMigration(
"create_user_table",
"Add the initial user table",
// language=PostgreSQL
`CREATE TABLE users (
id INT,
name VARCHAR(64) NOT NULL,
password VARCHAR(128) NOT NULL
);`,
)
if err != nil {
t.Error(err)
}
_, err = migrator.Run()
if err != nil {
t.Errorf("migrator run error: %s", err)
}
})
}