-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbolve_test.go
252 lines (218 loc) · 6.2 KB
/
dbolve_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
package dbolve
import (
"database/sql"
"fmt"
"log/slog"
"os"
"strings"
"testing"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
type dbCredentials struct {
driver string
user string
password string
host string
dbname string
}
func (c *dbCredentials) connectDB(t *testing.T) *sql.DB {
var url string
switch c.driver {
case "postgres":
url = fmt.Sprintf("%s://%s:%s@%s/%s?sslmode=disable", c.driver, c.user, c.password, c.host, c.dbname)
case "mysql":
url = fmt.Sprintf("%s:%s@tcp(%s)/%s?autocommit=false", c.user, c.password, c.host, c.dbname)
case "sqlite3":
url = fmt.Sprintf("file:%s", c.dbname)
default:
t.Fail()
}
db, err := sql.Open(c.driver, url)
if err != nil {
t.Error(err)
t.Fail()
}
return db
}
func CleanDB(t *testing.T, creds dbCredentials) *sql.DB {
switch creds.driver {
case "sqlite3":
os.Remove(creds.dbname + ".db")
return creds.connectDB(t)
default:
db := creds.connectDB(t)
_, err := db.Exec("DROP DATABASE IF EXISTS dbolve_test;")
if err != nil {
t.Error(err)
t.Fail()
}
_, err = db.Exec("CREATE DATABASE dbolve_test;")
if err != nil {
t.Error(err)
t.Fail()
}
db.Close()
creds.dbname = "dbolve_test"
db = creds.connectDB(t)
return db
}
}
func TestPostgres(t *testing.T) {
testWithDB(t, dbCredentials{driver: "postgres", user: "postgres", password: "postgres", host: "localhost"})
}
func TestMySQL(t *testing.T) {
testWithDB(t, dbCredentials{driver: "mysql", user: "root", password: "mysql", host: "localhost"})
}
func TestSQLite(t *testing.T) {
testWithDB(t, dbCredentials{driver: "sqlite3"})
}
func testWithDB(t *testing.T, creds dbCredentials) {
db := CleanDB(t, creds)
testEvolution(db, t)
_ = db.Close()
db = CleanDB(t, creds)
testModifiedMigration(db, t)
_ = db.Close()
db = CleanDB(t, creds)
testFailingMigration(db, t)
_ = db.Close()
if creds.driver != "sqlite3" {
db, _ = sql.Open(creds.driver, "some invalid")
if m, err := NewMigrator(db, make([]Migration, 0), slog.Default()); err == nil || m != nil {
t.Errorf("Invalid database should throw an error")
}
}
}
func testEvolution(db *sql.DB, t *testing.T) {
migrations := []Migration{
{
Name: "First migration",
Code: func(tx Transaction) error {
return tx.Exec(`CREATE TABLE account(
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);`)
},
},
}
m, err := NewMigrator(db, migrations, slog.Default())
if err != nil {
t.Error(err)
}
if err := m.DryRun(); err != nil {
t.Error(err)
}
if len(m.Applied()) != 0 {
t.Errorf("At the beginning, there should not be applied migrations")
}
if len(m.Pending()) != len(migrations) {
t.Errorf("At the beginning, there should be all migrations pending")
}
err = m.Migrate()
if err != nil {
t.Error(err)
}
if len(m.Applied()) != len(migrations) {
t.Errorf("After migration, all migrations should be applied")
}
if len(m.Pending()) != 0 {
t.Errorf("At migration, there should be no pending migrations")
}
migrations = append(migrations, Migration{
Name: "Second migration",
Code: func(tx Transaction) error {
return tx.Exec(`CREATE TABLE account2(
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL
);`)
},
})
m, _ = NewMigrator(db, migrations, slog.Default())
if len(m.Applied()) != len(migrations)-1 {
t.Errorf("Old migratinos should be applied")
}
if len(m.Pending()) != 1 {
t.Errorf("There should be one more pending migration")
}
err = m.Migrate()
if err != nil {
t.Error(err)
}
if len(m.Applied()) != len(migrations) {
t.Errorf("After migration, all migrations should be applied")
}
if len(m.Pending()) != 0 {
t.Errorf("At migration, there should be no pending migrations")
}
m, _ = NewMigrator(db, make([]Migration, 0), slog.Default())
if err := m.Migrate(); err.Error() != "found more applied migrations than supplied" {
t.Errorf("Should not accept unknown migrations")
}
}
func testModifiedMigration(db *sql.DB, t *testing.T) {
migrations := []Migration{
{
Name: "First migration",
Code: func(tx Transaction) error {
return tx.Exec(`CREATE TABLE account(
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);`)
},
},
}
m, _ := NewMigrator(db, migrations, slog.Default())
if err := m.Migrate(); err != nil {
t.Error(err)
}
if m.CountApplied() != 1 {
t.Error("No more migrations should have been applied")
}
migrations[0].Name = "Fist migration"
m, _ = NewMigrator(db, migrations, slog.Default())
if err := m.Migrate(); err.Error() != fmt.Sprintf("migration id 0 \"%s\" names changed: current:\"%s\" != applied:\"%s\"", "Fist migration", "Fist migration", "First migration") {
t.Error("name change should have been detected")
}
migrations[0].Name = "First migration"
migrations[0].Code = func(tx Transaction) error {
return tx.Exec(`CREATE TABLE account(
email VARCHAR (355) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);`)
}
if err := m.Migrate(); err.Error() != fmt.Sprintf("migration id 0 \"%s\" hash changed 6897e2a8451838d000afe849c4f1b7da4a5dc485c768ec128d96e9cf47d6041d expected 8cc30dbeae1a04ad30a7268fa5f00f673b74841caed544ef4f1be5d3e2163b99 actual", "First migration") {
t.Error("Name hash change should have been detected")
}
if m.CountApplied() != 1 {
t.Error("No more migrations should have been applied")
}
}
func testFailingMigration(db *sql.DB, t *testing.T) {
migrations := []Migration{
{
Name: "First migration",
Code: func(tx Transaction) error {
return tx.Exec(`CREATE TABLE WITH SYNTAX ERROR);`)
},
},
}
m, _ := NewMigrator(db, migrations, slog.Default())
if err := m.Migrate(); strings.Contains(err.Error(), "Migration (0) - \"First migration\" returned an error:") {
t.Error("Error not thrown on invalid migration code")
}
if m.CountApplied() != 0 {
t.Error("No migrations should have been applied")
}
}