Merge pull request #123 from pressly/goose-lite

support build tags to exclude drivers
pull/126/head v2.4.1
Vojtech Vitek 2018-11-13 13:24:54 -05:00 committed by GitHub
commit da4711c369
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 27 deletions

5
.gitignore vendored
View File

@ -4,7 +4,8 @@ cmd/goose/goose*
*.test
# Files output by tests
custom-goose
bin
go.db
goose
sql.db

View File

@ -30,6 +30,11 @@ Goose is a database migration tool. Manage your database schema by creating incr
This will install the `goose` binary to your `$GOPATH/bin` directory.
For a lite version of the binary without DB connection dependent commands, use the exclusive build tags:
$ go build -tags='no_mysql no_sqlite no_psql' -i -o goose ./cmd/goose
# Usage
```

View File

@ -0,0 +1,8 @@
// +build !no_mysql
package main
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/ziutek/mymysql/godrv"
)

7
cmd/goose/driver_psql.go Normal file
View File

@ -0,0 +1,7 @@
// +build !no_pq
package main
import (
_ "github.com/lib/pq"
)

View File

@ -0,0 +1,7 @@
// +build !no_sqlite
package main
import (
_ "github.com/mattn/go-sqlite3"
)

View File

@ -7,12 +7,6 @@ import (
"os"
"github.com/pressly/goose"
// Init DB drivers.
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
_ "github.com/ziutek/mymysql/godrv"
)
var (

View File

@ -20,12 +20,12 @@ func TestFix(t *testing.T) {
defer os.Remove("goose") // clean up
commands := []string{
"go build -i -o goose ./cmd/goose",
fmt.Sprintf("./goose -dir=%s create create_table", dir),
fmt.Sprintf("./goose -dir=%s create add_users", dir),
fmt.Sprintf("./goose -dir=%s create add_indices", dir),
fmt.Sprintf("./goose -dir=%s create update_users", dir),
fmt.Sprintf("./goose -dir=%s fix", dir),
"go build -i -o ./bin/goose ./cmd/goose",
fmt.Sprintf("./bin/goose -dir=%s create create_table", dir),
fmt.Sprintf("./bin/goose -dir=%s create add_users", dir),
fmt.Sprintf("./bin/goose -dir=%s create add_indices", dir),
fmt.Sprintf("./bin/goose -dir=%s create update_users", dir),
fmt.Sprintf("./bin/goose -dir=%s fix", dir),
}
for _, cmd := range commands {
@ -52,9 +52,9 @@ func TestFix(t *testing.T) {
// add more migrations and then fix it
commands = []string{
fmt.Sprintf("./goose -dir=%s create remove_column", dir),
fmt.Sprintf("./goose -dir=%s create create_books_table", dir),
fmt.Sprintf("./goose -dir=%s fix", dir),
fmt.Sprintf("./bin/goose -dir=%s create remove_column", dir),
fmt.Sprintf("./bin/goose -dir=%s create create_books_table", dir),
fmt.Sprintf("./bin/goose -dir=%s fix", dir),
}
for _, cmd := range commands {

View File

@ -1,6 +1,9 @@
package goose
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
@ -8,11 +11,42 @@ import (
func TestDefaultBinary(t *testing.T) {
commands := []string{
"go build -i -o goose ./cmd/goose",
"./goose -dir=examples/sql-migrations sqlite3 sql.db up",
"./goose -dir=examples/sql-migrations sqlite3 sql.db version",
"./goose -dir=examples/sql-migrations sqlite3 sql.db down",
"./goose -dir=examples/sql-migrations sqlite3 sql.db status",
"go build -i -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",
}
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)
}
}
}
func TestLiteBinary(t *testing.T) {
dir, err := ioutil.TempDir("", "tmptest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
defer os.Remove("./bin/goose") // clean up
commands := []string{
fmt.Sprintf("./bin/goose -dir=%s create user_indices sql", dir),
fmt.Sprintf("./bin/goose -dir=%s fix", dir),
}
// this has to be done outside of the loop
// since go only supports space separated tags list.
cmd := "go build -tags='no_mysql no_sqlite no_psql' -i -o ./bin/goose ./cmd/goose"
out, err := exec.Command("go", "build", "-tags='no_mysql no_sqlite no_psql'", "-i", "-o", "./bin/goose", "./cmd/goose").CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
for _, cmd := range commands {
@ -26,11 +60,11 @@ func TestDefaultBinary(t *testing.T) {
func TestCustomBinary(t *testing.T) {
commands := []string{
"go build -i -o custom-goose ./examples/go-migrations",
"./custom-goose -dir=examples/go-migrations sqlite3 go.db up",
"./custom-goose -dir=examples/go-migrations sqlite3 go.db version",
"./custom-goose -dir=examples/go-migrations sqlite3 go.db down",
"./custom-goose -dir=examples/go-migrations sqlite3 go.db status",
"go build -i -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",
}
for _, cmd := range commands {