Skip to content

Commit

Permalink
adds validate command
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed May 9, 2023
1 parent edd86c8 commit c32c418
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Create(

version := now.Format(timestampFormat)
if opt.Sequential {
sources, err := Collect(osFS{}, dir, true, nil)
sources, err := collect(osFS{}, dir, true, nil)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Fix(dir string) ([]FixResult, error) {
if dir == "" {
return nil, fmt.Errorf("dir is required")
}
sources, err := Collect(osFS{}, dir, true, nil)
sources, err := collect(osFS{}, dir, true, nil)
if err != nil {
return nil, err
}
Expand Down
15 changes: 12 additions & 3 deletions internal/cli/cmd_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"text/tabwriter"

"github.com/peterbourgon/ff/v3/ffcli"
"github.com/pressly/goose/v4"
"github.com/pressly/goose/v4/internal/migrationstats"
"github.com/pressly/goose/v4/internal/migrationstats/migrationstatsos"
)
Expand Down Expand Up @@ -48,9 +49,17 @@ func (c *validateCmd) Exec(ctx context.Context, args []string) error {
if dir == "" {
return fmt.Errorf("goose validate requires a migrations directory: %w", errNoDir)
}

filenames := []string{} // TODO(mf): fix me

sources, err := goose.Collect(dir, c.excludeFilenames)
if err != nil {
return err
}
if len(sources) == 0 {
return fmt.Errorf("goose: no migration sources found in %q", dir)
}
filenames := make([]string, 0, len(sources))
for _, src := range sources {
filenames = append(filenames, src.Fullpath)
}
fileWalker := migrationstatsos.NewFileWalker(filenames...)
stats, err := migrationstats.GatherStats(fileWalker, false)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewProvider(dbDialect Dialect, db *sql.DB, opt Options) (*Provider, error)
if opt.LockMode != LockModeNone && !store.CanLock() {
return nil, fmt.Errorf("locking is not supported for %s", dbDialect)
}
sources, err := Collect(opt.Filesystem, opt.Dir, true, opt.ExcludeFilenames)
sources, err := collect(opt.Filesystem, opt.Dir, true, opt.ExcludeFilenames)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion source.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (t SourceType) String() string {
}
}

// Source represents a single migration source file on disk.
type Source struct {
// Full path to the migration file.
//
Expand All @@ -40,7 +41,12 @@ type Source struct {
Type SourceType
}

func Collect(fsys fs.FS, dir string, strict bool, excludes []string) ([]*Source, error) {
// Collect returns a slice of Sources found in the given directory.
func Collect(dir string, excludes []string) ([]*Source, error) {
return collect(osFS{}, dir, false, excludes)
}

func collect(fsys fs.FS, dir string, strict bool, excludes []string) ([]*Source, error) {
if _, err := fs.Stat(fsys, dir); errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("directory does not exist: %s", dir)
}
Expand Down

0 comments on commit c32c418

Please sign in to comment.