Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Enforce an orderBy clause for chunk() and chunkById() #16283

Merged
merged 2 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ public function cursor()
*/
public function chunk($count, callable $callback)
{
$this->enforceOrderBy();

$page = 1;

do {
Expand Down Expand Up @@ -428,6 +430,8 @@ public function chunk($count, callable $callback)
*/
public function chunkById($count, callable $callback, $column = 'id')
{
$this->enforceOrderBy();

$lastId = 0;

do {
Expand Down Expand Up @@ -458,10 +462,6 @@ public function chunkById($count, callable $callback, $column = 'id')
*/
public function each(callable $callback, $count = 1000)
{
if (empty($this->query->orders) && empty($this->query->unionOrders)) {
$this->orderBy($this->model->getQualifiedKeyName(), 'asc');
}

return $this->chunk($count, function ($results) use ($callback) {
foreach ($results as $key => $value) {
if ($callback($value, $key) === false) {
Expand All @@ -471,6 +471,18 @@ public function each(callable $callback, $count = 1000)
});
}

/**
* Add a generic orderBy clause if the query doesn't already have one.
*
* @return void
*/
protected function enforceOrderBy()
{
if (empty($this->query->orders) && empty($this->query->unionOrders)) {
$this->orderBy($this->model->getQualifiedKeyName(), 'asc');
}
}

/**
* Get an array with the values of a given column.
*
Expand Down
24 changes: 18 additions & 6 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,8 @@ public function cursor()
*/
public function chunk($count, callable $callback)
{
$this->enforceOrderBy();

$page = 1;

do {
Expand Down Expand Up @@ -1842,6 +1844,8 @@ public function chunk($count, callable $callback)
*/
public function chunkById($count, callable $callback, $column = 'id', $alias = null)
{
$this->enforceOrderBy();

$alias = $alias ?: $column;

$lastId = 0;
Expand Down Expand Up @@ -1871,15 +1875,9 @@ public function chunkById($count, callable $callback, $column = 'id', $alias = n
* @param callable $callback
* @param int $count
* @return bool
*
* @throws \RuntimeException
*/
public function each(callable $callback, $count = 1000)
{
if (empty($this->orders) && empty($this->unionOrders)) {
throw new RuntimeException('You must specify an orderBy clause when using the "each" function.');
}

return $this->chunk($count, function ($results) use ($callback) {
foreach ($results as $key => $value) {
if ($callback($value, $key) === false) {
Expand All @@ -1889,6 +1887,20 @@ public function each(callable $callback, $count = 1000)
});
}

/**
* Throw an exception if the query doesn't have an orderBy clause.
*
* @return void
*
* @throws \RuntimeException
*/
protected function enforceOrderBy()
{
if (empty($this->orders) && empty($this->unionOrders)) {
throw new RuntimeException('You must specify an orderBy clause when using this function.');
}
}

/**
* Get an array with the values of a given column.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public function testValueMethodWithModelNotFound()
public function testChunkWithLastChunkComplete()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = new Collection(['foo1', 'foo2']);
$chunk2 = new Collection(['foo3', 'foo4']);
$chunk3 = new Collection([]);
Expand All @@ -178,6 +180,8 @@ public function testChunkWithLastChunkComplete()
public function testChunkWithLastChunkPartial()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = new Collection(['foo1', 'foo2']);
$chunk2 = new Collection(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
Expand All @@ -196,6 +200,8 @@ public function testChunkWithLastChunkPartial()
public function testChunkCanBeStoppedByReturningFalse()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = new Collection(['foo1', 'foo2']);
$chunk2 = new Collection(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
Expand All @@ -216,6 +222,8 @@ public function testChunkCanBeStoppedByReturningFalse()
public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = new Collection([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]);
$chunk3 = new Collection([]);
Expand All @@ -237,6 +245,8 @@ public function testChunkPaginatesUsingIdWithLastChunkComplete()
public function testChunkPaginatesUsingIdWithLastChunkPartial()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = new Collection([(object) ['someIdField' => 10]]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,8 @@ public function testTableValuedFunctionAsTableInSqlServer()
public function testChunkWithLastChunkComplete()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = collect(['foo1', 'foo2']);
$chunk2 = collect(['foo3', 'foo4']);
$chunk3 = collect([]);
Expand All @@ -1764,6 +1766,8 @@ public function testChunkWithLastChunkComplete()
public function testChunkWithLastChunkPartial()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = collect(['foo1', 'foo2']);
$chunk2 = collect(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
Expand All @@ -1782,6 +1786,8 @@ public function testChunkWithLastChunkPartial()
public function testChunkCanBeStoppedByReturningFalse()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = collect(['foo1', 'foo2']);
$chunk2 = collect(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
Expand All @@ -1802,6 +1808,8 @@ public function testChunkCanBeStoppedByReturningFalse()
public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = collect([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]);
$chunk3 = collect([]);
Expand All @@ -1823,6 +1831,8 @@ public function testChunkPaginatesUsingIdWithLastChunkComplete()
public function testChunkPaginatesUsingIdWithLastChunkPartial()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = collect([(object) ['someIdField' => 10]]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
Expand All @@ -1841,6 +1851,8 @@ public function testChunkPaginatesUsingIdWithLastChunkPartial()
public function testChunkPaginatesUsingIdWithAlias()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = collect([(object) ['table_id' => 1], (object) ['table_id' => 10]]);
$chunk2 = collect([]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'table.id')->andReturnSelf();
Expand Down