@@ -26,6 +26,7 @@ const (
26
26
)
27
27
28
28
var tableName = "gorp_migrations"
29
+ var schemaName = ""
29
30
var numberPrefixRegex = regexp .MustCompile (`^(\d+).*$` )
30
31
31
32
// Set the name of the table used to store migration info.
@@ -37,6 +38,22 @@ func SetTable(name string) {
37
38
}
38
39
}
39
40
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
+
40
57
type Migration struct {
41
58
Id string
42
59
Up []string
@@ -303,7 +320,7 @@ func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationD
303
320
}
304
321
305
322
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 () ))
307
324
if err != nil {
308
325
return nil , nil , err
309
326
}
@@ -409,7 +426,7 @@ func GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error)
409
426
}
410
427
411
428
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 () )
413
430
_ , err = dbMap .Select (& records , query )
414
431
if err != nil {
415
432
return nil , err
@@ -444,7 +461,7 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
444
461
445
462
// Create migration database map
446
463
dbMap := & gorp.DbMap {Db : db , Dialect : d }
447
- dbMap .AddTableWithName (MigrationRecord {}, tableName ).SetKeys (false , "Id" )
464
+ dbMap .AddTableWithNameAndSchema (MigrationRecord {}, schemaName , tableName ).SetKeys (false , "Id" )
448
465
//dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds))
449
466
450
467
err := dbMap .CreateTablesIfNotExists ()
0 commit comments