forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_parser_test.go
344 lines (297 loc) · 7.4 KB
/
sql_parser_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
package goose
import (
"os"
"strings"
"testing"
"github.com/pkg/errors"
)
func TestSemicolons(t *testing.T) {
t.Parallel()
type testData struct {
line string
result bool
}
tests := []testData{
{line: "END;", result: true},
{line: "END; -- comment", result: true},
{line: "END ; -- comment", result: true},
{line: "END -- comment", result: false},
{line: "END -- comment ;", result: false},
{line: "END \" ; \" -- comment", result: false},
}
for _, test := range tests {
r := endsWithSemicolon(test.line)
if r != test.result {
t.Errorf("incorrect semicolon. got %v, want %v", r, test.result)
}
}
}
func TestSplitStatements(t *testing.T) {
t.Parallel()
// SetVerbose(true)
type testData struct {
sql string
up int
down int
}
tt := []testData{
{sql: multilineSQL, up: 4, down: 1},
{sql: emptySQL, up: 0, down: 0},
{sql: emptySQL2, up: 0, down: 0},
{sql: functxt, up: 2, down: 2},
{sql: mysqlChangeDelimiter, up: 4, down: 0},
{sql: copyFromStdin, up: 1, down: 0},
{sql: plpgsqlSyntax, up: 2, down: 2},
{sql: plpgsqlSyntaxMixedStatements, up: 2, down: 2},
}
for i, test := range tt {
// up
stmts, _, err := parseSQLMigration(strings.NewReader(test.sql), true)
if err != nil {
t.Error(errors.Wrapf(err, "tt[%v] unexpected error", i))
}
if len(stmts) != test.up {
t.Errorf("tt[%v] incorrect number of up stmts. got %v (%+v), want %v", i, len(stmts), stmts, test.up)
}
// down
stmts, _, err = parseSQLMigration(strings.NewReader(test.sql), false)
if err != nil {
t.Error(errors.Wrapf(err, "tt[%v] unexpected error", i))
}
if len(stmts) != test.down {
t.Errorf("tt[%v] incorrect number of down stmts. got %v (%+v), want %v", i, len(stmts), stmts, test.down)
}
}
}
func TestUseTransactions(t *testing.T) {
t.Parallel()
type testData struct {
fileName string
useTransactions bool
}
tests := []testData{
{fileName: "./examples/sql-migrations/00001_create_users_table.sql", useTransactions: true},
{fileName: "./examples/sql-migrations/00002_rename_root.sql", useTransactions: true},
{fileName: "./examples/sql-migrations/00003_no_transaction.sql", useTransactions: false},
}
for _, test := range tests {
f, err := os.Open(test.fileName)
if err != nil {
t.Error(err)
}
_, useTx, err := parseSQLMigration(f, true)
if err != nil {
t.Error(err)
}
if useTx != test.useTransactions {
t.Errorf("Failed transaction check. got %v, want %v", useTx, test.useTransactions)
}
f.Close()
}
}
func TestParsingErrors(t *testing.T) {
tt := []string{
statementBeginNoStatementEnd,
unfinishedSQL,
noUpDownAnnotations,
multiUpDown,
downFirst,
}
for i, sql := range tt {
_, _, err := parseSQLMigration(strings.NewReader(sql), true)
if err == nil {
t.Errorf("expected error on tt[%v] %q", i, sql)
}
}
}
var multilineSQL = `-- +goose Up
CREATE TABLE post (
id int NOT NULL,
title text,
body text,
PRIMARY KEY(id)
); -- 1st stmt
-- comment
SELECT 2; -- 2nd stmt
SELECT 3; SELECT 3; -- 3rd stmt
SELECT 4; -- 4th stmt
-- +goose Down
-- comment
DROP TABLE post; -- 1st stmt
`
var functxt = `-- +goose Up
CREATE TABLE IF NOT EXISTS histories (
id BIGSERIAL PRIMARY KEY,
current_value varchar(2000) NOT NULL,
created_at timestamp with time zone NOT NULL
);
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION histories_partition_creation( DATE, DATE )
returns void AS $$
DECLARE
create_query text;
BEGIN
FOR create_query IN SELECT
'CREATE TABLE IF NOT EXISTS histories_'
|| TO_CHAR( d, 'YYYY_MM' )
|| ' ( CHECK( created_at >= timestamp '''
|| TO_CHAR( d, 'YYYY-MM-DD 00:00:00' )
|| ''' AND created_at < timestamp '''
|| TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD 00:00:00' )
|| ''' ) ) inherits ( histories );'
FROM generate_series( $1, $2, '1 month' ) AS d
LOOP
EXECUTE create_query;
END LOOP; -- LOOP END
END; -- FUNCTION END
$$
language plpgsql;
-- +goose StatementEnd
-- +goose Down
drop function histories_partition_creation(DATE, DATE);
drop TABLE histories;
`
var multiUpDown = `-- +goose Up
CREATE TABLE post (
id int NOT NULL,
title text,
body text,
PRIMARY KEY(id)
);
-- +goose Down
DROP TABLE post;
-- +goose Up
CREATE TABLE fancier_post (
id int NOT NULL,
title text,
body text,
created_on timestamp without time zone,
PRIMARY KEY(id)
);
`
var downFirst = `-- +goose Down
DROP TABLE fancier_post;
`
var statementBeginNoStatementEnd = `-- +goose Up
CREATE TABLE IF NOT EXISTS histories (
id BIGSERIAL PRIMARY KEY,
current_value varchar(2000) NOT NULL,
created_at timestamp with time zone NOT NULL
);
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION histories_partition_creation( DATE, DATE )
returns void AS $$
DECLARE
create_query text;
BEGIN
FOR create_query IN SELECT
'CREATE TABLE IF NOT EXISTS histories_'
|| TO_CHAR( d, 'YYYY_MM' )
|| ' ( CHECK( created_at >= timestamp '''
|| TO_CHAR( d, 'YYYY-MM-DD 00:00:00' )
|| ''' AND created_at < timestamp '''
|| TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD 00:00:00' )
|| ''' ) ) inherits ( histories );'
FROM generate_series( $1, $2, '1 month' ) AS d
LOOP
EXECUTE create_query;
END LOOP; -- LOOP END
END; -- FUNCTION END
$$
language plpgsql;
-- +goose Down
drop function histories_partition_creation(DATE, DATE);
drop TABLE histories;
`
var unfinishedSQL = `
-- +goose Up
ALTER TABLE post
-- +goose Down
`
var emptySQL = `-- +goose Up
-- This is just a comment`
var emptySQL2 = `
-- comment
-- +goose Up
-- comment
-- +goose Down
`
var noUpDownAnnotations = `
CREATE TABLE post (
id int NOT NULL,
title text,
body text,
PRIMARY KEY(id)
);
`
var mysqlChangeDelimiter = `
-- +goose Up
-- +goose StatementBegin
DELIMITER |
-- +goose StatementEnd
-- +goose StatementBegin
CREATE FUNCTION my_func( str CHAR(255) ) RETURNS CHAR(255) DETERMINISTIC
BEGIN
RETURN "Dummy Body";
END |
-- +goose StatementEnd
-- +goose StatementBegin
DELIMITER ;
-- +goose StatementEnd
select my_func("123") from dual;
-- +goose Down
`
var copyFromStdin = `
-- +goose Up
-- +goose StatementBegin
COPY public.django_content_type (id, app_label, model) FROM stdin;
1 admin logentry
2 auth permission
3 auth group
4 auth user
5 contenttypes contenttype
6 sessions session
\.
-- +goose StatementEnd
`
var plpgsqlSyntax = `
-- +goose Up
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'plpgsql';
-- +goose StatementEnd
-- +goose StatementBegin
CREATE TRIGGER update_properties_updated_at BEFORE UPDATE ON properties FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TRIGGER update_properties_updated_at
-- +goose StatementEnd
-- +goose StatementBegin
DROP FUNCTION update_updated_at_column()
-- +goose StatementEnd
`
var plpgsqlSyntaxMixedStatements = `
-- +goose Up
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'plpgsql';
-- +goose StatementEnd
CREATE TRIGGER update_properties_updated_at
BEFORE UPDATE
ON properties
FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();
-- +goose Down
DROP TRIGGER update_properties_updated_at;
DROP FUNCTION update_updated_at_column();
`