aboutsummaryrefslogtreecommitdiff
path: root/db/pdatadb/migrations_test.go
blob: 2d0b1b4fb6c50e5c06d9da20518704207ac7f19e (plain)
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
package pdatadb

import (
	"context"
	"path/filepath"
	"sort"
	"testing"

	_ "github.com/mattn/go-sqlite3"
)

func TestMigrations(t *testing.T) {
	db, err := Open(filepath.Join(t.TempDir(), "pdata.db"))
	if err != nil {
		panic(err)
	}
	defer db.Close()

	cur, _, err := db.Version()
	if err != nil {
		panic(err)
	}
	if cur != 0 {
		t.Fatalf("current version not 0")
	}

	var ms []uint64
	for m := range migrations {
		ms = append(ms, m)
	}
	sort.Slice(ms, func(i, j int) bool {
		return ms[i] < ms[j]
	})

	for _, to := range ms {
		if err := db.MigrateUp(context.Background(), to); err != nil {
			t.Fatalf("migrate up to %d: %v", to, err)
		}
		if err := db.MigrateDown(context.Background(), 0); err != nil {
			t.Fatalf("migrate down from %d to 0: %v", to, err)
		}
		if err := db.MigrateUp(context.Background(), to); err != nil {
			t.Fatalf("migrate up to %d again: %v", to, err)
		}
		if err := db.MigrateDown(context.Background(), 0); err != nil {
			t.Fatalf("migrate down from %d to 0 again: %v", to, err)
		}
	}
}