graphql-engine/cli/migrate/source/parse.go
Kali Vara Purushotham Santhati 489835eb36 cli: metadata apply,export enhancements
- modified the `dry-run` flag of metadata apply command.
- added new flag `o` which takes "json" or "yaml" as parameters for metadata apply command.
- added new flag `o` which takes "json" or "yaml" as parameters for metadata export command. It outputs the metadata from server to stdout in form of  json or yaml and won't change the project's metadata.
- added deprecation warnings for `--from-file` flag
- added info message for describing change of behavior of `--dry-run` flag
- v3 metadata objects like `rest_endpoints` was also added to list of metadata objects in config v2 (fix)
- the order in which metadata objects are appended to metadata objects list matter when using `--dry-run` flag, refactored this order to match server metadata.
- `metadata apply` command can now accept json/yaml metadata from io pipe
- config v3 `metadata apply` didn't show any information to user when applied metadata is inconsistent, this is addressed.
- removed `github.com/ghodss/yaml` dependency from repo
- version metadata object was added twice during intialization (fix)

Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 2316f519eb40645efd86ffee2a85d3c90543ec17
2021-05-14 19:09:59 +00:00

124 lines
2.7 KiB
Go

package source
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"github.com/goccy/go-yaml"
"github.com/pkg/errors"
)
var (
ErrParse = fmt.Errorf("no match")
)
var (
DefaultParse = Parse
DefaultParsev2 = Parsev2
DefaultRegex = Regex
)
// Parser to parse source files
type Parser func(raw string) (*Migration, error)
// Regex matches the following pattern:
// 123_name.up.ext
// 123_name.down.ext
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)
var Regexv2 = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(sql)$`)
// Parse returns Migration for matching Regex pattern.
func Parse(raw string) (*Migration, error) {
var direction Direction
m := Regex.FindStringSubmatch(raw)
if len(m) == 5 {
versionUint64, err := strconv.ParseUint(m[1], 10, 64)
if err != nil {
return nil, err
}
// Have different direction type for yaml and sql
if m[4] == "yaml" {
if m[3] == "up" {
direction = MetaUp
} else if m[3] == "down" {
direction = MetaDown
} else {
return nil, errors.New("Invalid Direction type")
}
} else if m[4] == "sql" {
if m[3] == "up" {
direction = Up
} else if m[3] == "down" {
direction = Down
} else {
return nil, errors.New("Invalid Direction type")
}
}
return &Migration{
Version: versionUint64,
Identifier: m[2],
Direction: direction,
}, nil
}
return nil, ErrParse
}
// Parsev2 returns Migration for matching Regex (only sql) pattern.
func Parsev2(raw string) (*Migration, error) {
var direction Direction
m := Regexv2.FindStringSubmatch(raw)
if len(m) == 5 {
versionUint64, err := strconv.ParseUint(m[1], 10, 64)
if err != nil {
return nil, err
}
// Have different direction type for sql
if m[4] == "sql" {
if m[3] == "up" {
direction = Up
} else if m[3] == "down" {
direction = Down
} else {
return nil, errors.New("Invalid Direction type")
}
}
return &Migration{
Version: versionUint64,
Identifier: m[2],
Direction: direction,
}, nil
}
return nil, ErrParse
}
// Validate file to check for empty sql or yaml content.
func IsEmptyFile(m *Migration, directory string) (bool, error) {
data, err := ioutil.ReadFile(filepath.Join(directory, m.Raw))
if err != nil {
return false, errors.Wrapf(err, "cannot read file %s", m.Raw)
}
switch direction := m.Direction; direction {
case MetaUp, MetaDown:
var t []interface{}
err = yaml.Unmarshal(data, &t)
if err != nil {
return false, errors.Wrapf(err, "invalid yaml file: %s", m.Raw)
}
if len(t) == 0 {
return false, nil
}
case Up, Down:
if string(data[:]) == "" {
return false, nil
}
}
return true, nil
}