0c88a7b475
This code needs to work against cockroachDB, so transactions must be retried when a retryable error is returned. This change puts migrate transactions into the dbutil.WithTx transactional helpers to achieve this in the easiest way. Change-Id: Ib930e82d55cb0257357a222ce9131e6e53372c03
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package migrate
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
|
|
"storj.io/storj/private/dbutil/txutil"
|
|
)
|
|
|
|
// DB is the minimal implementation that is needed by migrations.
|
|
//
|
|
// DB can optionally have `Rebind(string) string` for translating `? queries for the specific database.
|
|
type DB interface {
|
|
BeginTx(ctx context.Context, txOptions *sql.TxOptions) (*sql.Tx, error)
|
|
Driver() driver.Driver
|
|
}
|
|
|
|
// DBX contains additional methods for migrations.
|
|
type DBX interface {
|
|
DB
|
|
Schema() string
|
|
Rebind(string) string
|
|
}
|
|
|
|
// rebind uses Rebind method when the database has the func.
|
|
func rebind(db DB, s string) string {
|
|
if dbx, ok := db.(interface{ Rebind(string) string }); ok {
|
|
return dbx.Rebind(s)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// WithTx runs the given callback in the context of a transaction.
|
|
func WithTx(ctx context.Context, db DB, fn func(ctx context.Context, tx *sql.Tx) error) error {
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return txutil.ExecuteInTx(ctx, db.Driver(), tx, func() error {
|
|
return fn(ctx, tx)
|
|
})
|
|
}
|