Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding verbose output for statement execution #138

Merged
merged 6 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/joho/godotenv v1.3.0
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.5.2
github.com/mattn/go-sqlite3 v1.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gG
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d h1:cVtBfNW5XTHiKQe7jDaDBSh/EVM4XLPutLAGboIXuM0=
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ func NewApp() *cli.App {
{
Name: "up",
Usage: "Create database (if necessary) and migrate to the latest version",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "verbose, v",
Usage: "print the result of each statement execution",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Verbose = c.Bool("verbose")
return db.CreateAndMigrate()
}),
},
Expand All @@ -96,15 +103,29 @@ func NewApp() *cli.App {
{
Name: "migrate",
Usage: "Migrate to the latest version",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "verbose, v",
Usage: "print the result of each statement execution",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Verbose = c.Bool("verbose")
return db.Migrate()
}),
},
{
Name: "rollback",
Aliases: []string{"down"},
Usage: "Rollback the most recent migration",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "verbose, v",
Usage: "print the result of each statement execution",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Verbose = c.Bool("verbose")
return db.Rollback()
}),
},
Expand Down
11 changes: 9 additions & 2 deletions pkg/dbmate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type DB struct {
DatabaseURL *url.URL
MigrationsDir string
SchemaFile string
Verbose bool
WaitBefore bool
WaitInterval time.Duration
WaitTimeout time.Duration
Expand Down Expand Up @@ -304,8 +305,11 @@ func (db *DB) Migrate() error {

execMigration := func(tx Transaction) error {
// run actual migration
if _, err := tx.Exec(up.Contents); err != nil {
result, err := tx.Exec(up.Contents)
if err != nil {
return err
} else if db.Verbose {
printVerbose(result)
}

// record migration
Expand Down Expand Up @@ -425,8 +429,11 @@ func (db *DB) Rollback() error {

execMigration := func(tx Transaction) error {
// rollback migration
if _, err := tx.Exec(down.Contents); err != nil {
result, err := tx.Exec(down.Contents)
if err != nil {
return err
} else if db.Verbose {
printVerbose(result)
}

// remove migration record
Expand Down
22 changes: 21 additions & 1 deletion pkg/dbmate/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/kami-zh/go-capturer"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -161,9 +162,10 @@ func checkWaitCalled(t *testing.T, u *url.URL, command func() error) {
u.Host = oldHost
}

func TestWaitBefore(t *testing.T) {
func testWaitBefore(t *testing.T, verbose bool) {
u := postgresTestURL(t)
db := newTestDB(t, u)
db.Verbose = verbose
db.WaitBefore = true
// so that checkWaitCalled returns quickly
db.WaitInterval = time.Millisecond
Expand Down Expand Up @@ -200,6 +202,24 @@ func TestWaitBefore(t *testing.T) {
checkWaitCalled(t, u, db.DumpSchema)
}

func TestWaitBefore(t *testing.T) {
testWaitBefore(t, false)
}

func TestWaitBeforeVerbose(t *testing.T) {
output := capturer.CaptureOutput(func() {
testWaitBefore(t, true)
})
require.Contains(t, output,
`Applying: 20151129054053_test_migration.sql
Rows affected: 1
Applying: 20200227231541_test_posts.sql
Rows affected: 0`)
require.Contains(t, output,
`Rolling back: 20200227231541_test_posts.sql
Rows affected: 0`)
}

func testURLs(t *testing.T) []*url.URL {
return []*url.URL{
postgresTestURL(t),
Expand Down
11 changes: 11 additions & 0 deletions pkg/dbmate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ func queryColumn(db *sql.DB, query string) ([]string, error) {

return result, nil
}

func printVerbose(result sql.Result) {
lastInsertId, err := result.LastInsertId()
if err == nil {
fmt.Printf("Last insert ID: %d\n", lastInsertId)
}
rowsAffected, err := result.RowsAffected()
if err == nil {
fmt.Printf("Rows affected: %d\n", rowsAffected)
}
}