Repository: go-gorm/gorm
Pull request #5350 (2022-05-19 08:12:55)

test: test for skip prepared when auto migrate
<!--
Make sure these boxes checked before submitting your pull request.

For significant changes, please open an issue to make an agreement on an implementation design/plan first before starting it.
-->

- [x] Do only one thing
- [x] Non breaking API changes
- [x] Tested

### What did this pull request do?

<!--
provide a general description of the code changes in your pull request
-->
test for https://github.com/go-gorm/postgres/pull/109

### User Case Description

<!-- Your use case -->



Patch:
diff --git a/clause/expression.go b/clause/expression.go
index dde00b1..92ac7f2 100644
--- a/clause/expression.go
+++ b/clause/expression.go
@@ -127,7 +127,7 @@ func (expr NamedExpr) Build(builder Builder) {
 		if v == '@' && !inName {
 			inName = true
 			name = []byte{}
-		} else if v == ' ' || v == ',' || v == ')' || v == '"' || v == '\'' || v == '`' || v == '\n' || v == ';' {
+		} else if v == ' ' || v == ',' || v == ')' || v == '"' || v == '\'' || v == '`' || v == '\r' || v == '\n' || v == ';' {
 			if inName {
 				if nv, ok := namedMap[string(name)]; ok {
 					builder.AddVar(builder, nv)


Test patch:
diff --git a/clause/expression_test.go b/clause/expression_test.go
index 4826db3..aaede61 100644
--- a/clause/expression_test.go
+++ b/clause/expression_test.go
@@ -94,6 +94,16 @@ func TestNamedExpr(t *testing.T) {
 		Vars:         []interface{}{sql.Named("name", "jinzhu")},
 		Result:       "name1 = ? AND name2 = ?;",
 		ExpectedVars: []interface{}{"jinzhu", "jinzhu"},
+	}, {
+		SQL:          "name1 = @name1\r\n AND name2 = @name2",
+		Vars:         []interface{}{map[string]interface{}{"name1": "jinzhu", "name2": "jinzhu"}},
+		Result:       "name1 = ?\r\n AND name2 = ?",
+		ExpectedVars: []interface{}{"jinzhu", "jinzhu"},
+	}, {
+		SQL:          "name1 = @name1\r AND name2 = @name2",
+		Vars:         []interface{}{map[string]interface{}{"name1": "jinzhu", "name2": "jinzhu"}},
+		Result:       "name1 = ?\r AND name2 = ?",
+		ExpectedVars: []interface{}{"jinzhu", "jinzhu"},
 	}, {
 		SQL:    "?",
 		Vars:   []interface{}{clause.Column{Table: "table", Name: "col"}},
diff --git a/tests/migrate_test.go b/tests/migrate_test.go
index 12eb8ed..2b5d7ec 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 08f4f19..dcba3cb 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: