From 7d1a92d60e7df38fdc2f3e42ff1cc7842aefdf18 Mon Sep 17 00:00:00 2001 From: Cr <631807682@qq.com> Date: Sun, 22 May 2022 16:12:28 +0800 Subject: [PATCH] test: test for skip prepared when auto migrate (#5350) --- tests/migrate_test.go | 36 ++++++++++++++++++++++++++++++++++++ tests/tests_test.go | 11 ++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 12eb8ed0..2b5d7ecd 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/schema" . "gorm.io/gorm/utils/tests" @@ -712,3 +713,38 @@ func TestPrimarykeyID(t *testing.T) { t.Fatalf("AutoMigrate err:%v", err) } } + +func TestInvalidCachedPlan(t *testing.T) { + if DB.Dialector.Name() != "postgres" { + return + } + + db, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{}) + if err != nil { + t.Errorf("Open err:%v", err) + } + + type Object1 struct{} + type Object2 struct { + Field1 string + } + type Object3 struct { + Field2 string + } + db.Migrator().DropTable("objects") + + err = db.Table("objects").AutoMigrate(&Object1{}) + if err != nil { + t.Errorf("AutoMigrate err:%v", err) + } + + err = db.Table("objects").AutoMigrate(&Object2{}) + if err != nil { + t.Errorf("AutoMigrate err:%v", err) + } + + err = db.Table("objects").AutoMigrate(&Object3{}) + if err != nil { + t.Errorf("AutoMigrate err:%v", err) + } +} diff --git a/tests/tests_test.go b/tests/tests_test.go index 08f4f193..dcba3cbf 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -17,6 +17,11 @@ import ( ) var DB *gorm.DB +var ( + mysqlDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local" + postgresDSN = "user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai" + sqlserverDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" +) func init() { var err error @@ -49,13 +54,13 @@ func OpenTestConnection() (db *gorm.DB, err error) { case "mysql": log.Println("testing mysql...") if dbDSN == "" { - dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local" + dbDSN = mysqlDSN } db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{}) case "postgres": log.Println("testing postgres...") if dbDSN == "" { - dbDSN = "user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai" + dbDSN = postgresDSN } db, err = gorm.Open(postgres.New(postgres.Config{ DSN: dbDSN, @@ -72,7 +77,7 @@ func OpenTestConnection() (db *gorm.DB, err error) { // GO log.Println("testing sqlserver...") if dbDSN == "" { - dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" + dbDSN = sqlserverDSN } db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{}) default: