Skip to content

Commit

Permalink
fix: return error when no migration files are found or dir is not a d…
Browse files Browse the repository at this point in the history
…irectory (#539)
  • Loading branch information
craigpangea committed Jun 29, 2023
1 parent d21bf4f commit 7d9fbaf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
8 changes: 4 additions & 4 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goose

import (
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -25,11 +26,12 @@ func SetSequential(s bool) {

// Create writes a new blank migration file.
func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, migrationType string) error {
var version string
version := time.Now().Format(timestampFormat)

if sequential {
// always use DirFS here because it's modifying operation
migrations, err := collectMigrationsFS(osFS{}, dir, minVersion, maxVersion)
if err != nil {
if err != nil && !errors.Is(err, ErrNoMigrationFiles) {
return err
}

Expand All @@ -43,8 +45,6 @@ func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, m
} else {
version = fmt.Sprintf(seqVersionTemplate, int64(1))
}
} else {
version = time.Now().Format(timestampFormat)
}

filename := fmt.Sprintf("%v_%v.%v", version, snakeCase(name), migrationType)
Expand Down
17 changes: 15 additions & 2 deletions goose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ func TestIssue293(t *testing.T) {
}
}

func TestIssue336(t *testing.T) {
t.Parallel()
// error when no migrations are found
// https://github.com/pressly/goose/issues/336

tempDir := t.TempDir()
params := []string{"--dir=" + tempDir, "sqlite3", filepath.Join(tempDir, "sql.db"), "up"}

_, err := runGoose(params...)
check.HasError(t, err)
check.Contains(t, err.Error(), "no migration files found")
}

func TestLiteBinary(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -245,7 +258,7 @@ func TestEmbeddedMigrations(t *testing.T) {
t.Cleanup(func() { SetBaseFS(nil) })

t.Run("Migration cycle", func(t *testing.T) {
if err := Up(db, ""); err != nil {
if err := Up(db, "."); err != nil {
t.Errorf("Failed to run 'up' migrations: %s", err)
}

Expand All @@ -258,7 +271,7 @@ func TestEmbeddedMigrations(t *testing.T) {
t.Errorf("Expected version 3 after 'up', got %d", ver)
}

if err := Reset(db, ""); err != nil {
if err := Reset(db, "."); err != nil {
t.Errorf("Failed to run 'down' migrations: %s", err)
}

Expand Down
14 changes: 12 additions & 2 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

var (
// ErrNoMigrationFiles when no migration files have been found.
ErrNoMigrationFiles = errors.New("no migration files found")
// ErrNoCurrentVersion when a current migration version is not found.
ErrNoCurrentVersion = errors.New("no current version found")
// ErrNoNextVersion when the next migration version is not found.
Expand Down Expand Up @@ -192,8 +194,12 @@ func register(
}

func collectMigrationsFS(fsys fs.FS, dirpath string, current, target int64) (Migrations, error) {
if _, err := fs.Stat(fsys, dirpath); errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("%s directory does not exist", dirpath)
if _, err := fs.Stat(fsys, dirpath); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("%s directory does not exist", dirpath)
}

return nil, err
}

var migrations Migrations
Expand Down Expand Up @@ -251,6 +257,10 @@ func collectMigrationsFS(fsys fs.FS, dirpath string, current, target int64) (Mig
}
}

if len(migrations) == 0 {
return nil, ErrNoMigrationFiles
}

migrations = sortAndConnectMigrations(migrations)

return migrations, nil
Expand Down

0 comments on commit 7d9fbaf

Please sign in to comment.