forked from 13k/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3_test.go
118 lines (107 loc) · 2.31 KB
/
sqlite3_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
package sqlite3
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/golang-migrate/migrate/v4"
dt "github.com/golang-migrate/migrate/v4/database/testing"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mattn/go-sqlite3"
)
func Test(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);"))
}
func TestMigrate(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()
driver, err := WithInstance(db, &Config{})
if err != nil {
t.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./examples/migrations",
"ql", driver)
if err != nil {
t.Fatal(err)
}
dt.TestMigrate(t, m, []byte("CREATE TABLE t (Qty int, Name string);"))
}
func TestMigrationTable(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3-driver-test-migration-table")
if err != nil {
return
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()
config := &Config{
MigrationsTable: "my_migration_table",
}
driver, err := WithInstance(db, config)
if err != nil {
t.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./examples/migrations",
"ql", driver)
if err != nil {
t.Fatal(err)
}
t.Log("UP")
err = m.Up()
if err != nil {
t.Fatal(err)
}
_, err = db.Query(fmt.Sprintf("SELECT * FROM %s", config.MigrationsTable))
if err != nil {
t.Fatal(err)
}
}