Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ The following options are available with all commands. You must use command line
- `--strict` - fail if migrations would be applied out of order _(env: `DBMATE_STRICT`)_
- `--wait` - wait for the db to become available before executing the subsequent command _(env: `DBMATE_WAIT`)_
- `--wait-timeout 60s` - timeout for --wait flag _(env: `DBMATE_WAIT_TIMEOUT`)_
- `--wait-interval 1s` - time to wait between connection attempts for --wait flag _(env: `DBMATE_WAIT_INTERVAL`)_

## Usage

Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ func NewApp() *cli.App {
Usage: "timeout for --wait flag",
Value: defaultDB.WaitTimeout,
},
&cli.DurationFlag{
Name: "wait-interval",
EnvVars: []string{"DBMATE_WAIT_INTERVAL"},
Usage: "time to wait between connection attempts for --wait flag",
Value: defaultDB.WaitInterval,
},
}

app.Commands = []*cli.Command{
Expand Down Expand Up @@ -324,6 +330,10 @@ func configureDB(c *cli.Context) (*dbmate.DB, error) {
if waitTimeout != 0 {
db.WaitTimeout = waitTimeout
}
waitInterval := c.Duration("wait-interval")
if waitInterval != 0 {
db.WaitInterval = waitInterval
}

return db, nil
}
Expand Down
42 changes: 42 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -212,3 +213,44 @@ func TestConfigureDB_Driver(t *testing.T) {
require.Equal(t, "clickhouse", configuredDB.DriverName)
})
}

func TestConfigureDB_WaitInterval(t *testing.T) {
var configuredDB *dbmate.DB

app := NewApp()
app.Commands = []*cli.Command{
{
Name: "test-config",
Action: func(c *cli.Context) error {
var err error
configuredDB, err = configureDB(c)
return err
},
},
}

t.Run("default", func(t *testing.T) {
configuredDB = nil
err := app.Run([]string{"dbmate", "test-config"})
require.NoError(t, err)
require.Equal(t, time.Second, configuredDB.WaitInterval)
})

t.Run("from env variable", func(t *testing.T) {
configuredDB = nil
t.Setenv("DBMATE_WAIT_INTERVAL", "2s")

err := app.Run([]string{"dbmate", "test-config"})
require.NoError(t, err)
require.Equal(t, 2*time.Second, configuredDB.WaitInterval)
})

t.Run("flag overrides env variable", func(t *testing.T) {
configuredDB = nil
t.Setenv("DBMATE_WAIT_INTERVAL", "2s")

err := app.Run([]string{"dbmate", "--wait-interval", "3s", "test-config"})
require.NoError(t, err)
require.Equal(t, 3*time.Second, configuredDB.WaitInterval)
})
}