-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
89 lines (72 loc) · 1.77 KB
/
helpers.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
package fixture
import (
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
)
func isPathExist(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}
func findFixtureData(fixtureDataDir string, table *table) *fixtureData {
if !isPathExist(fixtureDataDir) {
panic(ErrFixtureDataDirNotFound)
}
possibleNames := make([]string, 0)
for ext := range extToDataFmtMapping {
possibleNames = append(possibleNames, table.name+ext)
}
var found string
for _, name := range possibleNames {
absPath := path.Join(fixtureDataDir, name)
if isPathExist(absPath) {
if found != "" {
panic(fmt.Sprintf("multiple formats of fixture data found for table '%s'", table.name))
}
found = absPath
}
}
if found == "" {
panic(fmt.Sprintf("fixture data not found for table '%s'", table.name))
}
return &fixtureData{
Format: extToDataFmtMapping[path.Ext(found)],
Path: found,
}
}
var rule = regexp.MustCompile("CREATE\\s.*TABLE\\s(.*)\\(.*")
func parseSchemaFile(filename string) []*table {
buf, err := ioutil.ReadFile(filename)
panicOnErr(err)
tables := make([]*table, 0)
for _, createSQL := range strings.Split(string(buf), ";") {
createSQL = strings.TrimSpace(createSQL)
if createSQL == "" {
continue
}
groups := rule.FindStringSubmatch(createSQL)
if len(groups) != 2 {
panic(fmt.Sprintf("cannot extract table name from sql '%s'", createSQL))
}
tb := &table{
name: trimTableName(groups[1]),
createSQL: createSQL,
}
tables = append(tables, tb)
}
return tables
}
func trimTableName(n string) string {
n = strings.Replace(n, "`", "", len(n))
n = strings.Replace(n, "'", "", len(n))
n = strings.Replace(n, "\"", "", len(n))
return strings.TrimSpace(n)
}