github.com/SigNoz/golang-migrate/v4@v4.0.0-20231005133642-7493dbaf5f5b/source/parse.go (about)

     1  package source
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  )
     8  
     9  var (
    10  	ErrParse = fmt.Errorf("no match")
    11  )
    12  
    13  var (
    14  	DefaultParse = Parse
    15  	DefaultRegex = Regex
    16  )
    17  
    18  // Regex matches the following pattern:
    19  //
    20  //	123_name.up.ext
    21  //	123_name.down.ext
    22  var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)
    23  
    24  // Parse returns Migration for matching Regex pattern.
    25  func Parse(raw string) (*Migration, error) {
    26  	m := Regex.FindStringSubmatch(raw)
    27  	if len(m) == 5 {
    28  		versionUint64, err := strconv.ParseUint(m[1], 10, 64)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		return &Migration{
    33  			Version:    uint(versionUint64),
    34  			Identifier: m[2],
    35  			Direction:  Direction(m[3]),
    36  			Raw:        raw,
    37  		}, nil
    38  	}
    39  	return nil, ErrParse
    40  }