@@ -26,6 +26,7 @@ const (
2626)
2727
2828var tableName = "gorp_migrations"
29+ var schemaName = ""
2930var numberPrefixRegex = regexp .MustCompile (`^(\d+).*$` )
3031
3132// Set the name of the table used to store migration info.
@@ -37,6 +38,22 @@ func SetTable(name string) {
3738 }
3839}
3940
41+ // SetSchema sets the name of a schema that the migration table be referenced.
42+ func SetSchema (name string ) {
43+ if name != "" {
44+ schemaName = name
45+ }
46+ }
47+
48+ func getTableName () string {
49+ t := tableName
50+ if schemaName != "" {
51+ t = fmt .Sprintf ("%s.%s" , schemaName , t )
52+ }
53+
54+ return t
55+ }
56+
4057type Migration struct {
4158 Id string
4259 Up []string
@@ -303,7 +320,7 @@ func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationD
303320 }
304321
305322 var migrationRecords []MigrationRecord
306- _ , err = dbMap .Select (& migrationRecords , fmt .Sprintf ("SELECT * FROM %s" , tableName ))
323+ _ , err = dbMap .Select (& migrationRecords , fmt .Sprintf ("SELECT * FROM %s" , getTableName () ))
307324 if err != nil {
308325 return nil , nil , err
309326 }
@@ -409,7 +426,7 @@ func GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error)
409426 }
410427
411428 var records []* MigrationRecord
412- query := fmt .Sprintf ("SELECT * FROM %s ORDER BY id ASC" , tableName )
429+ query := fmt .Sprintf ("SELECT * FROM %s ORDER BY id ASC" , getTableName () )
413430 _ , err = dbMap .Select (& records , query )
414431 if err != nil {
415432 return nil , err
@@ -444,7 +461,7 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
444461
445462 // Create migration database map
446463 dbMap := & gorp.DbMap {Db : db , Dialect : d }
447- dbMap .AddTableWithName (MigrationRecord {}, tableName ).SetKeys (false , "Id" )
464+ dbMap .AddTableWithNameAndSchema (MigrationRecord {}, schemaName , tableName ).SetKeys (false , "Id" )
448465 //dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds))
449466
450467 err := dbMap .CreateTablesIfNotExists ()
0 commit comments