-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_test.go
189 lines (148 loc) · 4.35 KB
/
sqlite_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
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
179
180
181
182
183
184
185
186
187
188
189
package dbmate
import (
"database/sql"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func sqliteTestURL(t *testing.T) *url.URL {
u, err := url.Parse("sqlite3:////tmp/dbmate.sqlite3")
require.Nil(t, err)
return u
}
func prepTestSQLiteDB(t *testing.T) *sql.DB {
drv := SQLiteDriver{}
u := sqliteTestURL(t)
// drop any existing database
err := drv.DropDatabase(u)
require.Nil(t, err)
// create database
err = drv.CreateDatabase(u)
require.Nil(t, err)
// connect database
db, err := drv.Open(u)
require.Nil(t, err)
return db
}
func TestSQLiteCreateDropDatabase(t *testing.T) {
drv := SQLiteDriver{}
u := sqliteTestURL(t)
// drop any existing database
err := drv.DropDatabase(u)
require.Nil(t, err)
// create database
err = drv.CreateDatabase(u)
require.Nil(t, err)
// check that database exists
_, err = os.Stat(sqlitePath(u))
require.Nil(t, err)
// drop the database
err = drv.DropDatabase(u)
require.Nil(t, err)
// check that database no longer exists
_, err = os.Stat(sqlitePath(u))
require.NotNil(t, err)
require.Equal(t, true, os.IsNotExist(err))
}
func TestSQLiteDatabaseExists(t *testing.T) {
drv := SQLiteDriver{}
u := sqliteTestURL(t)
// drop any existing database
err := drv.DropDatabase(u)
require.Nil(t, err)
// DatabaseExists should return false
exists, err := drv.DatabaseExists(u)
require.Nil(t, err)
require.Equal(t, false, exists)
// create database
err = drv.CreateDatabase(u)
require.Nil(t, err)
// DatabaseExists should return true
exists, err = drv.DatabaseExists(u)
require.Nil(t, err)
require.Equal(t, true, exists)
}
func TestSQLiteCreateMigrationsTable(t *testing.T) {
drv := SQLiteDriver{}
db := prepTestSQLiteDB(t)
defer mustClose(db)
// migrations table should not exist
count := 0
err := db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Regexp(t, "no such table: schema_migrations", err.Error())
// create table
err = drv.CreateMigrationsTable(db)
require.Nil(t, err)
// migrations table should exist
err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
// create table should be idempotent
err = drv.CreateMigrationsTable(db)
require.Nil(t, err)
}
func TestSQLiteSelectMigrations(t *testing.T) {
drv := SQLiteDriver{}
db := prepTestSQLiteDB(t)
defer mustClose(db)
err := drv.CreateMigrationsTable(db)
require.Nil(t, err)
_, err = db.Exec(`insert into schema_migrations (version)
values ('abc2'), ('abc1'), ('abc3')`)
require.Nil(t, err)
migrations, err := drv.SelectMigrations(db, -1, "default")
require.Nil(t, err)
require.Equal(t, true, migrations["abc1"])
require.Equal(t, true, migrations["abc2"])
require.Equal(t, true, migrations["abc2"])
// test limit param
migrations, err = drv.SelectMigrations(db, 1, "default")
require.Nil(t, err)
require.Equal(t, true, migrations["abc3"])
require.Equal(t, false, migrations["abc1"])
require.Equal(t, false, migrations["abc2"])
// test different project
_, err = db.Exec(`insert into schema_migrations (version, project)
values ('bcd1', 'app2')`)
require.Nil(t, err)
migrations, err = drv.SelectMigrations(db, -1, "app2")
require.Nil(t, err)
require.Equal(t, true, migrations["bcd1"])
require.Equal(t, false, migrations["abc1"])
require.Equal(t, false, migrations["abc2"])
require.Equal(t, false, migrations["abc3"])
}
func TestSQLiteInsertMigration(t *testing.T) {
drv := SQLiteDriver{}
db := prepTestSQLiteDB(t)
defer mustClose(db)
err := drv.CreateMigrationsTable(db)
require.Nil(t, err)
count := 0
err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
require.Equal(t, 0, count)
// insert migration
err = drv.InsertMigration(db, "abc1", "default")
require.Nil(t, err)
err = db.QueryRow("select count(*) from schema_migrations where version = 'abc1'").
Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}
func TestSQLiteDeleteMigration(t *testing.T) {
drv := SQLiteDriver{}
db := prepTestSQLiteDB(t)
defer mustClose(db)
err := drv.CreateMigrationsTable(db)
require.Nil(t, err)
_, err = db.Exec(`insert into schema_migrations (version)
values ('abc1'), ('abc2')`)
require.Nil(t, err)
err = drv.DeleteMigration(db, "abc2")
require.Nil(t, err)
count := 0
err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}