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

modify: Add statement timeout query parmeter for MySQL #778

Merged
merged 4 commits into from
Jul 27, 2022
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
1 change: 1 addition & 0 deletions database/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|------------|---------------------|-------------|
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
| `x-no-lock` | `NoLock` | Set to `true` to skip `GET_LOCK`/`RELEASE_LOCK` statements. Useful for [multi-master MySQL flavors](https://www.percona.com/doc/percona-xtradb-cluster/LATEST/features/pxc-strict-mode.html#explicit-table-locking). Only run migrations from one host when this is enabled. |
| `x-statement-timeout` | `StatementTimeout` | Abort any statement that takes more than the specified number of milliseconds, functionally similar to [Server-side SELECT statement timeouts](https://dev.mysql.com/blog-archive/server-side-select-statement-timeouts/) but enforced by the client. Available for all versions of MySQL, not just >=5.7. |
Dombo marked this conversation as resolved.
Show resolved Hide resolved
| `dbname` | `DatabaseName` | The name of the database to connect to |
| `user` | | The user to sign in as |
| `password` | | The user's password |
Expand Down
33 changes: 26 additions & 7 deletions database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
nurl "net/url"
"strconv"
"strings"
"time"

"github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate/v4/database"
Expand All @@ -38,9 +39,10 @@ var (
)

type Config struct {
MigrationsTable string
DatabaseName string
NoLock bool
MigrationsTable string
DatabaseName string
NoLock bool
StatementTimeout time.Duration
}

type Mysql struct {
Expand Down Expand Up @@ -241,15 +243,25 @@ func (m *Mysql) Open(url string) (database.Driver, error) {
}
}

statementTimeoutParam := customParams["x-statement-timeout"]
statementTimeout := 0
if statementTimeoutParam != "" {
statementTimeout, err := strconv.ParseFloat(statementTimeoutParam, 64)
if err != nil {
return nil, fmt.Errorf("could not parse x-statement-timeout as float: %w", err)
}
}

db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
return nil, err
}

mx, err := WithInstance(db, &Config{
DatabaseName: config.DBName,
MigrationsTable: customParams["x-migrations-table"],
NoLock: noLock,
DatabaseName: config.DBName,
MigrationsTable: customParams["x-migrations-table"],
NoLock: noLock,
StatementTimeout: time.Duration(statementTimeout) * time.Millisecond,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -327,8 +339,15 @@ func (m *Mysql) Run(migration io.Reader) error {
return err
}

ctx := context.Background()
if m.config.StatementTimeout != 0 {
dhui marked this conversation as resolved.
Show resolved Hide resolved
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, m.config.StatementTimeout)
defer cancel()
}

query := string(migr[:])
if _, err := m.conn.ExecContext(context.Background(), query); err != nil {
if _, err := m.conn.ExecContext(ctx, query); err != nil {
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
}

Expand Down