-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
91 lines (79 loc) · 1.65 KB
/
db.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
package main
import (
"bufio"
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"regexp"
)
const (
SCHEMA_FILE = "./data/schema.sql"
SEED_FILE = "./data/fifa14_seed.sql"
TEST_DB = "./test.db"
)
var DB *sql.DB
func initDB(dbPath string) {
dbFile, err := os.Open(dbPath)
if os.IsNotExist(err) {
fmt.Println("Creating Database at ", dbPath)
createDB(dbPath)
} else {
dbFile.Close()
openDB(dbPath)
fmt.Println("Database found at ", dbPath)
}
}
func openDB(dbPath string) {
var err error
DB, err = sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatal(err)
}
DB.Ping()
}
/*
Basically does the same as `sqlite3 dbPath < data/schema.sql && sqlite3 dbPath < data/seed.sql`
and has limitations (doesn't support multi-line SQL in schema and seed file) but we don't want
to require the user doing manual steps to get going.
*/
func createDB(dbPath string) {
openDB(dbPath)
execFile(SCHEMA_FILE)
execFile(SEED_FILE)
}
func execFile(filePath string) {
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
sql := scanner.Text()
if isCommentOrEmptyLine(sql) {
continue
}
_, err := DB.Exec(sql)
if err != nil {
log.Printf("%q: %s\n", err, sql)
return
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func isCommentOrEmptyLine(sql string) bool {
comment := regexp.MustCompile("^\\s*--.*$")
emptyLine := regexp.MustCompile("^\\s*$")
return comment.MatchString(sql) || emptyLine.MatchString(sql)
}
func createTestDB() {
os.Remove(TEST_DB)
createDB(TEST_DB)
}
func removeTestDB() {
os.Remove(TEST_DB)
}