fix: break first

This commit is contained in:
a631807682 2022-04-14 15:25:02 +08:00
parent cd35587764
commit 8b19b51425
No known key found for this signature in database
GPG Key ID: 137D1D75522168AB
2 changed files with 9 additions and 4 deletions

View File

@ -212,13 +212,12 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
} }
if totalSize > 0 { if totalSize > 0 {
if totalSize/batchSize == batch {
batchSize = totalSize % batchSize
}
if totalSize <= int(rowsAffected) { if totalSize <= int(rowsAffected) {
break break
} }
if totalSize/batchSize == batch {
batchSize = totalSize % batchSize
}
} }
// Optimize for-break // Optimize for-break

View File

@ -336,6 +336,12 @@ func TestFindInBatchesWithOffsetLimit(t *testing.T) {
}); result.Error != nil || result.RowsAffected != 5 { }); result.Error != nil || result.RowsAffected != 5 {
t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected) t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
} }
if result := DB.Limit(4).Where("name = ?", users[0].Name).FindInBatches(&sub, 2, func(tx *gorm.DB, batch int) error {
return nil
}); result.Error != nil || result.RowsAffected != 4 {
t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
}
} }
func TestFindInBatchesWithError(t *testing.T) { func TestFindInBatchesWithError(t *testing.T) {