Skip to content

Commit 3769dd0

Browse files
committed
Implement MassPrunable without chunks limit
1 parent ad79fb1 commit 3769dd0

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/Eloquent/MassPrunable.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Eloquent;
4+
5+
use Illuminate\Database\Eloquent\MassPrunable as EloquentMassPrunable;
6+
use Illuminate\Database\Events\ModelsPruned;
7+
8+
/**
9+
* @see \Illuminate\Database\Eloquent\MassPrunable
10+
*/
11+
trait MassPrunable
12+
{
13+
use EloquentMassPrunable;
14+
15+
/**
16+
* Prune all prunable models in the database.
17+
*/
18+
public function pruneAll(): int
19+
{
20+
$query = $this->prunable();
21+
$total = in_array(SoftDeletes::class, class_uses_recursive(get_class($this)))
22+
? $query->forceDelete()
23+
: $query->delete();
24+
25+
event(new ModelsPruned(static::class, $total));
26+
27+
return $total;
28+
}
29+
}

tests/Eloquent/MassPrunableTest.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Eloquent;
6+
7+
use Illuminate\Database\Console\PruneCommand;
8+
use Illuminate\Database\Eloquent\MassPrunable;
9+
use Illuminate\Database\Eloquent\Prunable;
10+
use MongoDB\Laravel\Tests\Models\Soft;
11+
use MongoDB\Laravel\Tests\Models\User;
12+
use MongoDB\Laravel\Tests\TestCase;
13+
14+
class MassPrunableTest extends TestCase
15+
{
16+
public function tearDown(): void
17+
{
18+
User::truncate();
19+
Soft::truncate();
20+
}
21+
22+
public function testPruneWithQuery(): void
23+
{
24+
$this->assertTrue($this->isPrunable(User::class));
25+
26+
User::insert([
27+
['name' => 'John Doe', 'age' => 35],
28+
['name' => 'Jane Doe', 'age' => 32],
29+
['name' => 'Tomy Doe', 'age' => 11],
30+
]);
31+
32+
$model = new User();
33+
$total = $model->pruneAll();
34+
$this->assertEquals(2, $total);
35+
$this->assertEquals(1, User::count());
36+
}
37+
38+
public function testPruneSoftDelete(): void
39+
{
40+
$this->assertTrue($this->isPrunable(Soft::class));
41+
42+
Soft::insert([
43+
['name' => 'John Doe'],
44+
['name' => 'Jane Doe'],
45+
]);
46+
47+
$model = new Soft();
48+
$total = $model->pruneAll();
49+
$this->assertEquals(2, $total);
50+
$this->assertEquals(0, Soft::count());
51+
$this->assertEquals(0, Soft::withTrashed()->count());
52+
}
53+
54+
/**
55+
* @see PruneCommand::isPrunable()
56+
*/
57+
protected function isPrunable($model)
58+
{
59+
$uses = class_uses_recursive($model);
60+
61+
return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses);
62+
}
63+
}

tests/Models/Soft.php

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MongoDB\Laravel\Tests\Models;
66

7+
use MongoDB\Laravel\Eloquent\Builder;
8+
use MongoDB\Laravel\Eloquent\MassPrunable;
79
use MongoDB\Laravel\Eloquent\Model as Eloquent;
810
use MongoDB\Laravel\Eloquent\SoftDeletes;
911

@@ -15,9 +17,15 @@
1517
class Soft extends Eloquent
1618
{
1719
use SoftDeletes;
20+
use MassPrunable;
1821

1922
protected $connection = 'mongodb';
2023
protected $collection = 'soft';
2124
protected static $unguarded = true;
2225
protected $casts = ['deleted_at' => 'datetime'];
26+
27+
public function prunable(): Builder
28+
{
29+
return $this->newQuery();
30+
}
2331
}

tests/Models/User.php

+8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use Illuminate\Database\Eloquent\Casts\Attribute;
1313
use Illuminate\Notifications\Notifiable;
1414
use Illuminate\Support\Str;
15+
use MongoDB\Laravel\Eloquent\Builder;
1516
use MongoDB\Laravel\Eloquent\HybridRelations;
17+
use MongoDB\Laravel\Eloquent\MassPrunable;
1618
use MongoDB\Laravel\Eloquent\Model as Eloquent;
1719

1820
/**
@@ -35,6 +37,7 @@ class User extends Eloquent implements AuthenticatableContract, CanResetPassword
3537
use CanResetPassword;
3638
use HybridRelations;
3739
use Notifiable;
40+
use MassPrunable;
3841

3942
protected $connection = 'mongodb';
4043
protected $casts = [
@@ -106,4 +109,9 @@ protected function username(): Attribute
106109
set: fn ($value) => Str::slug($value)
107110
);
108111
}
112+
113+
public function prunable(): Builder
114+
{
115+
return $this->where('age', '>', 18);
116+
}
109117
}

0 commit comments

Comments
 (0)