mirror of https://github.com/go-gorm/gorm.git
Fix: FindInBatches ignores errors (#4203)
This commit is contained in:
parent
8c92d9694a
commit
26dd4c980a
|
@ -190,6 +190,8 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
|
||||||
|
|
||||||
if result.Error == nil && result.RowsAffected != 0 {
|
if result.Error == nil && result.RowsAffected != 0 {
|
||||||
tx.AddError(fc(result, batch))
|
tx.AddError(fc(result, batch))
|
||||||
|
} else if result.Error != nil {
|
||||||
|
tx.AddError(result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.Error != nil || int(result.RowsAffected) < batchSize {
|
if tx.Error != nil || int(result.RowsAffected) < batchSize {
|
||||||
|
|
|
@ -292,6 +292,34 @@ func TestFindInBatches(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindInBatchesWithError(t *testing.T) {
|
||||||
|
var users = []User{
|
||||||
|
*GetUser("find_in_batches_with_error", Config{}),
|
||||||
|
*GetUser("find_in_batches_with_error", Config{}),
|
||||||
|
*GetUser("find_in_batches_with_error", Config{}),
|
||||||
|
*GetUser("find_in_batches_with_error", Config{}),
|
||||||
|
*GetUser("find_in_batches_with_error", Config{}),
|
||||||
|
*GetUser("find_in_batches_with_error", Config{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.Create(&users)
|
||||||
|
|
||||||
|
var (
|
||||||
|
results []User
|
||||||
|
totalBatch int
|
||||||
|
)
|
||||||
|
|
||||||
|
if result := DB.Table("wrong_table").Where("name = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
|
||||||
|
totalBatch += batch
|
||||||
|
return nil
|
||||||
|
}); result.Error == nil || result.RowsAffected > 0 {
|
||||||
|
t.Fatal("expected errors to have occurred, but nothing happened")
|
||||||
|
}
|
||||||
|
if totalBatch != 0 {
|
||||||
|
t.Fatalf("incorrect total batch, expected: %v, got: %v", 0, totalBatch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFillSmallerStruct(t *testing.T) {
|
func TestFillSmallerStruct(t *testing.T) {
|
||||||
user := User{Name: "SmallerUser", Age: 100}
|
user := User{Name: "SmallerUser", Age: 100}
|
||||||
DB.Save(&user)
|
DB.Save(&user)
|
||||||
|
|
Loading…
Reference in New Issue