-
Notifications
You must be signed in to change notification settings - Fork 159
/
migrations_test.go
63 lines (52 loc) · 1.39 KB
/
migrations_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
package empire
import (
"os"
"os/exec"
"testing"
_ "github.com/lib/pq"
"github.com/remind101/empire/dbtest"
"github.com/remind101/empire/internal/migrate"
"github.com/stretchr/testify/assert"
)
// Tests migrating the database down, then back up again.
func TestMigrations(t *testing.T) {
db, err := NewDB(dbtest.Open(t))
if err != nil {
t.Fatal(err)
}
migrations := DefaultSchema.migrations()
err = db.migrator.Exec(migrate.Up, migrations...)
assert.NoError(t, err)
err = db.Reset()
assert.NoError(t, err)
err = db.migrator.Exec(migrate.Down, migrations...)
assert.NoError(t, err)
err = db.migrator.Exec(migrate.Up, migrations...)
assert.NoError(t, err)
f, err := os.Create("schema.sql")
if err != nil {
t.Fatal(err)
}
defer f.Close()
cmd := exec.Command("pg_dump", "--schema-only", "--no-owner", "--no-acl", *dbtest.DatabaseURL)
cmd.Stdout = f
cmd.Stderr = os.Stderr
assert.NoError(t, cmd.Run())
}
func TestLatestSchema(t *testing.T) {
assert.Equal(t, 21, DefaultSchema.latestSchema())
}
func TestNoDuplicateMigrations(t *testing.T) {
visited := make(map[int]bool)
expectedID := 1
for _, m := range DefaultSchema.migrations() {
if visited[m.ID] {
t.Fatalf("Migration %d appears more than once", m.ID)
}
visited[m.ID] = true
if m.ID != expectedID {
t.Fatalf("Expected migration %d after %d, but got %d", expectedID, expectedID-1, m.ID)
}
expectedID++
}
}