~egtann/migrate

ref: f8bba57067c303761315e2916cc06bc61880b2cd migrate/sqlite/sqlite_test.go -rw-r--r-- 3.4 KiB
f8bba570 — Evan Tann update module path 1 year, 1 month ago
                                                                                
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package sqlite

import (
	"testing"

	"egt.run/pkg/migrate"
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
)

const checkpointFile = "2.sql"

func TestCreateMetaIfNotExists(t *testing.T) {
	t.Parallel()
	db := newDB()

	err := db.CreateMetaIfNotExists()
	check(t, err)

	var tmp []int
	err = db.DB.Select(&tmp, `SELECT 1 FROM meta`)
	check(t, err)
}

func TestCreateMetaCheckpointsIfNotExists(t *testing.T) {
	t.Parallel()
	db := newDB()
	err := db.CreateMetaCheckpointsIfNotExists()
	check(t, err)

	var tmp []int
	err = db.DB.Select(&tmp, `SELECT 1 FROM metacheckpoints`)
	check(t, err)
}

func TestGetMigrations(t *testing.T) {
	t.Parallel()
	db := setupDBV1(t)
	ms, err := db.GetMigrations()
	check(t, err)
	if len(ms) != 1 {
		t.Fatal("expected 1 migration")
	}
}

func TestGetMetaCheckpoints(t *testing.T) {
	t.Parallel()
	db := setupDBV1(t)
	mcs, err := db.GetMetaCheckpoints(checkpointFile)
	check(t, err)
	if len(mcs) != 1 {
		t.Fatal("expected 1 checkpoint")
	}
}

func TestUpsertMigration(t *testing.T) {
	t.Parallel()
	db := setupDBV1(t)

	// Test update
	err := db.UpsertMigration("1.sql", "SELECT 1;", "md5")
	check(t, err)

	// Test insert
	err = db.UpsertMigration("3.sql", "SELECT 3;", "md5")
	check(t, err)

	ms, err := db.GetMigrations()
	check(t, err)
	if len(ms) != 2 {
		t.Fatal("expected 2 migrations")
	}
}

func TestInsertMetaCheckpoint(t *testing.T) {
	t.Parallel()
	db := setupDBV1(t)

	err := db.InsertMetaCheckpoint(checkpointFile, "SELECT 3;", "md5", 1)
	check(t, err)

	mcs, err := db.GetMetaCheckpoints(checkpointFile)
	check(t, err)
	if len(mcs) != 2 {
		t.Fatal("expected 2 checkpoints")
	}
}

func TestInsertMigration(t *testing.T) {
	t.Parallel()
	db := setupDBV1(t)

	err := db.InsertMigration("3.sql", "SELECT 3;", "md5")
	check(t, err)

	ms, err := db.GetMigrations()
	check(t, err)
	if len(ms) != 2 {
		t.Fatal("expected 2 migrations")
	}
}

func TestDeleteMetaCheckpoints(t *testing.T) {
	t.Parallel()
	db := setupDBV1(t)

	err := db.DeleteMetaCheckpoints()
	check(t, err)

	mcs, err := db.GetMetaCheckpoints(checkpointFile)
	check(t, err)
	if len(mcs) != 0 {
		t.Fatal("expected 0 checkpoints")
	}
}

func check(t *testing.T, err error) {
	t.Helper()
	if err != nil {
		t.Fatal(err)
	}
}

func newDB() *DB {
	// Every database connection sees a different database, which is
	// perfect, as that lets us run tests in parallel.
	db, err := sqlx.Open("sqlite3", ":memory:")
	if err != nil {
		panic(err)
	}
	return &DB{DB: db}
}

func setupDBV1(t *testing.T) *DB {
	db := setupDBV0(t)
	err := db.UpgradeToV1([]migrate.Migration{{
		Filename: "1.sql",
		Checksum: "md5",
		Content:  "SELECT 1;",
	}})
	check(t, err)

	q := `
		INSERT INTO metacheckpoints (idx, filename, content, md5)
		VALUES ($1, $2, $3, $4)`
	_, err = db.DB.Exec(q, 0, checkpointFile, "SELECT 2;", "md5")
	check(t, err)

	return db
}

func setupDBV0(t *testing.T) *DB {
	db := newDB()

	q := `CREATE TABLE IF NOT EXISTS meta (
		filename TEXT UNIQUE NOT NULL,
		md5 TEXT UNIQUE NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)`
	_, err := db.DB.Exec(q)
	check(t, err)

	q = `CREATE TABLE IF NOT EXISTS metacheckpoints (
		filename TEXT NOT NULL,
		idx INTEGER NOT NULL,
		md5 TEXT NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY (filename, idx)
	)`
	_, err = db.DB.Exec(q)
	check(t, err)

	q = `INSERT INTO meta (filename, md5) VALUES ($1, $2)`
	_, err = db.DB.Exec(q, "1.sql", "md5")
	check(t, err)

	return db
}