-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.go
55 lines (52 loc) · 1.01 KB
/
testing.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
package mysql
import (
"io/ioutil"
"os"
"strings"
)
// sourceDirs 按照分号或者冒号分割多个dir
func InitTestDB(name, dsn, sourceDirs string) {
if err := AddDB(
name,
dsn,
10,
10,
); err != nil {
panic(err)
}
for _, dir := range strings.FieldsFunc(sourceDirs, func(r rune) bool {
switch r {
case ':', ';':
return true
}
return false
}) {
log.Debug("source dir", name, dir)
sourceSqlFiles(name, dir)
}
}
func sourceSqlFiles(dbname, dir string) {
sqlDir, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
for _, fi := range sqlDir {
if fi.IsDir() {
continue
}
if strings.HasSuffix(strings.ToUpper(fi.Name()), ".SQL") {
sqlfile := dir + string(os.PathSeparator) + fi.Name()
sourceSql, err := ioutil.ReadFile(sqlfile)
if err != nil {
panic(err)
}
for _, sql := range strings.Split(string(sourceSql), ";") {
if len(strings.TrimSpace(sql)) > 0 {
if _, err := Get(dbname).Execute(sql); err != nil {
panic(err)
}
}
}
}
}
}