2017-02-08 06:01:29 +00:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2017-02-10 03:42:48 +00:00
|
|
|
var (
|
|
|
|
ErrParse = fmt.Errorf("no match")
|
|
|
|
)
|
2017-02-08 06:01:29 +00:00
|
|
|
|
2017-02-10 03:42:48 +00:00
|
|
|
var (
|
|
|
|
DefaultParse = Parse
|
|
|
|
DefaultRegex = Regex
|
|
|
|
)
|
2017-02-08 06:01:29 +00:00
|
|
|
|
2017-02-10 03:42:48 +00:00
|
|
|
// Regex matches the following pattern:
|
|
|
|
// 123_name.up.ext
|
|
|
|
// 123_name.down.ext
|
2017-02-08 06:01:29 +00:00
|
|
|
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)
|
|
|
|
|
2017-02-10 03:42:48 +00:00
|
|
|
// Parse returns Migration for matching Regex pattern.
|
2017-02-08 06:01:29 +00:00
|
|
|
func Parse(raw string) (*Migration, error) {
|
|
|
|
m := Regex.FindStringSubmatch(raw)
|
|
|
|
if len(m) == 5 {
|
2017-04-14 00:57:22 +00:00
|
|
|
versionUint64, err := strconv.ParseUint(m[1], 10, 64)
|
2017-02-08 06:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Migration{
|
|
|
|
Version: uint(versionUint64),
|
|
|
|
Identifier: m[2],
|
|
|
|
Direction: Direction(m[3]),
|
|
|
|
Raw: raw,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, ErrParse
|
|
|
|
}
|