Skip to content

Commit

Permalink
Merge pull request #199 from akhorsi/master
Browse files Browse the repository at this point in the history
Add disable option to avoid creating migration table on getting db map
  • Loading branch information
rubenv authored Oct 23, 2021
2 parents 55d5740 + aad1dd2 commit 9f02b1e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type MigrationSet struct {
//
// This should be used sparingly as it is removing a safety check.
IgnoreUnknown bool
// DisableCreateTable disable the creation of the migration table
DisableCreateTable bool
}

var migSet = MigrationSet{}
Expand Down Expand Up @@ -106,6 +108,11 @@ func SetSchema(name string) {
}
}

// SetDisableCreateTable sets the boolean to disable the creation of the migration table
func SetDisableCreateTable(disable bool) {
migSet.DisableCreateTable = disable
}

// SetIgnoreUnknown sets the flag that skips database check to see if there is a
// migration in the database that is not in migration source.
//
Expand Down Expand Up @@ -752,6 +759,10 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
table.ColMap("Id").SetMaxSize(4000)
}

if migSet.DisableCreateTable {
return dbMap, nil
}

err := dbMap.CreateTablesIfNotExists()
if err != nil {
// Oracle database does not support `if not exists`, so use `ORA-00955:` error code
Expand Down
17 changes: 17 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,20 @@ func (s *SqliteMigrateSuite) TestRunMigrationObjOtherTable(c *C) {
c.Assert(err, IsNil)
c.Assert(n, Equals, 0)
}

func (s *SqliteMigrateSuite) TestSetDisableCreateTable(c *C) {
c.Assert(migSet.DisableCreateTable, Equals, false)

SetDisableCreateTable(true)
c.Assert(migSet.DisableCreateTable, Equals, true)

SetDisableCreateTable(false)
c.Assert(migSet.DisableCreateTable, Equals, false)
}

func (s *SqliteMigrateSuite) TestGetMigrationDbMapWithDisableCreateTable(c *C) {
SetDisableCreateTable(false)

_, err := migSet.getMigrationDbMap(s.Db, "postgres")
c.Assert(err, IsNil)
}

0 comments on commit 9f02b1e

Please sign in to comment.