-
Notifications
You must be signed in to change notification settings - Fork 4
/
postgres_test.go
218 lines (186 loc) · 4.52 KB
/
postgres_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
package postgres
import (
"database/sql"
"os"
"reflect"
"testing"
"github.com/db-journey/migrate/v2/direction"
"github.com/db-journey/migrate/v2/driver"
"github.com/db-journey/migrate/v2/file"
)
// TestMigrate runs some additional tests on Migrate().
// Basic testing is already done in migrate_test.go
func TestMigrate(t *testing.T) {
host := getenvDefault("POSTGRES_PORT_5432_TCP_ADDR", "localhost")
port := getenvDefault("POSTGRES_PORT_5432_TCP_PORT", "5432")
driverURL := "postgres://postgres:p@" + host + ":" + port + "/template1?sslmode=disable"
// prepare clean database
connection, err := sql.Open("postgres", driverURL)
if err != nil {
t.Fatal(err)
}
dropTestTables(t, connection)
migrate(t, driverURL)
dropTestTables(t, connection)
// Make an old-style `int` version column that we'll have to upgrade.
_, err = connection.Exec("CREATE TABLE IF NOT EXISTS " + tableName + " (version bigint not null primary key)")
if err != nil {
t.Fatal(err)
}
migrate(t, driverURL)
}
func migrate(t *testing.T, driverURL string) {
var err error
var d driver.Driver
if d, err = Open(driverURL); err != nil {
t.Fatal(err)
}
files := []file.File{
{
Path: "/foobar",
FileName: "20060102150405_foobar.up.sql",
Version: 20060102150405,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE yolo (
id serial not null primary key
);
CREATE TYPE colors AS ENUM (
'red',
'green'
);
`),
},
{
Path: "/foobar",
FileName: "20060102150405_foobar.down.sql",
Version: 20060102150405,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
Path: "/foobar",
FileName: "20060102150406_foobar.up.sql",
Version: 20060102150406,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`-- disable_ddl_transaction
ALTER TYPE colors ADD VALUE 'blue' AFTER 'red';
`),
},
{
Path: "/foobar",
FileName: "20060102150406_foobar.down.sql",
Version: 20060102150406,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TYPE colors;
`),
},
{
Path: "/foobar",
FileName: "20060102150407_foobar.up.sql",
Version: 20060102150407,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE error (
id THIS WILL CAUSE AN ERROR
)
`),
},
}
// should create table yolo
err = d.Migrate(files[0])
if err != nil {
t.Fatal(err)
}
version, err := d.Version()
if err != nil {
t.Fatal(err)
}
if version != 20060102150405 {
t.Errorf("Expected version to be: %d, got: %d", 20060102150405, version)
}
// Check versions applied in DB
expectedVersions := file.Versions{20060102150405}
versions, err := d.Versions()
if err != nil {
t.Errorf("Could not fetch versions: %s", err)
}
if !reflect.DeepEqual(versions, expectedVersions) {
t.Errorf("Expected versions to be: %v, got: %v", expectedVersions, versions)
}
// should alter type colors
err = d.Migrate(files[2])
if err != nil {
t.Fatal(err)
}
colors := []string{}
expectedColors := []string{"red", "blue", "green"}
rows, err := d.(*Driver).db.Query("SELECT unnest(enum_range(NULL::colors));")
if err != nil {
t.Error(err)
return
}
defer rows.Close()
for rows.Next() {
var color string
if err = rows.Scan(&color); err != nil {
t.Error(err)
return
}
colors = append(colors, color)
}
if err = rows.Err(); err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(colors, expectedColors) {
t.Errorf("Expected colors enum to be %q, got %q\n", expectedColors, colors)
}
err = d.Migrate(files[3])
if err != nil {
t.Fatal(err)
}
err = d.Migrate(files[1])
if err != nil {
t.Fatal(err)
}
err = d.Migrate(files[4])
if err == nil {
t.Error("Expected test case to fail")
}
// Check versions applied in DB
expectedVersions = file.Versions{}
versions, err = d.Versions()
if err != nil {
t.Errorf("Could not fetch versions: %s", err)
}
if !reflect.DeepEqual(versions, expectedVersions) {
t.Errorf("Expected versions to be: %v, got: %v", expectedVersions, versions)
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}
func dropTestTables(t *testing.T, db *sql.DB) {
if _, err := db.Exec(`
DROP TYPE IF EXISTS colors;
DROP TABLE IF EXISTS yolo;
DROP TABLE IF EXISTS ` + tableName + `;`); err != nil {
t.Fatal(err)
}
}
func getenvDefault(varname, defaultValue string) string {
v := os.Getenv(varname)
if v == "" {
return defaultValue
}
return v
}