这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/commands/migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type migrateStatusOptions struct {
func (o *migrateStatusOptions) run() (*migrate.Status, error) {
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.Logger)
if err != nil {
return nil, errors.Wrap(err, "cannot create migrate instance")
return nil, err
}
status, err := executeStatus(migrateDrv)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion cli/migrate/source/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ func (f *File) Open(url string, logger *log.Logger) (source.Driver, error) {
if err != nil {
continue // ignore files that we can't parse
}

ok, err := source.IsEmptyFile(m, p)
if err != nil {
return nil, err
}
if !ok {
continue
}
err = nf.migrations.Append(m)
if err != nil {
return nil, err
Expand Down
45 changes: 25 additions & 20 deletions cli/migrate/source/parse.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package source

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"

yaml "github.com/ghodss/yaml"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -44,18 +44,6 @@ func Parse(raw string, directory string) (*Migration, error) {
} else {
return nil, errors.New("Invalid Direction type")
}
data, err := ioutil.ReadFile(filepath.Join(directory, raw))
if err != nil {
return nil, err
}
var t []interface{}
err = yaml.Unmarshal(data, &t)
if err != nil {
return nil, err
}
if len(t) == 0 {
return nil, errors.New("Empty metadata file")
}
} else if m[4] == "sql" {
if m[3] == "up" {
direction = Up
Expand All @@ -64,13 +52,6 @@ func Parse(raw string, directory string) (*Migration, error) {
} else {
return nil, errors.New("Invalid Direction type")
}
data, err := ioutil.ReadFile(filepath.Join(directory, raw))
if err != nil {
return nil, err
}
if string(data[:]) == "" {
return nil, errors.New("Empty SQL file")
}
}

return &Migration{
Expand All @@ -82,3 +63,27 @@ func Parse(raw string, directory string) (*Migration, error) {
}
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
}