mirror of https://github.com/pressly/goose.git
187 lines
4.9 KiB
Go
187 lines
4.9 KiB
Go
package goose
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/pressly/goose/v4/internal/check"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func TestDefaultBinary(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
commands := []string{
|
|
"go build -o ./bin/goose ./cmd/goose",
|
|
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db up",
|
|
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db version",
|
|
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db down",
|
|
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db status",
|
|
"./bin/goose --version",
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := os.Remove("./bin/goose"); err != nil {
|
|
t.Logf("failed to remove %s resources: %v", t.Name(), err)
|
|
}
|
|
if err := os.Remove("./sql.db"); err != nil {
|
|
t.Logf("failed to remove %s resources: %v", t.Name(), err)
|
|
}
|
|
})
|
|
|
|
for _, cmd := range commands {
|
|
args := strings.Split(cmd, " ")
|
|
command := args[0]
|
|
var params []string
|
|
if len(args) > 1 {
|
|
params = args[1:]
|
|
}
|
|
|
|
cmd := exec.Command(command, params...)
|
|
cmd.Env = os.Environ()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIssue293(t *testing.T) {
|
|
t.Parallel()
|
|
// https://github.com/pressly/goose/issues/293
|
|
commands := []string{
|
|
"go build -o ./bin/goose293 ./cmd/goose",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db up",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db version",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db down",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db down",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db version",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db up",
|
|
"./bin/goose293 -dir=examples/sql-migrations sqlite3 issue_293.db status",
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := os.Remove("./bin/goose293"); err != nil {
|
|
t.Logf("failed to remove %s resources: %v", t.Name(), err)
|
|
}
|
|
if err := os.Remove("./issue_293.db"); err != nil {
|
|
t.Logf("failed to remove %s resources: %v", t.Name(), err)
|
|
}
|
|
})
|
|
for _, cmd := range commands {
|
|
args := strings.Split(cmd, " ")
|
|
command := args[0]
|
|
var params []string
|
|
if len(args) > 1 {
|
|
params = args[1:]
|
|
}
|
|
|
|
cmd := exec.Command(command, params...)
|
|
cmd.Env = os.Environ()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLiteBinary(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dir := t.TempDir()
|
|
t.Cleanup(func() {
|
|
if err := os.Remove("./bin/lite-goose"); err != nil {
|
|
t.Logf("failed to remove %s resources: %v", t.Name(), err)
|
|
}
|
|
})
|
|
|
|
// this has to be done outside of the loop
|
|
// since go only supports space separated tags list.
|
|
cmd := exec.Command("go", "build", "-tags='no_postgres no_mysql no_sqlite3'", "-o", "./bin/lite-goose", "./cmd/goose")
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
|
|
}
|
|
|
|
commands := []string{
|
|
fmt.Sprintf("./bin/lite-goose -dir=%s create user_indices sql", dir),
|
|
fmt.Sprintf("./bin/lite-goose -dir=%s fix", dir),
|
|
}
|
|
|
|
for _, cmd := range commands {
|
|
args := strings.Split(cmd, " ")
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
cmd.Env = os.Environ()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCustomBinary(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
commands := []string{
|
|
"go build -o ./bin/custom-goose ./examples/go-migrations",
|
|
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db up",
|
|
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db version",
|
|
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db down",
|
|
"./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db status",
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := os.Remove("./go.db"); err != nil {
|
|
t.Logf("failed to remove %s resouces: %v", t.Name(), err)
|
|
}
|
|
})
|
|
|
|
for _, cmd := range commands {
|
|
args := strings.Split(cmd, " ")
|
|
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
//go:embed examples/sql-migrations/*.sql
|
|
var embedMigrations embed.FS
|
|
|
|
func TestEmbeddedMigrations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
check.NoError(t, err)
|
|
db.SetMaxOpenConns(1)
|
|
|
|
options := &Options{
|
|
Filesystem: embedMigrations,
|
|
}
|
|
p, err := NewProvider(DialectSqlite, db, "examples/sql-migrations", options)
|
|
check.NoError(t, err)
|
|
|
|
t.Run("migration_cycle", func(t *testing.T) {
|
|
err := p.Up(ctx)
|
|
check.NoError(t, err)
|
|
dbVersion, err := p.GetDBVersion(ctx)
|
|
check.NoError(t, err)
|
|
check.Number(t, dbVersion, 3)
|
|
|
|
err = p.Reset(ctx)
|
|
check.NoError(t, err)
|
|
|
|
dbVersion, err = p.GetDBVersion(ctx)
|
|
check.NoError(t, err)
|
|
check.Number(t, dbVersion, 0)
|
|
})
|
|
|
|
t.Run("create_uses_os_fs", func(t *testing.T) {
|
|
// TODO(mf): add this test back.
|
|
})
|
|
}
|