Skip to content

Commit

Permalink
prints the migrate/rollback execution time (#550)
Browse files Browse the repository at this point in the history
Example output
```
Applying: 20230110131043_change-updated_at-fields.sql
Applied: 20230110131043_change-updated_at-fields.sql in 45.098471ms
```
  • Loading branch information
thinhbuzz authored Jun 4, 2024
1 parent 0e1b2d0 commit 9107c09
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Dropping: myapp_test
$ dbmate -e TEST_DATABASE_URL --no-dump-schema up
Creating: myapp_test
Applying: 20151127184807_create_users_table.sql
Applied: 20151127184807_create_users_table.sql in 123µs
```

Alternatively, you can specify the url directly on the command line:
Expand Down Expand Up @@ -343,6 +344,7 @@ Run `dbmate up` to run any pending migrations.
$ dbmate up
Creating: myapp_development
Applying: 20151127184807_create_users_table.sql
Applied: 20151127184807_create_users_table.sql in 123µs
Writing: ./db/schema.sql
```

Expand Down Expand Up @@ -371,6 +373,7 @@ Run `dbmate rollback` to roll back the most recent migration:
```sh
$ dbmate rollback
Rolling back: 20151127184807_create_users_table.sql
Rolled back: 20151127184807_create_users_table.sql in 123µs
Writing: ./db/schema.sql
```

Expand Down
10 changes: 10 additions & 0 deletions pkg/dbmate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ func (db *DB) Migrate() error {
for _, migration := range pendingMigrations {
fmt.Fprintf(db.Log, "Applying: %s\n", migration.FileName)

start := time.Now()

parsed, err := migration.Parse()
if err != nil {
return err
Expand All @@ -403,6 +405,9 @@ func (db *DB) Migrate() error {
err = execMigration(sqlDB)
}

elapsed := time.Since(start)
fmt.Fprintf(db.Log, "Applied: %s in %s\n", migration.FileName, elapsed)

if err != nil {
return err
}
Expand Down Expand Up @@ -541,6 +546,8 @@ func (db *DB) Rollback() error {

fmt.Fprintf(db.Log, "Rolling back: %s\n", latest.FileName)

start := time.Now()

parsed, err := latest.Parse()
if err != nil {
return err
Expand All @@ -567,6 +574,9 @@ func (db *DB) Rollback() error {
err = execMigration(sqlDB)
}

elapsed := time.Since(start)
fmt.Fprintf(db.Log, "Rolled back: %s in %s\n", latest.FileName, elapsed)

if err != nil {
return err
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/dbmate/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"testing/fstest"
Expand Down Expand Up @@ -315,17 +316,22 @@ func TestWaitBeforeVerbose(t *testing.T) {
output := capturer.CaptureOutput(func() {
testWaitBefore(t, true)
})
require.Contains(t, output,
re := regexp.MustCompile(`((Applied|Rolled back): .* in) ([\w.,µ]+)`)
maskedOutput := re.ReplaceAllString(output, "$1 ELAPSED")
require.Contains(t, maskedOutput,
`Applying: 20151129054053_test_migration.sql
Last insert ID: 1
Rows affected: 1
Applied: 20151129054053_test_migration.sql in ELAPSED
Applying: 20200227231541_test_posts.sql
Last insert ID: 1
Rows affected: 1`)
require.Contains(t, output,
Rows affected: 1
Applied: 20200227231541_test_posts.sql in ELAPSED`)
require.Contains(t, maskedOutput,
`Rolling back: 20200227231541_test_posts.sql
Last insert ID: 0
Rows affected: 0`)
Rows affected: 0
Rolled back: 20200227231541_test_posts.sql in ELAPSED`)
}

func testEachURL(t *testing.T, fn func(*testing.T, *url.URL)) {
Expand Down

0 comments on commit 9107c09

Please sign in to comment.