Skip to content

Commit

Permalink
Replace internal deleteLeafNodes query to use chunking instead of pag…
Browse files Browse the repository at this point in the history
…ination

Closes #324
  • Loading branch information
stevebauman committed Sep 11, 2021
1 parent bfdcac4 commit d7e7263
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1174,16 +1174,15 @@ public function delete($recursive = false)
*
* @throws \LdapRecord\LdapRecordException
*
* @return Collection
* @return void
*/
protected function deleteLeafNodes()
{
return $this->newQueryWithoutScopes()
$this->newQueryWithoutScopes()
->in($this->dn)
->listing()
->paginate()
->each(function (self $model) {
$model->delete($recursive = true);
->chunk(250, function ($models) {
$models->each->delete($recursive = true);
});
}

Expand Down
9 changes: 7 additions & 2 deletions tests/Models/ModelQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LdapRecord\Tests\Models;

use Closure;
use LdapRecord\Connection;
use LdapRecord\Container;
use LdapRecord\ContainerException;
Expand Down Expand Up @@ -338,13 +339,17 @@ public function test_delete_leaf_nodes()
$query = m::mock(Builder::class);
$query->shouldReceive('listing')->once()->andReturnSelf();
$query->shouldReceive('in')->once()->with('foo')->andReturnSelf();
$query->shouldReceive('paginate')->once()->andReturn($shouldBeDeleted);
$query->shouldReceive('chunk')->once()->with(250, m::on(function ($callback) use ($shouldBeDeleted) {
$callback($shouldBeDeleted);

return $callback instanceof Closure;
}));

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQueryWithoutScopes')->once()->andReturn($query);

$model->setRawAttributes(['dn' => 'foo', 'cn' => 'bar']);
$this->assertEquals($shouldBeDeleted, $model->deleteLeafNodes());
$this->assertNull($model->deleteLeafNodes());
$this->assertFalse($shouldBeDeleted->first()->exists);
}

Expand Down

0 comments on commit d7e7263

Please sign in to comment.