@@ -2,6 +2,7 @@ package migrate
22
33import (
44 "bytes"
5+ "context"
56 "database/sql"
67 "errors"
78 "fmt"
@@ -429,12 +430,24 @@ type SqlExecutor interface {
429430//
430431// Returns the number of applied migrations.
431432func Exec (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection ) (int , error ) {
432- return ExecMax ( db , dialect , m , dir , 0 )
433+ return ExecMaxContext ( context . Background (), db , dialect , m , dir , 0 )
433434}
434435
435436// Returns the number of applied migrations.
436437func (ms MigrationSet ) Exec (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection ) (int , error ) {
437- return ms .ExecMax (db , dialect , m , dir , 0 )
438+ return ms .ExecMaxContext (context .Background (), db , dialect , m , dir , 0 )
439+ }
440+
441+ // Execute a set of migrations with an input context.
442+ //
443+ // Returns the number of applied migrations.
444+ func ExecContext (ctx context.Context , db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection ) (int , error ) {
445+ return ExecMaxContext (ctx , db , dialect , m , dir , 0 )
446+ }
447+
448+ // Returns the number of applied migrations.
449+ func (ms MigrationSet ) ExecContext (ctx context.Context , db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection ) (int , error ) {
450+ return ms .ExecMaxContext (ctx , db , dialect , m , dir , 0 )
438451}
439452
440453// Execute a set of migrations
@@ -446,50 +459,78 @@ func ExecMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirecti
446459 return migSet .ExecMax (db , dialect , m , dir , max )
447460}
448461
462+ // Execute a set of migrations with an input context.
463+ //
464+ // Will apply at most `max` migrations. Pass 0 for no limit (or use Exec).
465+ //
466+ // Returns the number of applied migrations.
467+ func ExecMaxContext (ctx context.Context , db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) (int , error ) {
468+ return migSet .ExecMaxContext (ctx , db , dialect , m , dir , max )
469+ }
470+
449471// Execute a set of migrations
450472//
451473// Will apply at the target `version` of migration. Cannot be a negative value.
452474//
453475// Returns the number of applied migrations.
454476func ExecVersion (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , version int64 ) (int , error ) {
477+ return ExecVersionContext (context .Background (), db , dialect , m , dir , version )
478+ }
479+
480+ // Execute a set of migrations with an input context.
481+ //
482+ // Will apply at the target `version` of migration. Cannot be a negative value.
483+ //
484+ // Returns the number of applied migrations.
485+ func ExecVersionContext (ctx context.Context , db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , version int64 ) (int , error ) {
455486 if version < 0 {
456487 return 0 , fmt .Errorf ("target version %d should not be negative" , version )
457488 }
458- return migSet .ExecVersion ( db , dialect , m , dir , version )
489+ return migSet .ExecVersionContext ( ctx , db , dialect , m , dir , version )
459490}
460491
461492// Returns the number of applied migrations.
462493func (ms MigrationSet ) ExecMax (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) (int , error ) {
494+ return ms .ExecMaxContext (context .Background (), db , dialect , m , dir , max )
495+ }
496+
497+ // Returns the number of applied migrations, but applies with an input context.
498+ func (ms MigrationSet ) ExecMaxContext (ctx context.Context , db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) (int , error ) {
463499 migrations , dbMap , err := ms .PlanMigration (db , dialect , m , dir , max )
464500 if err != nil {
465501 return 0 , err
466502 }
467- return ms .applyMigrations (dir , migrations , dbMap )
503+ return ms .applyMigrations (ctx , dir , migrations , dbMap )
468504}
469505
470506// Returns the number of applied migrations.
471507func (ms MigrationSet ) ExecVersion (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , version int64 ) (int , error ) {
508+ return ms .ExecVersionContext (context .Background (), db , dialect , m , dir , version )
509+ }
510+
511+ func (ms MigrationSet ) ExecVersionContext (ctx context.Context , db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , version int64 ) (int , error ) {
472512 migrations , dbMap , err := ms .PlanMigrationToVersion (db , dialect , m , dir , version )
473513 if err != nil {
474514 return 0 , err
475515 }
476- return ms .applyMigrations (dir , migrations , dbMap )
516+ return ms .applyMigrations (ctx , dir , migrations , dbMap )
477517}
478518
479519// Applies the planned migrations and returns the number of applied migrations.
480- func (MigrationSet ) applyMigrations (dir MigrationDirection , migrations []* PlannedMigration , dbMap * gorp.DbMap ) (int , error ) {
520+ func (MigrationSet ) applyMigrations (ctx context. Context , dir MigrationDirection , migrations []* PlannedMigration , dbMap * gorp.DbMap ) (int , error ) {
481521 applied := 0
482522 for _ , migration := range migrations {
483523 var executor SqlExecutor
484524 var err error
485525
486526 if migration .DisableTransaction {
487- executor = dbMap
527+ executor = dbMap . WithContext ( ctx )
488528 } else {
489- executor , err = dbMap .Begin ()
529+ e , err : = dbMap .Begin ()
490530 if err != nil {
491531 return applied , newTxError (migration , err )
492532 }
533+ executor = e .WithContext (ctx )
493534 }
494535
495536 for _ , stmt := range migration .Queries {
0 commit comments