Skip to content

Commit

Permalink
Merge pull request #123 from pressly/goose-lite
Browse files Browse the repository at this point in the history
support build tags to exclude drivers
  • Loading branch information
VojtechVitek authored Nov 13, 2018
2 parents 58aa6a8 + 0f83d35 commit da4711c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 27 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ cmd/goose/goose*
*.test

# Files output by tests
custom-goose
bin
go.db
goose
sql.db


5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
8 changes: 8 additions & 0 deletions cmd/goose/driver_mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build !no_mysql

package main

import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/ziutek/mymysql/godrv"
)
7 changes: 7 additions & 0 deletions cmd/goose/driver_psql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !no_pq

package main

import (
_ "github.com/lib/pq"
)
7 changes: 7 additions & 0 deletions cmd/goose/driver_sqlite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !no_sqlite

package main

import (
_ "github.com/mattn/go-sqlite3"
)
6 changes: 0 additions & 6 deletions cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
18 changes: 9 additions & 9 deletions fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
54 changes: 44 additions & 10 deletions goose_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
package goose

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
)

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 {
Expand All @@ -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 {
Expand Down

0 comments on commit da4711c

Please sign in to comment.