Skip to content

Commit 074b27e

Browse files
committed
Fix DisableCreateTable for MigrationSet instances.
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`.
1 parent 4e602c7 commit 074b27e

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
819819
table.ColMap("Id").SetMaxSize(4000)
820820
}
821821

822-
if migSet.DisableCreateTable {
822+
if ms.DisableCreateTable {
823823
return dbMap, nil
824824
}
825825

migrate_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,51 @@ func (s *SqliteMigrateSuite) TestGetMigrationDbMapWithDisableCreateTable(c *C) {
758758
_, err := migSet.getMigrationDbMap(s.Db, "postgres")
759759
c.Assert(err, IsNil)
760760
}
761+
762+
// If ms.DisableCreateTable == true, then the the migrations table should not be
763+
// created, regardless of the global migSet.DisableCreateTable setting.
764+
func (s *SqliteMigrateSuite) TestGetMigrationObjDbMapWithDisableCreateTableTrue(c *C) {
765+
SetDisableCreateTable(false)
766+
ms := MigrationSet{
767+
DisableCreateTable: true,
768+
TableName: "silly_example_table",
769+
}
770+
c.Assert(migSet.DisableCreateTable, Equals, false)
771+
c.Assert(ms.DisableCreateTable, Equals, true)
772+
773+
dbMap, err := ms.getMigrationDbMap(s.Db, "sqlite3")
774+
c.Assert(err, IsNil)
775+
c.Assert(dbMap, NotNil)
776+
777+
tableNameIfExists, err := s.DbMap.SelectNullStr(
778+
"SELECT name FROM sqlite_master WHERE type='table' AND name=$1",
779+
ms.TableName,
780+
)
781+
c.Assert(err, IsNil)
782+
c.Assert(tableNameIfExists.Valid, Equals, false)
783+
}
784+
785+
// If ms.DisableCreateTable == false, then the the migrations table should not be
786+
// created, regardless of the global migSet.DisableCreateTable setting.
787+
func (s *SqliteMigrateSuite) TestGetMigrationObjDbMapWithDisableCreateTableFalse(c *C) {
788+
SetDisableCreateTable(true)
789+
defer SetDisableCreateTable(false) // reset the global state when the test ends.
790+
ms := MigrationSet{
791+
DisableCreateTable: false,
792+
TableName: "silly_example_table",
793+
}
794+
c.Assert(migSet.DisableCreateTable, Equals, true)
795+
c.Assert(ms.DisableCreateTable, Equals, false)
796+
797+
dbMap, err := ms.getMigrationDbMap(s.Db, "sqlite3")
798+
c.Assert(err, IsNil)
799+
c.Assert(dbMap, NotNil)
800+
801+
tableNameIfExists, err := s.DbMap.SelectNullStr(
802+
"SELECT name FROM sqlite_master WHERE type='table' AND name=$1",
803+
ms.TableName,
804+
)
805+
c.Assert(err, IsNil)
806+
c.Assert(tableNameIfExists.Valid, Equals, true)
807+
c.Assert(tableNameIfExists.String, Equals, ms.TableName)
808+
}

0 commit comments

Comments
 (0)