@@ -25,8 +25,24 @@ const (
2525 Down
2626)
2727
28- var tableName = "gorp_migrations"
29- var schemaName = ""
28+ // MigrationSet provides database parameters for a migration execution
29+ type MigrationSet struct {
30+ // TableName name of the table used to store migration info.
31+ TableName string
32+ // SchemaName schema that the migration table be referenced.
33+ SchemaName string
34+ }
35+
36+ var migSet = MigrationSet {}
37+
38+ // NewMigrationSet returns a parametrized Migration object
39+ func (ms MigrationSet ) getTableName () string {
40+ if ms .TableName == "" {
41+ return "gorp_migrations"
42+ }
43+ return ms .TableName
44+ }
45+
3046var numberPrefixRegex = regexp .MustCompile (`^(\d+).*$` )
3147
3248// PlanError happens where no migration plan could be created between the sets
@@ -73,14 +89,14 @@ func (e *TxError) Error() string {
7389// Should be called before any other call such as (Exec, ExecMax, ...).
7490func SetTable (name string ) {
7591 if name != "" {
76- tableName = name
92+ migSet . TableName = name
7793 }
7894}
7995
8096// SetSchema sets the name of a schema that the migration table be referenced.
8197func SetSchema (name string ) {
8298 if name != "" {
83- schemaName = name
99+ migSet . SchemaName = name
84100 }
85101}
86102
@@ -366,13 +382,23 @@ func Exec(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection)
366382 return ExecMax (db , dialect , m , dir , 0 )
367383}
368384
385+ // Returns the number of applied migrations.
386+ func (ms MigrationSet ) Exec (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection ) (int , error ) {
387+ return ms .ExecMax (db , dialect , m , dir , 0 )
388+ }
389+
369390// Execute a set of migrations
370391//
371392// Will apply at most `max` migrations. Pass 0 for no limit (or use Exec).
372393//
373394// Returns the number of applied migrations.
374395func ExecMax (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) (int , error ) {
375- migrations , dbMap , err := PlanMigration (db , dialect , m , dir , max )
396+ return migSet .ExecMax (db , dialect , m , dir , max )
397+ }
398+
399+ // Returns the number of applied migrations.
400+ func (ms MigrationSet ) ExecMax (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) (int , error ) {
401+ migrations , dbMap , err := ms .PlanMigration (db , dialect , m , dir , max )
376402 if err != nil {
377403 return 0 , err
378404 }
@@ -443,7 +469,11 @@ func ExecMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirecti
443469
444470// Plan a migration.
445471func PlanMigration (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) ([]* PlannedMigration , * gorp.DbMap , error ) {
446- dbMap , err := getMigrationDbMap (db , dialect )
472+ return migSet .PlanMigration (db , dialect , m , dir , max )
473+ }
474+
475+ func (ms MigrationSet ) PlanMigration (db * sql.DB , dialect string , m MigrationSource , dir MigrationDirection , max int ) ([]* PlannedMigration , * gorp.DbMap , error ) {
476+ dbMap , err := ms .getMigrationDbMap (db , dialect )
447477 if err != nil {
448478 return nil , nil , err
449479 }
@@ -454,7 +484,7 @@ func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationD
454484 }
455485
456486 var migrationRecords []MigrationRecord
457- _ , err = dbMap .Select (& migrationRecords , fmt .Sprintf ("SELECT * FROM %s" , dbMap .Dialect .QuotedTableForQuery (schemaName , tableName )))
487+ _ , err = dbMap .Select (& migrationRecords , fmt .Sprintf ("SELECT * FROM %s" , dbMap .Dialect .QuotedTableForQuery (ms . SchemaName , ms . getTableName () )))
458488 if err != nil {
459489 return nil , nil , err
460490 }
@@ -621,13 +651,17 @@ func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration)
621651}
622652
623653func GetMigrationRecords (db * sql.DB , dialect string ) ([]* MigrationRecord , error ) {
624- dbMap , err := getMigrationDbMap (db , dialect )
654+ return migSet .GetMigrationRecords (db , dialect )
655+ }
656+
657+ func (ms MigrationSet ) GetMigrationRecords (db * sql.DB , dialect string ) ([]* MigrationRecord , error ) {
658+ dbMap , err := ms .getMigrationDbMap (db , dialect )
625659 if err != nil {
626660 return nil , err
627661 }
628662
629663 var records []* MigrationRecord
630- query := fmt .Sprintf ("SELECT * FROM %s ORDER BY id ASC" , dbMap .Dialect .QuotedTableForQuery (schemaName , tableName ))
664+ query := fmt .Sprintf ("SELECT * FROM %s ORDER BY id ASC" , dbMap .Dialect .QuotedTableForQuery (ms . SchemaName , ms . getTableName () ))
631665 _ , err = dbMap .Select (& records , query )
632666 if err != nil {
633667 return nil , err
@@ -636,7 +670,7 @@ func GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error)
636670 return records , nil
637671}
638672
639- func getMigrationDbMap (db * sql.DB , dialect string ) (* gorp.DbMap , error ) {
673+ func ( ms MigrationSet ) getMigrationDbMap (db * sql.DB , dialect string ) (* gorp.DbMap , error ) {
640674 d , ok := MigrationDialects [dialect ]
641675 if ! ok {
642676 return nil , fmt .Errorf ("Unknown dialect: %s" , dialect )
@@ -664,7 +698,7 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
664698
665699 // Create migration database map
666700 dbMap := & gorp.DbMap {Db : db , Dialect : d }
667- dbMap .AddTableWithNameAndSchema (MigrationRecord {}, schemaName , tableName ).SetKeys (false , "Id" )
701+ dbMap .AddTableWithNameAndSchema (MigrationRecord {}, ms . SchemaName , ms . getTableName () ).SetKeys (false , "Id" )
668702 //dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds))
669703
670704 err := dbMap .CreateTablesIfNotExists ()
0 commit comments