Skip to content

Commit

Permalink
Fix DisableCreateTable for MigrationSet instances.
Browse files Browse the repository at this point in the history
Previously the DisableCreateTable setting was always being read from the
global `migSet` instance. This meant that setting
`ms.DisableCreateTable` on a given `var ms MigrationSet` instance had no
effect.

This commit fixes that bug, and adds a test to confirm the fix.
Regardless of the global `migSet.DisableCreateTable` value,
`ms.getMigrationDbMap()` will always read from `ms.DisableCreateTable`.
  • Loading branch information
peterldowns committed May 30, 2023
1 parent 4e602c7 commit 074b27e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
table.ColMap("Id").SetMaxSize(4000)
}

if migSet.DisableCreateTable {
if ms.DisableCreateTable {
return dbMap, nil
}

Expand Down
48 changes: 48 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,51 @@ func (s *SqliteMigrateSuite) TestGetMigrationDbMapWithDisableCreateTable(c *C) {
_, err := migSet.getMigrationDbMap(s.Db, "postgres")
c.Assert(err, IsNil)
}

// If ms.DisableCreateTable == true, then the the migrations table should not be
// created, regardless of the global migSet.DisableCreateTable setting.
func (s *SqliteMigrateSuite) TestGetMigrationObjDbMapWithDisableCreateTableTrue(c *C) {
SetDisableCreateTable(false)
ms := MigrationSet{
DisableCreateTable: true,
TableName: "silly_example_table",
}
c.Assert(migSet.DisableCreateTable, Equals, false)
c.Assert(ms.DisableCreateTable, Equals, true)

dbMap, err := ms.getMigrationDbMap(s.Db, "sqlite3")
c.Assert(err, IsNil)
c.Assert(dbMap, NotNil)

tableNameIfExists, err := s.DbMap.SelectNullStr(
"SELECT name FROM sqlite_master WHERE type='table' AND name=$1",
ms.TableName,
)
c.Assert(err, IsNil)
c.Assert(tableNameIfExists.Valid, Equals, false)
}

// If ms.DisableCreateTable == false, then the the migrations table should not be
// created, regardless of the global migSet.DisableCreateTable setting.
func (s *SqliteMigrateSuite) TestGetMigrationObjDbMapWithDisableCreateTableFalse(c *C) {
SetDisableCreateTable(true)
defer SetDisableCreateTable(false) // reset the global state when the test ends.
ms := MigrationSet{
DisableCreateTable: false,
TableName: "silly_example_table",
}
c.Assert(migSet.DisableCreateTable, Equals, true)
c.Assert(ms.DisableCreateTable, Equals, false)

dbMap, err := ms.getMigrationDbMap(s.Db, "sqlite3")
c.Assert(err, IsNil)
c.Assert(dbMap, NotNil)

tableNameIfExists, err := s.DbMap.SelectNullStr(
"SELECT name FROM sqlite_master WHERE type='table' AND name=$1",
ms.TableName,
)
c.Assert(err, IsNil)
c.Assert(tableNameIfExists.Valid, Equals, true)
c.Assert(tableNameIfExists.String, Equals, ms.TableName)
}

0 comments on commit 074b27e

Please sign in to comment.