-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgmigrate_test.go
118 lines (110 loc) · 2.42 KB
/
pgmigrate_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
package pgmigrate
import (
"math/rand"
"os"
"sync"
"testing"
)
func TestPgMigrate(t *testing.T) {
postgresUrl := os.Getenv("POSTGRES_URL")
if postgresUrl == "" {
postgresUrl = "postgres://postgres:@127.0.0.1:5432/pgmigrate_test?sslmode=disable"
}
m := NewMigrator(postgresUrl)
m.Add(Migration{
Version: 1,
Name: "widgets_init",
Up: `
create table widgets (
widget_id integer primary key,
name text
);
`,
Down: `
drop table if exists widgets;
`,
})
m.Add(Migration{
Version: 2,
Name: "users_init",
Up: `
create table users (
user_id integer primary key,
name text
);
alter table widgets add column user_id integer references widgets;
`,
Down: `
alter table widgets drop column user_id;
drop table if exists users;
`,
})
m.Add(Migration{
Version: 3,
Name: "users_add_birthday",
Up: `
alter table users add column birthday date;
create index users_birthday on users (birthday);
`,
Down: `
alter table users drop column birthday;
`,
})
m.Add(Migration{
Version: 4,
Name: "users_add_cat_or_dog",
Up: `
alter table users add column cat_or_dog text;
`,
Down: `
alter table users drop column cat_or_dog;
`,
})
m.Add(Migration{
Version: 5,
Name: "users_rename_cat_or_dog_to_favorite_pet_type",
Up: `
alter table users rename column cat_or_dog to favorite_pet_type;
`,
Down: `
alter table users rename column favorite_pet_type to cat_or_dog;
`,
})
if err := m.DownAll(); err != nil {
t.Fatalf("unexpected error running DownAll: %s", err)
}
var funcs = []struct {
name string
fn func() error
}{
{name: "DownAll", fn: m.DownAll},
{name: "UpAll", fn: m.UpAll},
{name: "UpOne", fn: m.UpOne},
{name: "UpOne", fn: m.UpOne},
{name: "UpOne", fn: m.UpOne},
{name: "UpOne", fn: m.UpOne},
{name: "UpOne", fn: m.UpOne},
{name: "DownOne", fn: m.DownOne},
{name: "DownOne", fn: m.DownOne},
{name: "DownOne", fn: m.DownOne},
{name: "DownOne", fn: m.DownOne},
{name: "DownOne", fn: m.DownOne},
}
wg := sync.WaitGroup{}
for j := 0; j < 10; j++ {
wg.Add(1)
go func(j int) {
defer wg.Done()
for i := 0; i < 100; i++ {
n := rand.Intn(len(funcs))
if err := funcs[n].fn(); err != nil {
t.Fatalf("unexpected error running %s (random): %s", funcs[n].name, err)
}
}
}(j)
}
wg.Wait()
if err := m.UpAll(); err != nil {
t.Fatalf("unexpected error running UpAll: %s", err)
}
}