-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MassPrunable without chunks limit
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace MongoDB\Laravel\Eloquent; | ||
|
||
use Illuminate\Database\Eloquent\MassPrunable as EloquentMassPrunable; | ||
use Illuminate\Database\Events\ModelsPruned; | ||
|
||
/** | ||
* @see \Illuminate\Database\Eloquent\MassPrunable | ||
*/ | ||
trait MassPrunable | ||
{ | ||
use EloquentMassPrunable; | ||
|
||
/** | ||
* Prune all prunable models in the database. | ||
*/ | ||
public function pruneAll(): int | ||
{ | ||
$query = $this->prunable(); | ||
$total = in_array(SoftDeletes::class, class_uses_recursive(get_class($this))) | ||
? $query->forceDelete() | ||
: $query->delete(); | ||
|
||
event(new ModelsPruned(static::class, $total)); | ||
|
||
return $total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eloquent; | ||
|
||
use Illuminate\Database\Console\PruneCommand; | ||
use Illuminate\Database\Eloquent\MassPrunable; | ||
use Illuminate\Database\Eloquent\Prunable; | ||
use MongoDB\Laravel\Tests\Models\Soft; | ||
use MongoDB\Laravel\Tests\Models\User; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class MassPrunableTest extends TestCase | ||
{ | ||
public function tearDown(): void | ||
{ | ||
User::truncate(); | ||
Soft::truncate(); | ||
} | ||
|
||
public function testPruneWithQuery(): void | ||
{ | ||
$this->assertTrue($this->isPrunable(User::class)); | ||
|
||
User::insert([ | ||
['name' => 'John Doe', 'age' => 35], | ||
['name' => 'Jane Doe', 'age' => 32], | ||
['name' => 'Tomy Doe', 'age' => 11], | ||
]); | ||
|
||
$model = new User(); | ||
$total = $model->pruneAll(); | ||
$this->assertEquals(2, $total); | ||
$this->assertEquals(1, User::count()); | ||
} | ||
|
||
public function testPruneSoftDelete(): void | ||
{ | ||
$this->assertTrue($this->isPrunable(Soft::class)); | ||
|
||
Soft::insert([ | ||
['name' => 'John Doe'], | ||
['name' => 'Jane Doe'], | ||
]); | ||
|
||
$model = new Soft(); | ||
$total = $model->pruneAll(); | ||
$this->assertEquals(2, $total); | ||
$this->assertEquals(0, Soft::count()); | ||
$this->assertEquals(0, Soft::withTrashed()->count()); | ||
} | ||
|
||
/** | ||
* @see PruneCommand::isPrunable() | ||
*/ | ||
protected function isPrunable($model) | ||
{ | ||
$uses = class_uses_recursive($model); | ||
|
||
return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters