-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_migrate_test.go
139 lines (116 loc) · 3.26 KB
/
integration_migrate_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
package main
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMigration(t *testing.T) {
t.Run("empty migrations", func(t *testing.T) {
t.Parallel()
tc := NewMigrationTestContext(t, nil)
defer tc.CleanUp(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := tc.Migrator().Up(ctx, migrationStepAll)
assert.NoError(t, err)
err = tc.Migrator().Down(ctx, migrationStepAll)
assert.NoError(t, err)
})
t.Run("apply all migrations", func(t *testing.T) {
t.Parallel()
tc := NewMigrationTestContext(t, map[string]string{
"1_test.up.sql": `create table test (id int);`,
"1_test.down.sql": `drop table test;`,
})
defer tc.CleanUp(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
t.Log("up")
{
err := tc.Migrator().Up(ctx, migrationStepAll)
assert.NoError(t, err)
t.Log("rerunning migrations")
err = tc.Migrator().Up(ctx, migrationStepAll)
assert.NoError(t, err)
}
t.Log("down")
{
err := tc.Migrator().Down(ctx, migrationStepAll)
assert.NoError(t, err)
t.Log("rerunning migrations")
err = tc.Migrator().Down(ctx, migrationStepAll)
assert.NoError(t, err)
}
})
t.Run("apply migrations by step", func(t *testing.T) {
t.Parallel()
tc := NewMigrationTestContext(t, map[string]string{
"1_test.up.sql": `create table test (id int);`,
"1_test.down.sql": `drop table test;`,
"2_test2.up.sql": `create table test2 (id int);`,
"2_test2.down.sql": `drop table test2;`,
})
defer tc.CleanUp(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
t.Log("up 1 step (current step = 0)")
{
err := tc.Migrator().Up(ctx, 1)
assert.NoError(t, err)
}
t.Log("up 1 step (current step = 1)")
{
err := tc.Migrator().Up(ctx, 1)
assert.NoError(t, err)
}
t.Log("up 1 step (current step = 2)")
{
err := tc.Migrator().Up(ctx, 1)
assert.Error(t, err)
}
t.Log("down 1 step (current step = 2)")
{
err := tc.Migrator().Down(ctx, 1)
assert.NoError(t, err)
}
t.Log("down 1 step (current step = 1)")
{
err := tc.Migrator().Down(ctx, 1)
assert.NoError(t, err)
}
t.Log("down 1 step (current step = 0)")
{
err := tc.Migrator().Down(ctx, 1)
assert.Error(t, err)
}
})
t.Run("failed migrations", func(t *testing.T) {
t.Run("up", func(t *testing.T) {
t.Parallel()
tc := NewMigrationTestContext(t, map[string]string{
"1_test.up.sql": `create table test invalid sql;`,
"1_test.down.sql": `drop table test;`,
})
defer tc.CleanUp(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := tc.Migrator().Up(ctx, migrationStepAll)
assert.Error(t, err)
})
t.Run("down", func(t *testing.T) {
t.Parallel()
tc := NewMigrationTestContext(t, map[string]string{
"1_test.up.sql": `create table test (id int);`,
"1_test.down.sql": `drop table invalid sql;`,
})
defer tc.CleanUp(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := tc.Migrator().Up(ctx, migrationStepAll)
assert.NoError(t, err)
err = tc.migrator.Down(ctx, migrationStepAll)
assert.Error(t, err)
})
})
}