diff --git a/goose_test.go b/goose_test.go index f449d5b76..b3ad14dd8 100644 --- a/goose_test.go +++ b/goose_test.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" @@ -15,6 +16,38 @@ import ( _ "modernc.org/sqlite" ) +const ( + binName = "goose-test" +) + +func TestMain(m *testing.M) { + if runtime.GOOS == "windows" { + log.Fatal("this test is not supported on Windows") + } + dir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + args := []string{ + "build", + "-ldflags=-s -w", + // disable all drivers except sqlite3 + "-tags=no_clickhouse no_mssql no_mysql no_vertica no_postgres", + "-o", binName, + "./cmd/goose", + } + build := exec.Command("go", args...) + out, err := build.CombinedOutput() + if err != nil { + log.Fatalf("failed to build %s binary: %v: %s", binName, err, string(out)) + } + result := m.Run() + defer func() { os.Exit(result) }() + if err := os.Remove(filepath.Join(dir, binName)); err != nil { + log.Printf("failed to remove binary: %v", err) + } +} + func TestDefaultBinary(t *testing.T) { t.Parallel() @@ -52,6 +85,41 @@ func TestDefaultBinary(t *testing.T) { } } +func TestIssue532(t *testing.T) { + t.Parallel() + + migrationsDir := filepath.Join("examples", "sql-migrations") + count := countSQLFiles(t, migrationsDir) + check.Number(t, count, 3) + + tempDir := t.TempDir() + dirFlag := "--dir=" + migrationsDir + + tt := []struct { + command string + output string + }{ + {"up", ""}, + {"up", "goose: no migrations to run. current version: 3"}, + {"version", "goose: version 3"}, + } + for _, tc := range tt { + params := []string{dirFlag, "sqlite3", filepath.Join(tempDir, "sql.db"), tc.command} + got, err := runGoose(params...) + check.NoError(t, err) + if tc.output == "" { + continue + } + if !strings.Contains(strings.TrimSpace(got), tc.output) { + t.Logf("output mismatch for command: %q", tc.command) + t.Logf("got\n%s", strings.TrimSpace(got)) + t.Log("====") + t.Logf("want\n%s", tc.output) + t.FailNow() + } + } +} + func TestIssue293(t *testing.T) { t.Parallel() // https://github.com/pressly/goose/issues/293 @@ -226,3 +294,24 @@ func TestEmbeddedMigrations(t *testing.T) { } }) } + +func runGoose(params ...string) (string, error) { + dir, err := os.Getwd() + if err != nil { + return "", err + } + cmdPath := filepath.Join(dir, binName) + cmd := exec.Command(cmdPath, params...) + out, err := cmd.CombinedOutput() + if err != nil { + return "", fmt.Errorf("%v\n%v", err, string(out)) + } + return string(out), nil +} + +func countSQLFiles(t *testing.T, dir string) int { + t.Helper() + files, err := filepath.Glob(filepath.Join(dir, "*.sql")) + check.NoError(t, err) + return len(files) +} diff --git a/up.go b/up.go index ab5a30630..3dc011d03 100644 --- a/up.go +++ b/up.go @@ -111,7 +111,7 @@ func UpTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) error { } current = m.Version } - if len(migrationsToApply) == 0 && option.applyUpByOne { + if len(migrationsToApply) == 0 { current, err = GetDBVersion(db) if err != nil { return err