-
Notifications
You must be signed in to change notification settings - Fork 33
/
ql_dialect_test.go
76 lines (70 loc) · 1.49 KB
/
ql_dialect_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
package darwin
import (
"database/sql"
"log"
"testing"
_ "github.com/cznic/ql/driver"
)
func TestQLDialect(t *testing.T) {
migrations := []Migration{
{
Version: 1,
Description: "Creating table posts",
Script: `CREATE TABLE posts (
id int,
title string,
);;`,
},
{
Version: 2,
Description: "Adding column body",
Script: "ALTER TABLE posts ADD body string;",
},
}
db, err := sql.Open("ql-mem", "test.db")
if err != nil {
t.Fatal(err)
}
dv := NewGenericDriver(db, QLDialect{})
d := New(dv, migrations, nil)
err = d.Migrate()
if err != nil {
t.Fatal(err)
}
if !hasTable(db, "posts", t) {
t.Error("expected the tble posts to exist")
}
cols := getAllColumns(db, "posts", t)
if len(cols) != 3 {
t.Errorf("expected 3 columns got %d", len(cols))
}
}
func hasTable(db *sql.DB, tableName string, t *testing.T) bool {
querry := "select count() from __Table where Name=$1"
var count int
err := db.QueryRow(querry, tableName).Scan(&count)
if err != nil {
t.Fatal(err)
}
return count > 0
}
func getAllColumns(db *sql.DB, tableName string, t *testing.T) []string {
var result []string
query := `select Name from __Column where TableName=$1`
rows, err := db.Query(query, tableName)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
log.Fatal(err)
}
result = append(result, name)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
return result
}