-
Notifications
You must be signed in to change notification settings - Fork 22
/
rollback_test.go
132 lines (105 loc) · 3.45 KB
/
rollback_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
package migrations
import (
"os"
"testing"
"time"
"github.com/go-pg/pg/v10"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRollback(t *testing.T) {
db := pg.Connect(&pg.Options{
Addr: "localhost:5432",
User: os.Getenv("TEST_DATABASE_USER"),
Database: os.Getenv("TEST_DATABASE_NAME"),
})
db.AddQueryHook(logQueryHook{})
m := newMigrator(db, RunOptions{})
err := m.ensureMigrationTables()
require.Nil(t, err)
defer clearMigrations(t, db)
defer resetMigrations(t)
t.Run("sorts migrations in reverse order", func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)
migrations = []*migration{
{Name: "123", Up: noopMigration, Down: noopMigration},
{Name: "456", Up: noopMigration, Down: noopMigration},
}
err := m.rollback()
assert.Nil(tt, err)
assert.Equal(tt, "456", migrations[0].Name)
assert.Equal(tt, "123", migrations[1].Name)
})
t.Run("returns an error if the migration lock is already held", func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)
migrations = []*migration{
{Name: "123", Up: noopMigration, Down: noopMigration},
{Name: "456", Up: noopMigration, Down: noopMigration},
}
err := m.acquireLock()
assert.Nil(tt, err)
defer m.releaseLock()
err = m.rollback()
assert.Equal(tt, ErrAlreadyLocked, err)
})
t.Run("exits early if there aren't any migrations to rollback", func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)
migrations = []*migration{
{Name: "123", Up: noopMigration, Down: noopMigration},
{Name: "456", Up: noopMigration, Down: noopMigration},
}
err := m.rollback()
assert.Nil(tt, err)
count, err := db.Model(&migration{}).Count()
assert.Nil(tt, err)
assert.Equal(tt, 0, count)
})
t.Run("only rolls back the last batch", func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)
migrations = []*migration{
{Name: "123", Up: noopMigration, Down: noopMigration, Batch: 4, CompletedAt: time.Now()},
{Name: "456", Up: noopMigration, Down: noopMigration, Batch: 5, CompletedAt: time.Now()},
{Name: "789", Up: noopMigration, Down: noopMigration, Batch: 5, CompletedAt: time.Now()},
{Name: "010", Up: noopMigration, Down: noopMigration},
}
mig := migrations[:2]
_, err := db.Model(&mig).Insert()
assert.Nil(tt, err)
err = m.rollback()
assert.Nil(tt, err)
batch, err := m.getLastBatchNumber()
assert.Nil(tt, err)
assert.Equal(tt, batch, int32(4))
count, err := db.Model(&migration{}).Count()
assert.Nil(tt, err)
assert.Equal(tt, 1, count)
})
t.Run(`runs "down" within a transaction if specified`, func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)
migrations = []*migration{
{Name: "123", Up: noopMigration, Down: erringMigration, DisableTransaction: false, Batch: 1, CompletedAt: time.Now()},
}
_, err := db.Model(&migrations).Insert()
assert.Nil(tt, err)
err = m.rollback()
assert.EqualError(tt, err, "123: error")
assertTable(tt, db, "test_table", false)
})
t.Run(`doesn't run "down" within a transaction if specified`, func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)
migrations = []*migration{
{Name: "123", Up: noopMigration, Down: erringMigration, DisableTransaction: true, Batch: 1, CompletedAt: time.Now()},
}
_, err := db.Model(&migrations).Insert()
assert.Nil(tt, err)
err = m.rollback()
assert.EqualError(tt, err, "123: error")
assertTable(tt, db, "test_table", true)
})
}