-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-update_test.go
119 lines (102 loc) · 2.6 KB
/
schema-update_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
package sqldb
import (
"strings"
"testing"
"github.com/jmoiron/sqlx"
)
func TestUpdateSchema(t *testing.T) {
c := NewSQLite(SQLiteInMemoryFilepathRaceSafe)
//Need to deploy schema first.
createTable := `
CREATE TABLE IF NOT EXISTS users (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Username TEXT NOT NULL
)
`
c.DeployQueries = []string{createTable}
deployOpts := &DeploySchemaOptions{
CloseConnection: false,
}
err := c.DeploySchema(deployOpts)
if err != nil {
t.Fatal(err)
return
}
//Update schema.
updateTable := `ALTER TABLE users ADD COLUMN FirstName TEXT`
c.UpdateQueries = []string{updateTable}
uf := func(c *sqlx.DB) error {
q := "ALTER TABLE users ADD COLUMN LastName TEXT"
_, err := c.Exec(q)
return err
}
c.UpdateFuncs = []QueryFunc{uf}
updateOpts := &UpdateSchemaOptions{
CloseConnection: false,
}
err = c.UpdateSchema(updateOpts)
if err != nil {
t.Fatal(err)
return
}
//Insert into new columns to make sure they were created.
insert := `INSERT INTO users (Username, FirstName) VALUES (?, ?)`
_, err = c.connection.Exec(insert, "[email protected]", "john")
if err != nil {
t.Fatal(err)
return
}
insert = `INSERT INTO users (Username, FirstName, LastName) VALUES (?, ?, ?)`
_, err = c.connection.Exec(insert, "[email protected]", "john", "doe")
if err != nil {
t.Fatal(err)
return
}
//Try updating with an invalid config.
c.SQLitePath = ""
err = c.UpdateSchema(updateOpts)
if err == nil {
t.Fatal("Error about invalid config should have occured.")
return
}
//Try updating a db that is not already connected.
//Note error checking b/c db has not been deployed. If we deploy and close the
//db connection, the in-memory db is gone.
c.Close()
c.SQLitePath = SQLiteInMemoryFilepathRaceSafe
err = c.UpdateSchema(updateOpts)
if err != nil && !strings.Contains(err.Error(), "no such table") {
t.Fatal(err)
return
}
//Test with a bad update func.
c.Close()
c = NewSQLite(SQLiteInMemoryFilepathRaceSafe)
createTable = `
CREATE TABLE IF NOT EXISTS users (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Username TEXT NOT NULL
)
`
c.DeployQueries = []string{createTable}
err = c.DeploySchema(deployOpts)
if err != nil {
t.Fatal(err)
return
}
uf = func(c *sqlx.DB) error {
q := "ALTER ELBAT dynamite ADD COLUMN LastName TEXT"
_, err := c.Exec(q)
return err
}
c.UpdateFuncs = []QueryFunc{uf}
err = c.UpdateSchema(updateOpts)
if err == nil {
t.Fatal("Error about bad update func should have occured.")
return
}
if c.Connected() {
t.Fatal("Connection should be closed after bad update func.")
return
}
}