@@ 1,4 1,4 @@
-# migrate
+# migrate [![godoc](https://godocs.io/petersanchez.com/migrate?status.svg)](https://godocs.io/petersanchez.com/migrate)
Database migrations for your Go project. Supports files, functions, and query
migrations.
@@ 181,7 181,7 @@ func (me *MigrationEngine) rebind(query
func (me *MigrationEngine) getRecord(ctx context.Context, id string) (string, error) {
var found string
- err := WithTx(ctx, txOptionsRO, func(tx *sql.Tx) error {
+ err := withTx(ctx, txOptionsRO, func(tx *sql.Tx) error {
query := me.rebind("SELECT id FROM migrations WHERE id=?")
return tx.QueryRowContext(ctx, query, id).Scan(&found)
})
@@ 197,7 197,7 @@ func (me *MigrationEngine) printf(format
}
func (me *MigrationEngine) createMigrationTable(ctx context.Context) error {
- err := WithTx(ctx, nil, func(tx *sql.Tx) error {
+ err := withTx(ctx, nil, func(tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS migrations (id TEXT PRIMARY KEY)")
if err != nil {
return fmt.Errorf("creating migrations table: %w", err)
@@ 216,7 216,7 @@ func (me *MigrationEngine) runMigration(
defer cancel()
}
- err := WithTx(ctx, nil, func(tx *sql.Tx) error {
+ err := withTx(ctx, nil, func(tx *sql.Tx) error {
query := me.rebind("INSERT INTO migrations (id) VALUES (?)")
_, err := tx.ExecContext(ctx, query, m.ID)
if err != nil {
@@ 244,7 244,7 @@ func (me *MigrationEngine) runRollback(c
defer cancel()
}
- err := WithTx(ctx, nil, func(tx *sql.Tx) error {
+ err := withTx(ctx, nil, func(tx *sql.Tx) error {
query := me.rebind("DELETE FROM migrations WHERE id=?")
_, err := tx.ExecContext(ctx, query, m.ID)
if err != nil {
@@ 80,8 80,7 @@ func dbFromContext(ctx context.Context)
return db
}
-// WithTx calls a function wrapped in a database transaction
-func WithTx(ctx context.Context, opts *sql.TxOptions, fn func(tx *sql.Tx) error) error {
+func withTx(ctx context.Context, opts *sql.TxOptions, fn func(tx *sql.Tx) error) error {
db := dbFromContext(ctx)
tx, err := db.BeginTx(ctx, opts)
if err != nil {