-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmigrations_test.go
More file actions
123 lines (92 loc) · 2.66 KB
/
migrations_test.go
File metadata and controls
123 lines (92 loc) · 2.66 KB
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
package migrations
import (
"testing"
"github.com/google/go-cmp/cmp"
)
const inputGoose = `
-- +goose Up
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
-- sqlc:ignore
CREATE TABLE countries (id int);
CREATE TABLE people (id int);
-- sqlc:ignore
-- +goose Down
ALTER TABLE archived_jobs DROP COLUMN expires_at;
`
const outputGoose = `
-- +goose Up
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
`
const inputMigrate = `
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
-- sqlc:ignore
INVALID SYNTAX HERE IS OK, WE SHOULD IGNORE THIS
-- sqlc:ignore end
-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE people;
`
const outputMigrate = `
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
`
const inputTern = `
-- sqlc:ignore
As first row also ok, all contents after should be processed
-- sqlc:ignore end
-- Write your migrate up statements here
ALTER TABLE todo RENAME COLUMN done TO is_done;
---- create above / drop below ----
ALTER TABLE todo RENAME COLUMN is_done TO done;
`
const outputTern = `
-- Write your migrate up statements here
ALTER TABLE todo RENAME COLUMN done TO is_done;`
const inputDbmate = `
-- migrate:up
CREATE TABLE foo (bar int);
-- sqlc:ignore
In up section
-- sqlc:ignore end
-- migrate:down
DROP TABLE foo;
-- sqlc:ignore
In down section
-- sqlc:ignore end`
const outputDbmate = `
-- migrate:up
CREATE TABLE foo (bar int);
`
func TestRemoveIgnored(t *testing.T) {
if diff := cmp.Diff(outputGoose, RemoveIgnoredStatements(inputGoose)); diff != "" {
t.Errorf("goose migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputMigrate, RemoveIgnoredStatements(inputMigrate)); diff != "" {
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputTern, RemoveIgnoredStatements(inputTern)); diff != "" {
t.Errorf("tern migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputDbmate, RemoveIgnoredStatements(inputDbmate)); diff != "" {
t.Errorf("dbmate migration mismatch:\n%s", diff)
}
}
func TestRemoveGolangMigrateRollback(t *testing.T) {
filenames := map[string]bool{
// make sure we let through golang-migrate files that aren't ignored
"migrations/1.up.sql": false,
// make sure we let through other sql files
"migrations/2.sql": false,
"migrations/foo.sql": false,
"migrations/1.down.sql": true,
}
for filename, want := range filenames {
got := IsDown(filename)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("IsDown mismatch: %s\n %s", filename, diff)
}
}
}