Skip to content

Commit

Permalink
Merge pull request #1 from macdabby/master
Browse files Browse the repository at this point in the history
Make code more maintainable
  • Loading branch information
macdabby authored Apr 11, 2018
2 parents b0454a4 + 8a4b517 commit 43204d9
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 168 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Goose is a database migration tool. Manage your database schema by creating incr

### Goals of this fork

`github.com/pressly/goose` is a fork of `bitbucket.org/liamstask/goose` with the following changes:
`github.com/discovery-digital/goose` is a fork of `bitbucket.org/liamstask/goose` with the following changes:
- No config files
- [Default goose binary](./cmd/goose/main.go) can migrate SQL files only
- Go migrations:
Expand All @@ -28,7 +28,7 @@ Goose is a database migration tool. Manage your database schema by creating incr

# Install

$ go get -u github.com/pressly/goose/cmd/goose
$ go get -u github.com/discovery-digital/goose/cmd/goose

This will install the `goose` binary to your `$GOPATH/bin` directory.

Expand Down Expand Up @@ -205,7 +205,7 @@ language plpgsql;
## Go Migrations

1. Create your own goose binary, see [example](./examples/go-migrations)
2. Import `github.com/pressly/goose`
2. Import `github.com/discovery-digital/goose`
3. Register your migration functions
4. Run goose command, ie. `goose.Up(db *sql.DB, dir string)`

Expand All @@ -217,7 +217,7 @@ package migrations
import (
"database/sql"

"github.com/pressly/goose"
"github.com/discovery-digital/goose"
)

