Skip to content

Commit 803b4ea

Browse files
committed
Merge pull request #17 from nadams/schema-support
Adds schema support
2 parents 493e520 + 63b1fec commit 803b4ea

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

migrate.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
)
2727

2828
var tableName = "gorp_migrations"
29+
var schemaName = ""
2930
var 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+
4057
type 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()

sql-migrate/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Environment struct {
3535
DataSource string `yaml:"datasource"`
3636
Dir string `yaml:"dir"`
3737
TableName string `yaml:"table"`
38+
SchemaName string `yaml:"schema"`
3839
}
3940

4041
func ReadConfig() (map[string]*Environment, error) {
@@ -79,6 +80,10 @@ func GetEnvironment() (*Environment, error) {
7980
migrate.SetTable(env.TableName)
8081
}
8182

83+
if env.SchemaName != "" {
84+
migrate.SetSchema(env.SchemaName)
85+
}
86+
8287
return env, nil
8388
}
8489

0 commit comments

Comments
 (0)