-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmigrations_test.go
More file actions
107 lines (87 loc) · 2.65 KB
/
migrations_test.go
File metadata and controls
107 lines (87 loc) · 2.65 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
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;
-- +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);
-- +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 = `
-- 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);
-- migrate:down
DROP TABLE foo;`
const outputDbmate = `
-- migrate:up
CREATE TABLE foo (bar int);`
const inputPsqlMeta = `\restrict auwherpfqaiuwrhgp
CREATE TABLE foo (id int);
\unrestrict auwherpfqaiuwrhgp
`
const outputPsqlMeta = `
CREATE TABLE foo (id int);
`
func TestRemoveRollback(t *testing.T) {
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
t.Errorf("goose migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputTern, RemoveRollbackStatements(inputTern)); diff != "" {
t.Errorf("tern migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputDbmate, RemoveRollbackStatements(inputDbmate)); diff != "" {
t.Errorf("dbmate migration mismatch:\n%s", diff)
}
}
func TestRemovePsqlMetaCommands(t *testing.T) {
if diff := cmp.Diff(outputPsqlMeta, RemovePsqlMetaCommands(inputPsqlMeta)); diff != "" {
t.Errorf("psql meta-command mismatch:\n%s", diff)
}
}
func TestRemoveGolangMigrateRollback(t *testing.T) {
filenames := map[string]bool{
// make sure we let through golang-migrate files that aren't rollbacks
"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)
}
}
}