fix: FindInBatches Limit zero
This commit is contained in:
		
							parent
							
								
									8b19b51425
								
							
						
					
					
						commit
						f9a86e9148
					
				@ -187,7 +187,7 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
 | 
				
			|||||||
		if limit, ok := c.Expression.(clause.Limit); ok {
 | 
							if limit, ok := c.Expression.(clause.Limit); ok {
 | 
				
			||||||
			totalSize = limit.Limit
 | 
								totalSize = limit.Limit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if batchSize > totalSize {
 | 
								if totalSize > 0 && batchSize > totalSize {
 | 
				
			||||||
				batchSize = totalSize
 | 
									batchSize = totalSize
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -330,14 +330,24 @@ func TestFindInBatchesWithOffsetLimit(t *testing.T) {
 | 
				
			|||||||
		AssertEqual(t, results[i], targetUsers[i])
 | 
							AssertEqual(t, results[i], targetUsers[i])
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var sub1 []User
 | 
				
			||||||
	// limit < batchSize
 | 
						// limit < batchSize
 | 
				
			||||||
	if result := DB.Limit(5).Where("name = ?", users[0].Name).FindInBatches(&sub, 10, func(tx *gorm.DB, batch int) error {
 | 
						if result := DB.Limit(5).Where("name = ?", users[0].Name).FindInBatches(&sub1, 10, func(tx *gorm.DB, batch int) error {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}); 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 {
 | 
						var sub2 []User
 | 
				
			||||||
 | 
						// only offset
 | 
				
			||||||
 | 
						if result := DB.Offset(3).Where("name = ?", users[0].Name).FindInBatches(&sub2, 2, func(tx *gorm.DB, batch int) error {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}); result.Error != nil || result.RowsAffected != 7 {
 | 
				
			||||||
 | 
							t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var sub3 []User
 | 
				
			||||||
 | 
						if result := DB.Limit(4).Where("name = ?", users[0].Name).FindInBatches(&sub3, 2, func(tx *gorm.DB, batch int) error {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}); result.Error != nil || result.RowsAffected != 4 {
 | 
						}); result.Error != nil || result.RowsAffected != 4 {
 | 
				
			||||||
		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)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user