forked from vmware-archive/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
137 lines (114 loc) · 3.3 KB
/
parser.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package voyager
import (
"errors"
"regexp"
"strconv"
"strings"
)
var noTxPrefix = regexp.MustCompile(`^\s*--\s+(NO_TRANSACTION)`)
var migrationDirection = regexp.MustCompile(`\.(up|down)\.`)
var goMigrationFuncName = regexp.MustCompile(`(Up|Down)_[0-9]*`)
var ErrCouldNotParseDirection = errors.New("could not parse direction for migration")
type Parser struct {
source Source
}
func NewParser(source Source) *Parser {
return &Parser{
source: source,
}
}
func (p *Parser) ParseMigrationFilename(fileName string) (migration, error) {
var (
migration migration
err error
)
migration.Direction, err = determineDirection(fileName)
if err != nil {
return migration, err
}
migration.Version, err = schemaVersion(fileName)
if err != nil {
return migration, err
}
return migration, nil
}
func (p *Parser) ParseFileToMigration(migrationName string) (migration, error) {
var migrationContents string
migration, err := p.ParseMigrationFilename(migrationName)
if err != nil {
return migration, err
}
migrationBytes, err := p.source.Asset(migrationName)
if err != nil {
return migration, err
}
migrationContents = string(migrationBytes)
migration.Strategy = determineMigrationStrategy(migrationName, migrationContents)
switch migration.Strategy {
case GoMigration:
migration.Name = goMigrationFuncName.FindString(migrationContents)
case SQLNoTransaction:
migration.Statements = splitStatements(migrationContents)
migration.Name = migrationName
case SQLTransaction:
migration.Statements = splitStatements(migrationContents)
migration.Name = migrationName
}
return migration, nil
}
func schemaVersion(assetName string) (int, error) {
regex := regexp.MustCompile(`(\d+)`)
match := regex.FindStringSubmatch(assetName)
return strconv.Atoi(match[1])
}
func determineDirection(migrationName string) (string, error) {
matches := migrationDirection.FindStringSubmatch(migrationName)
if len(matches) < 2 {
return "", ErrCouldNotParseDirection
}
return matches[1], nil
}
func determineMigrationStrategy(migrationName string, migrationContents string) Strategy {
if strings.HasSuffix(migrationName, ".go") {
return GoMigration
} else {
if noTxPrefix.MatchString(migrationContents) {
return SQLNoTransaction
}
}
return SQLTransaction
}
func splitStatements(migrationContents string) []string {
var (
fileStatements []string
migrationStatements []string
)
fileStatements = append(fileStatements, strings.Split(migrationContents, ";")...)
// last string is empty
if strings.TrimSpace(fileStatements[len(fileStatements)-1]) == "" {
fileStatements = fileStatements[:len(fileStatements)-1]
}
var isSqlStatement bool = false
var sqlStatement string
for _, statement := range fileStatements {
statement = strings.TrimSpace(statement)
if statement == "BEGIN" || statement == "COMMIT" {
continue
}
if strings.Contains(statement, "BEGIN") {
isSqlStatement = true
sqlStatement = statement + ";"
} else {
if isSqlStatement {
sqlStatement = strings.Join([]string{sqlStatement, statement, ";"}, "")
if strings.HasPrefix(statement, "$$") {
migrationStatements = append(migrationStatements, sqlStatement)
isSqlStatement = false
}
} else {
migrationStatements = append(migrationStatements, statement)
}
}
}
return migrationStatements
}