diff --git a/create.go b/create.go index 1a8bb74c0..8634763c3 100644 --- a/create.go +++ b/create.go @@ -2,6 +2,7 @@ package goose import ( "database/sql" + "errors" "fmt" "os" "path/filepath" @@ -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 } @@ -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) diff --git a/goose_test.go b/goose_test.go index b3ad14dd8..6dede3a98 100644 --- a/goose_test.go +++ b/goose_test.go @@ -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() @@ -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) } @@ -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) } diff --git a/migrate.go b/migrate.go index fb1500af1..96c2cccf4 100644 --- a/migrate.go +++ b/migrate.go @@ -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. @@ -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 @@ -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