From a19ed9bc1bbd7b96d5212b9728f02432040a5730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=88=90=E7=BA=A2?= Date: Tue, 25 May 2021 14:22:57 +0800 Subject: [PATCH] fix: fixed a bug in function migrationRan --- gormigrate.go | 5 ++++- gormigrate_test.go | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gormigrate.go b/gormigrate.go index cba54eb..a5ef4f9 100644 --- a/gormigrate.go +++ b/gormigrate.go @@ -383,7 +383,10 @@ func (g *Gormigrate) createMigrationTableIfNotExists() error { func (g *Gormigrate) migrationRan(m *Migration) (bool, error) { var count int64 - err := g.tx. + // if g.tx.clone == 0, g.tx.Statement.Table is always equal to "migrations",it will get a wrong result. + tx := g.tx.Session(&gorm.Session{}) + + err := tx. Table(g.options.TableName). Where(fmt.Sprintf("%s = ?", g.options.IDColumnName), m.ID). Count(&count). diff --git a/gormigrate_test.go b/gormigrate_test.go index 8189e2d..d9a2e0d 100644 --- a/gormigrate_test.go +++ b/gormigrate_test.go @@ -420,6 +420,9 @@ func forEachDatabase(t *testing.T, fn func(database *gorm.DB), dialects ...strin // ensure tables do not exists assert.NoError(t, db.Migrator().DropTable("migrations", "people", "pets")) + // set charset, get the result of db.clone is 0 + db = db.Set("gorm:table_options", "CHARSET=utf8mb4") + fn(db) }() }