From 2f106453dc402434b03f530380a27a8c08653a08 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Sat, 10 Jun 2023 09:50:46 -0400 Subject: [PATCH 1/3] Fix current version in empty up state --- goose_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ up.go | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/goose_test.go b/goose_test.go index f449d5b76..372372941 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,42 @@ 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 + params := []string{dirFlag, "sqlite3", filepath.Join(tempDir, "sql.db")} + + tt := []struct { + command string + skipCheck bool + output string + }{ + {"up", true, ""}, + {"up", false, "goose: no migrations to run. current version: 3"}, + } + for _, tc := range tt { + params = append(params, tc.command) + got, err := runGoose(params...) + check.NoError(t, err) + if tc.skipCheck { + 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 +295,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 From 738b435be9aa8bf1af5ce41e034aebbbfdf9ab36 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Sat, 10 Jun 2023 10:09:08 -0400 Subject: [PATCH 2/3] move params --- goose_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/goose_test.go b/goose_test.go index 372372941..909307eb3 100644 --- a/goose_test.go +++ b/goose_test.go @@ -94,7 +94,6 @@ func TestIssue532(t *testing.T) { tempDir := t.TempDir() dirFlag := "--dir=" + migrationsDir - params := []string{dirFlag, "sqlite3", filepath.Join(tempDir, "sql.db")} tt := []struct { command string @@ -103,9 +102,10 @@ func TestIssue532(t *testing.T) { }{ {"up", true, ""}, {"up", false, "goose: no migrations to run. current version: 3"}, + {"version", false, "goose: version 3"}, } for _, tc := range tt { - params = append(params, tc.command) + params := []string{dirFlag, "sqlite3", filepath.Join(tempDir, "sql.db"), tc.command} got, err := runGoose(params...) check.NoError(t, err) if tc.skipCheck { From 6698c603941bb8f9262d915a27deee7e7f723261 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Sat, 10 Jun 2023 10:10:01 -0400 Subject: [PATCH 3/3] remove bool --- goose_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/goose_test.go b/goose_test.go index 909307eb3..b3ad14dd8 100644 --- a/goose_test.go +++ b/goose_test.go @@ -96,19 +96,18 @@ func TestIssue532(t *testing.T) { dirFlag := "--dir=" + migrationsDir tt := []struct { - command string - skipCheck bool - output string + command string + output string }{ - {"up", true, ""}, - {"up", false, "goose: no migrations to run. current version: 3"}, - {"version", false, "goose: version 3"}, + {"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.skipCheck { + if tc.output == "" { continue } if !strings.Contains(strings.TrimSpace(got), tc.output) {