Skip to content

Commit

Permalink
Use directly the type MigrationSet and create getTableName()
Browse files Browse the repository at this point in the history
  • Loading branch information
marema31 committed Nov 20, 2019
1 parent 9d446b4 commit df6f689
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,20 @@ const (

// MigrationSet provides database parameters for a migration execution
type MigrationSet struct {
// Name of the table used to store migration info.
tableName string
// Name of a schema that the migration table be referenced.
schemaName string
// TableName name of the table used to store migration info.
TableName string
// SchemaName schema that the migration table be referenced.
SchemaName string
}

var migSet = MigrationSet{}

// NewMigrationSet returns a parametrized Migration object
func NewMigrationSet(schemaName string, tableName string) MigrationSet {
if tableName == "" {
tableName = "gorp_migrations"
}
return MigrationSet{
tableName: tableName,
schemaName: schemaName,
func (ms MigrationSet) getTableName() string {
if ms.TableName == "" {
return "gorp_migrations"
}
}

var migSet = MigrationSet{
tableName: "gorp_migrations",
schemaName: "",
return ms.TableName
}

var numberPrefixRegex = regexp.MustCompile(`^(\d+).*$`)
Expand Down Expand Up @@ -95,14 +89,14 @@ func (e *TxError) Error() string {
// Should be called before any other call such as (Exec, ExecMax, ...).
func SetTable(name string) {
if name != "" {
migSet.tableName = name
migSet.TableName = name
}
}

// SetSchema sets the name of a schema that the migration table be referenced.
func SetSchema(name string) {
if name != "" {
migSet.schemaName = name
migSet.SchemaName = name
}
}

Expand Down Expand Up @@ -490,7 +484,7 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
}

var migrationRecords []MigrationRecord
_, err = dbMap.Select(&migrationRecords, fmt.Sprintf("SELECT * FROM %s", dbMap.Dialect.QuotedTableForQuery(ms.schemaName, ms.tableName)))
_, err = dbMap.Select(&migrationRecords, fmt.Sprintf("SELECT * FROM %s", dbMap.Dialect.QuotedTableForQuery(ms.SchemaName, ms.getTableName())))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -667,7 +661,7 @@ func (ms MigrationSet) GetMigrationRecords(db *sql.DB, dialect string) ([]*Migra
}

var records []*MigrationRecord
query := fmt.Sprintf("SELECT * FROM %s ORDER BY id ASC", dbMap.Dialect.QuotedTableForQuery(ms.schemaName, ms.tableName))
query := fmt.Sprintf("SELECT * FROM %s ORDER BY id ASC", dbMap.Dialect.QuotedTableForQuery(ms.SchemaName, ms.getTableName()))
_, err = dbMap.Select(&records, query)
if err != nil {
return nil, err
Expand Down Expand Up @@ -704,7 +698,7 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)

// Create migration database map
dbMap := &gorp.DbMap{Db: db, Dialect: d}
dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.schemaName, ms.tableName).SetKeys(false, "Id")
dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.SchemaName, ms.getTableName()).SetKeys(false, "Id")
//dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds))

err := dbMap.CreateTablesIfNotExists()
Expand Down

0 comments on commit df6f689

Please sign in to comment.