migrate/source/parse.go

35 lines
689 B
Go

package source
import (
"fmt"
"regexp"
"strconv"
)
var ErrParse = fmt.Errorf("no match")
var DefaultParse = Parse
var DefaultRegex = Regex
// filename example: `123_name.up.ext`
// filename example: `123_name.down.ext`
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)
func Parse(raw string) (*Migration, error) {
m := Regex.FindStringSubmatch(raw)
if len(m) == 5 {
versionUint64, err := strconv.ParseUint(m[1], 10, 32)
if err != nil {
return nil, err
}
return &Migration{
Version: uint(versionUint64),
Identifier: m[2],
Direction: Direction(m[3]),
Raw: raw,
}, nil
}
return nil, ErrParse
}