Skip to content

Commit eb18766

Browse files
authored
Merge branch 'master' into gomod
2 parents 8960597 + 0d68dd7 commit eb18766

3 files changed

Lines changed: 99 additions & 15 deletions

File tree

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ language: go
33
sudo: false
44

55
go:
6-
- 1.11.x
7-
- 1.12.x
8-
- 1.13.x
9-
6+
- "1.11"
7+
- "1.12"
8+
- "1.13"
9+
1010
services:
1111
- mysql
1212
- postgresql

migrate.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3046
var 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, ...).
7490
func 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.
8197
func 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.
374395
func 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.
445471
func 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

623653
func 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()

migrate_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,53 @@ func (s *SqliteMigrateSuite) TestExecWithUnknownMigrationInDatabase(c *C) {
557557
_, err = s.DbMap.Exec("SELECT age FROM people")
558558
c.Assert(err, NotNil)
559559
}
560+
561+
func (s *SqliteMigrateSuite) TestRunMigrationObjDefaultTable(c *C) {
562+
migrations := &MemoryMigrationSource{
563+
Migrations: sqliteMigrations[:1],
564+
}
565+
566+
ms := MigrationSet{}
567+
// Executes one migration
568+
n, err := ms.Exec(s.Db, "sqlite3", migrations, Up)
569+
c.Assert(err, IsNil)
570+
c.Assert(n, Equals, 1)
571+
572+
// Can use table now
573+
_, err = s.DbMap.Exec("SELECT * FROM people")
574+
c.Assert(err, IsNil)
575+
576+
// Uses default tableName
577+
_, err = s.DbMap.Exec("SELECT * FROM gorp_migrations")
578+
c.Assert(err, IsNil)
579+
580+
// Shouldn't apply migration again
581+
n, err = ms.Exec(s.Db, "sqlite3", migrations, Up)
582+
c.Assert(err, IsNil)
583+
c.Assert(n, Equals, 0)
584+
}
585+
586+
func (s *SqliteMigrateSuite) TestRunMigrationObjOtherTable(c *C) {
587+
migrations := &MemoryMigrationSource{
588+
Migrations: sqliteMigrations[:1],
589+
}
590+
591+
ms := MigrationSet{TableName: "other_migrations"}
592+
// Executes one migration
593+
n, err := ms.Exec(s.Db, "sqlite3", migrations, Up)
594+
c.Assert(err, IsNil)
595+
c.Assert(n, Equals, 1)
596+
597+
// Can use table now
598+
_, err = s.DbMap.Exec("SELECT * FROM people")
599+
c.Assert(err, IsNil)
600+
601+
// Uses default tableName
602+
_, err = s.DbMap.Exec("SELECT * FROM other_migrations")
603+
c.Assert(err, IsNil)
604+
605+
// Shouldn't apply migration again
606+
n, err = ms.Exec(s.Db, "sqlite3", migrations, Up)
607+
c.Assert(err, IsNil)
608+
c.Assert(n, Equals, 0)
609+
}

0 commit comments

Comments
 (0)