func init() {
Expand Down Expand Up @@ -245,7 +245,7 @@ func Down(tx *sql.Tx) error {

Licensed under [MIT License](./LICENSE)

[GoDoc]: https://godoc.org/github.com/pressly/goose
[GoDoc Widget]: https://godoc.org/github.com/pressly/goose?status.svg
[Travis]: https://travis-ci.org/pressly/goose
[Travis Widget]: https://travis-ci.org/pressly/goose.svg?branch=master
[GoDoc]: https://godoc.org/github.com/discovery-digital/goose
[GoDoc Widget]: https://godoc.org/github.com/discovery-digital/goose?status.svg
[Travis]: https://travis-ci.org/discovery-digital/goose
[Travis Widget]: https://travis-ci.org/discovery-digital/goose.svg?branch=master
122 changes: 0 additions & 122 deletions cmd/goose/main.go

This file was deleted.

9 changes: 9 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/discovery-digital/goose"
)

func main() {
goose.Command()
}
24 changes: 7 additions & 17 deletions examples/go-migrations/main.go → command.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main
package goose

import (
"database/sql"
"flag"
"log"
"os"

"github.com/pressly/goose"

// Init DB drivers.
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
Expand All @@ -20,14 +18,14 @@ var (
dir = flags.String("dir", ".", "directory with migration files")
)

func main() {
func Command() {
flags.Usage = usage
flags.Parse(os.Args[1:])

args := flags.Args()

if len(args) > 1 && args[0] == "create" {
if err := goose.Run("create", nil, *dir, args[1:]...); err != nil {
if err := Run("create", nil, *dir, args[1:]...); err != nil {
log.Fatalf("goose run: %v", err)
}
return
Expand All @@ -47,23 +45,13 @@ func main() {

switch driver {
case "postgres", "mysql", "sqlite3", "redshift":
if err := goose.SetDialect(driver); err != nil {
if err := SetDialect(driver); err != nil {
log.Fatal(err)
}
default:
log.Fatalf("%q driver not supported\n", driver)
}

switch dbstring {
case "":
log.Fatalf("-dbstring=%q not supported\n", dbstring)
default:
}

if driver == "redshift" {
driver = "postgres"
}

db, err := sql.Open(driver, dbstring)
if err != nil {
log.Fatalf("-dbstring=%q: %v\n", dbstring, err)
Expand All @@ -74,7 +62,7 @@ func main() {
arguments = append(arguments, args[3:]...)
}

if err := goose.Run(command, db, *dir, arguments...); err != nil {
if err := Run(command, db, *dir, arguments...); err != nil {
log.Fatalf("goose run: %v", err)
}
}
Expand Down Expand Up @@ -104,6 +92,7 @@ Examples:
goose postgres "user=postgres dbname=postgres sslmode=disable" status
goose mysql "user:password@/dbname?parseTime=true" status
goose redshift "postgres://user:[email protected]:5439/db" status
goose tidb "user:password@/dbname?parseTime=true" status
Options:
`
Expand All @@ -115,6 +104,7 @@ Commands:
down Roll back the version by 1
down-to VERSION Roll back to a specific VERSION
redo Re-run the latest migration
reset Roll back all migrations
status Dump the migration status for the current DB
version Print the current version of the database
create NAME [sql|go] Creates new migration file with next version
Expand Down
2 changes: 1 addition & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var goSQLMigrationTemplate = template.Must(template.New("goose.go-migration").Pa
import (
"database/sql"
"github.com/pressly/goose"
"github.com/discovery-digital/goose"
)
func init() {
Expand Down
13 changes: 4 additions & 9 deletions examples/go-migrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ $

## Best practice: Split migrations into a standalone package

1. Move [main.go](main.go) into your `cmd/` directory
1. Move [main.go](main.go) into your `src/cmd/` directory

2. Rename package name in all `*_.go` migration files from `main` to `migrations`.
2. Adjust the imports to the paths for your project

3. Import this `migrations` package from your custom [cmd/main.go](main.go) file:
3. Create `src/migrations/` directory with your migrations named `#######_migration_name.go` with the package declared as `migrations`.

```go
import (
// Invoke init() functions within migrations pkg.
_ "github.com/pressly/goose/example/migrations-go"
)
```
4. Build the go package: `$ go build -o goose src/cmd/*.go`
12 changes: 12 additions & 0 deletions examples/go-migrations/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
// Import your migrations directory here
_ "github.com/discovery-digital/goose/examples/go-migrations/migrations"
// In your repo, replace "goose" line above with the path to goose:
"github.com/discovery-digital/goose"
)

func main() {
goose.Command()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main
package migrations

import (
"database/sql"

"github.com/pressly/goose"
"github.com/discovery-digital/goose"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion examples/sql-migrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
See [this example](../go-migrations) for Go migrations.

```bash
$ go get -u github.com/pressly/goose/cmd/goose
$ go get -u github.com/discovery-digital/goose/cmd/goose
```

```bash
Expand Down
12 changes: 6 additions & 6 deletions goose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestDefaultBinary(t *testing.T) {
commands := []string{
"go build -i -o goose ./cmd/goose",
"go build -i -o goose ./cmd",
"./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",
Expand All @@ -26,11 +26,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 custom-goose ./examples/go-migrations/custom.go",
"./custom-goose -dir=examples/go-migrations/migrations sqlite3 go.db up",
"./custom-goose -dir=examples/go-migrations/migrations sqlite3 go.db version",
"./custom-goose -dir=examples/go-migrations/migrations sqlite3 go.db down",
"./custom-goose -dir=examples/go-migrations/migrations sqlite3 go.db status",
}

for _, cmd := range commands {
Expand Down
2 changes: 1 addition & 1 deletion migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (m *Migration) run(db *sql.DB, direction bool) error {

case ".go":
if !m.Registered {
log.Fatalf("failed to apply Go migration %q: Go functions must be registered and built into a custom binary (see https://github.com/pressly/goose/tree/master/examples/go-migrations)", m.Source)
log.Fatalf("failed to apply Go migration %q: Go functions must be registered and built into a custom binary (see https://github.com/discovery-digital/goose/tree/master/examples/go-migrations)", m.Source)
}
tx, err := db.Begin()
if err != nil {
Expand Down

0 comments on commit 43204d9

Please sign in to comment.