test: test for skip prepared when auto migrate (#5350)

This commit is contained in:
Cr 2022-05-22 16:12:28 +08:00 committed by GitHub
parent 540fb49bcb
commit 7d1a92d60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"testing" "testing"
"time" "time"
"gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
. "gorm.io/gorm/utils/tests" . "gorm.io/gorm/utils/tests"
@ -712,3 +713,38 @@ func TestPrimarykeyID(t *testing.T) {
t.Fatalf("AutoMigrate err:%v", err) 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)
}
}

View File

@ -17,6 +17,11 @@ import (
) )
var DB *gorm.DB 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() { func init() {
var err error var err error
@ -49,13 +54,13 @@ func OpenTestConnection() (db *gorm.DB, err error) {
case "mysql": case "mysql":
log.Println("testing mysql...") log.Println("testing mysql...")
if dbDSN == "" { 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{}) db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
case "postgres": case "postgres":
log.Println("testing postgres...") log.Println("testing postgres...")
if dbDSN == "" { 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{ db, err = gorm.Open(postgres.New(postgres.Config{
DSN: dbDSN, DSN: dbDSN,
@ -72,7 +77,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {
// GO // GO
log.Println("testing sqlserver...") log.Println("testing sqlserver...")
if dbDSN == "" { if dbDSN == "" {
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" dbDSN = sqlserverDSN
} }
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{}) db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{})
default: default: