From 074b27e229855b21c6e676fe25c8efb751d0f7d3 Mon Sep 17 00:00:00 2001 From: Peter Downs Date: Mon, 29 May 2023 13:22:02 -0400 Subject: [PATCH] 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`. --- migrate.go | 2 +- migrate_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/migrate.go b/migrate.go index 5ff70325..34757d33 100644 --- a/migrate.go +++ b/migrate.go @@ -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 } diff --git a/migrate_test.go b/migrate_test.go index 89073a35..fcecc45f 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -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) +}