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

prints the migrate/rollback execution time #550

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 123us
```

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 123us
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 123us
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
19 changes: 8 additions & 11 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,13 @@ func TestWaitBeforeVerbose(t *testing.T) {
output := capturer.CaptureOutput(func() {
testWaitBefore(t, true)
})
require.Contains(t, output,
`Applying: 20151129054053_test_migration.sql
Last insert ID: 1
Rows affected: 1
Applying: 20200227231541_test_posts.sql
Last insert ID: 1
Rows affected: 1`)
require.Contains(t, output,
`Rolling back: 20200227231541_test_posts.sql
Last insert ID: 0
Rows affected: 0`)
matched, err := regexp.MatchString(`Applying: 20151129054053_test_migration\.sql\nLast insert ID: 1\nRows affected: 1\nApplied: 20151129054053_test_migration\.sql in ([\w.,µ]+)\nApplying: 20200227231541_test_posts\.sql\nLast insert ID: 1\nRows affected: 1\nApplied: 20200227231541_test_posts\.sql in ([\w.,µ]+)`, output)
require.NoError(t, err)
require.True(t, matched)

matched, err = regexp.MatchString(`Rolling back: 20200227231541_test_posts\.sql\nLast insert ID: 0\nRows affected: 0\nRolled back: 20200227231541_test_posts\.sql in ([\w.,µ]+)`, output)
require.NoError(t, err)
require.True(t, matched)
dossy marked this conversation as resolved.
Show resolved Hide resolved
}

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