Compare commits

...

4 Commits

Author SHA1 Message Date
Cr. 17bcbdd914
Merge 68fccb87b0 into 49bbaa637f 2024-11-14 17:50:31 +08:00
Ivan Ryabov 49bbaa637f
use map look-up for indexes (#7242) 2024-11-14 17:41:43 +08:00
홍성욱 b0d70a26d1
[#6372] Fixed nullable constraint bug for columns during auto migration (#7269)
* [#6372] Fixed nullable constraint bug for columns during auto migration

* [#6372] fix comment

* [#6372] Add test code

* [#6372] Add test code

* [#6372] Fix failed test case

* [#6372] Fix failed test case

* [#6372] wip

* [#6372] wip

* [#6372] wip

* [#6372] wip
2024-11-14 17:40:18 +08:00
a631807682 68fccb87b0
fix: merge group conds clause 2024-09-20 15:55:23 +08:00
8 changed files with 150 additions and 18 deletions

View File

@ -380,14 +380,68 @@ func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) {
}
func (db *DB) executeScopes() (tx *DB) {
if len(db.Statement.scopes) == 0 {
return db
}
scopes := db.Statement.scopes
db.Statement.scopes = nil
originClause := db.Statement.Clauses
// use clean db in scope
cleanDB := db.Session(&Session{})
cleanDB.Statement.Clauses = map[string]clause.Clause{}
txs := make([]*DB, 0, len(scopes))
for _, scope := range scopes {
db = scope(db)
txs = append(txs, scope(cleanDB))
}
db.Statement.Clauses = originClause
db.mergeClauses(txs)
return db
}
func (db *DB) mergeClauses(txs []*DB) {
if len(txs) == 0 {
return
}
for _, tx := range txs {
stmt := tx.Statement
if stmt != nil {
stmtClause := stmt.Clauses
// merge clauses
if cs, ok := stmtClause["WHERE"]; ok {
if where, ok := cs.Expression.(clause.Where); ok {
db.Statement.AddClause(where)
}
}
// cover other expr
if stmt.TableExpr != nil {
db.Statement.TableExpr = stmt.TableExpr
}
if stmt.Table != "" {
db.Statement.Table = stmt.Table
}
if stmt.Model != nil {
db.Statement.Model = stmt.Model
}
if stmt.Selects != nil {
db.Statement.Selects = stmt.Selects
}
if stmt.Omits != nil {
db.Statement.Omits = stmt.Omits
}
}
}
}
// Preload preload associations with given conditions
//
// // get all users, and preload all non-cancelled orders
@ -448,9 +502,10 @@ func (db *DB) Assign(attrs ...interface{}) (tx *DB) {
// Unscoped allows queries to include records marked as deleted,
// overriding the soft deletion behavior.
// Example:
// var users []User
// db.Unscoped().Find(&users)
// // Retrieves all users, including deleted ones.
//
// var users []User
// db.Unscoped().Find(&users)
// // Retrieves all users, including deleted ones.
func (db *DB) Unscoped() (tx *DB) {
tx = db.getInstance()
tx.Statement.Unscoped = true

View File

@ -524,8 +524,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
// check nullable
if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull {
// not primary key & database is nullable
if !field.PrimaryKey && nullable {
// not primary key & current database is non-nullable(to be nullable)
if !field.PrimaryKey && !nullable {
alterColumn = true
}
}

View File

@ -76,11 +76,12 @@ func (schema *Schema) ParseIndexes() map[string]Index {
func (schema *Schema) LookIndex(name string) *Index {
if schema != nil {
indexes := schema.ParseIndexes()
for _, index := range indexes {
if index.Name == name {
return &index
}
if index, found := indexes[name]; found {
return &index
}
for _, index := range indexes {
for _, field := range index.Fields {
if field.Name == name {
return &index
@ -111,10 +112,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
idx = len(tag)
}
if idx != -1 {
name = tag[0:idx]
}
name = tag[0:idx]
if name == "" {
subName := field.Name
const key = "COMPOSITE"

View File

@ -21,6 +21,9 @@ type UserIndex struct {
Name7 string `gorm:"index:type"`
Name8 string `gorm:"index:,length:10;index:,collate:utf8"`
CompName1 string `gorm:"index:,unique,composite:idx_compname_1,option:NULLS NOT DISTINCT;not null"`
CompName2 string `gorm:"index:,composite:idx_compname_1"`
// Composite Index: Flattened structure.
Data0A string `gorm:"index:,composite:comp_id0"`
Data0B string `gorm:"index:,composite:comp_id0"`
@ -154,6 +157,15 @@ func TestParseIndex(t *testing.T) {
Field: &schema.Field{Name: "Data2B"},
}},
},
"idx_user_indices_idx_compname_1": {
Class: "UNIQUE",
Name: "idx_user_indices_idx_compname_1",
Option: "NULLS NOT DISTINCT",
Fields: []schema.IndexOption{
{Field: &schema.Field{Name: "CompName1", NotNull: true}},
{Field: &schema.Field{Name: "CompName2"}},
},
},
}
CheckIndices(t, results, user.ParseIndexes())
@ -253,7 +265,7 @@ func CheckIndices(t *testing.T, expected, actual map[string]schema.Index) {
}
for i, ef := range ei.Fields {
af := ai.Fields[i]
tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length")
tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length", "NotNull")
}
})
delete(actual, k)

View File

@ -325,7 +325,7 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
case clause.Expression:
conds = append(conds, v)
case *DB:
v.executeScopes()
v = v.executeScopes()
if cs, ok := v.Statement.Clauses["WHERE"]; ok {
if where, ok := cs.Expression.(clause.Where); ok {
@ -334,6 +334,7 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
where.Exprs[0] = clause.AndConditions(orConds)
}
}
conds = append(conds, clause.And(where.Exprs...))
} else if cs.Expression != nil {
conds = append(conds, cs.Expression)

View File

@ -10,7 +10,7 @@ require (
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlserver v1.5.3
gorm.io/driver/sqlserver v1.5.4
gorm.io/gorm v1.25.12
)

View File

@ -141,6 +141,48 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
}
}
func TestAutoMigrateNullable(t *testing.T) {
type MigrateNullableColumn struct {
ID uint
Bonus float64 `gorm:"not null"`
Stock float64
}
DB.Migrator().DropTable(&MigrateNullableColumn{})
DB.AutoMigrate(&MigrateNullableColumn{})
type MigrateNullableColumn2 struct {
ID uint
Bonus float64
Stock float64 `gorm:"not null"`
}
if err := DB.Table("migrate_nullable_columns").AutoMigrate(&MigrateNullableColumn2{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
columnTypes, err := DB.Table("migrate_nullable_columns").Migrator().ColumnTypes(&MigrateNullableColumn{})
if err != nil {
t.Fatalf("failed to get column types, got error: %v", err)
}
for _, columnType := range columnTypes {
switch columnType.Name() {
case "bonus":
// allow to change non-nullable to nullable
if nullable, _ := columnType.Nullable(); !nullable {
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
}
case "stock":
// do not allow to change nullable to non-nullable
if nullable, _ := columnType.Nullable(); !nullable {
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
}
}
}
}
func TestSmartMigrateColumn(t *testing.T) {
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]

View File

@ -90,7 +90,31 @@ func TestComplexScopes(t *testing.T) {
).Find(&Language{})
},
expected: `SELECT * FROM "languages" WHERE a = 1 AND (b = 2 OR c = 3)`,
}, {
},
{
name: "group_cond",
queryFn: func(tx *gorm.DB) *gorm.DB {
return tx.Scopes(
func(d *gorm.DB) *gorm.DB { return d.Table("languages1") },
func(d *gorm.DB) *gorm.DB { return d.Table("languages2") },
func(d *gorm.DB) *gorm.DB {
return d.Where(
d.Where("a = 1").Or("b = 2"),
)
},
func(d *gorm.DB) *gorm.DB {
return d.Select("f1, f2")
},
func(d *gorm.DB) *gorm.DB {
return d.Where(
d.Where("c = 3"),
)
},
).Find(&Language{})
},
expected: `SELECT f1, f2 FROM "languages2" WHERE (a = 1 OR b = 2) AND c = 3`,
},
{
name: "depth_1_pre_cond",
queryFn: func(tx *gorm.DB) *gorm.DB {
return tx.Where("z = 0").Scopes(