Skip to content

Commit

Permalink
Fix current version in empty up state log (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jun 10, 2023
1 parent f08d203 commit d88978d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
89 changes: 89 additions & 0 deletions goose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,46 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/pressly/goose/v3/internal/check"
_ "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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d88978d

Please sign in to comment.