Merge branch 'master' into test_for_skip_prepared
This commit is contained in:
commit
58319a8c76
@ -589,8 +589,7 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
err = fc(db.Session(&Session{}))
|
||||
err = fc(db.Session(&Session{NewDB: db.clone == 1}))
|
||||
} else {
|
||||
tx := db.Begin(opts...)
|
||||
if tx.Error != nil {
|
||||
|
@ -223,7 +223,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
|
||||
}
|
||||
|
||||
createTableSQL += ","
|
||||
values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
|
||||
values = append(values, clause.Column{Name: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -235,7 +235,8 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||
Name: joinFieldName,
|
||||
PkgPath: ownField.StructField.PkgPath,
|
||||
Type: ownField.StructField.Type,
|
||||
Tag: removeSettingFromTag(ownField.StructField.Tag, "column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
Tag: removeSettingFromTag(appendSettingFromTag(ownField.StructField.Tag, "primaryKey"),
|
||||
"column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
})
|
||||
}
|
||||
|
||||
@ -258,7 +259,8 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||
Name: joinFieldName,
|
||||
PkgPath: relField.StructField.PkgPath,
|
||||
Type: relField.StructField.Type,
|
||||
Tag: removeSettingFromTag(relField.StructField.Tag, "column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
Tag: removeSettingFromTag(appendSettingFromTag(relField.StructField.Tag, "primaryKey"),
|
||||
"column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -59,6 +60,14 @@ func removeSettingFromTag(tag reflect.StructTag, names ...string) reflect.Struct
|
||||
return tag
|
||||
}
|
||||
|
||||
func appendSettingFromTag(tag reflect.StructTag, value string) reflect.StructTag {
|
||||
t := tag.Get("gorm")
|
||||
if strings.Contains(t, value) {
|
||||
return tag
|
||||
}
|
||||
return reflect.StructTag(fmt.Sprintf(`gorm:"%s;%s"`, value, t))
|
||||
}
|
||||
|
||||
// GetRelationsValues get relations's values from a reflect value
|
||||
func GetRelationsValues(ctx context.Context, reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value) {
|
||||
for _, rel := range rels {
|
||||
|
@ -263,6 +263,25 @@ func TestMigrateTable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateWithQuotedIndex(t *testing.T) {
|
||||
if DB.Dialector.Name() != "mysql" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
type QuotedIndexStruct struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"size:255;index:AS"` // AS is one of MySQL reserved words
|
||||
}
|
||||
|
||||
if err := DB.Migrator().DropTable(&QuotedIndexStruct{}); err != nil {
|
||||
t.Fatalf("Failed to drop table, got error %v", err)
|
||||
}
|
||||
|
||||
if err := DB.AutoMigrate(&QuotedIndexStruct{}); err != nil {
|
||||
t.Fatalf("Failed to auto migrate, but got error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateIndexes(t *testing.T) {
|
||||
type IndexStruct struct {
|
||||
gorm.Model
|
||||
@ -659,6 +678,42 @@ func TestMigrateWithSpecialName(t *testing.T) {
|
||||
AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_2"))
|
||||
}
|
||||
|
||||
// https://github.com/go-gorm/gorm/issues/5320
|
||||
func TestPrimarykeyID(t *testing.T) {
|
||||
if DB.Dialector.Name() != "postgres" {
|
||||
return
|
||||
}
|
||||
|
||||
type MissPKLanguage struct {
|
||||
ID string `gorm:"type:uuid;default:uuid_generate_v4()"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type MissPKUser struct {
|
||||
ID string `gorm:"type:uuid;default:uuid_generate_v4()"`
|
||||
MissPKLanguages []MissPKLanguage `gorm:"many2many:miss_pk_user_languages;"`
|
||||
}
|
||||
|
||||
var err error
|
||||
err = DB.Migrator().DropTable(&MissPKUser{}, &MissPKLanguage{})
|
||||
if err != nil {
|
||||
t.Fatalf("DropTable err:%v", err)
|
||||
}
|
||||
|
||||
DB.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`)
|
||||
|
||||
err = DB.AutoMigrate(&MissPKUser{}, &MissPKLanguage{})
|
||||
if err != nil {
|
||||
t.Fatalf("AutoMigrate err:%v", err)
|
||||
}
|
||||
|
||||
// patch
|
||||
err = DB.AutoMigrate(&MissPKUser{}, &MissPKLanguage{})
|
||||
if err != nil {
|
||||
t.Fatalf("AutoMigrate err:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCachedPlan(t *testing.T) {
|
||||
if DB.Dialector.Name() != "postgres" {
|
||||
return
|
||||
|
@ -367,3 +367,33 @@ func TestTransactionOnClosedConn(t *testing.T) {
|
||||
t.Errorf("should returns error when commit with closed conn, got error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransactionWithHooks(t *testing.T) {
|
||||
user := GetUser("tTestTransactionWithHooks", Config{Account: true})
|
||||
DB.Create(&user)
|
||||
|
||||
var err error
|
||||
err = DB.Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Model(&User{}).Limit(1).Transaction(func(tx2 *gorm.DB) error {
|
||||
return tx2.Scan(&User{}).Error
|
||||
})
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// method with hooks
|
||||
err = DB.Transaction(func(tx1 *gorm.DB) error {
|
||||
// callMethod do
|
||||
tx2 := tx1.Find(&User{}).Session(&gorm.Session{NewDB: true})
|
||||
// trx in hooks
|
||||
return tx2.Transaction(func(tx3 *gorm.DB) error {
|
||||
return tx3.Where("user_id", user.ID).Delete(&Account{}).Error
|
||||
})
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user