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

[12.x] Dont stop pruning if pruning one model fails #55237

Merged
merged 1 commit into from
Apr 1, 2025
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
18 changes: 16 additions & 2 deletions src/Illuminate/Database/Eloquent/Prunable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Illuminate\Database\Eloquent;

use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Database\Events\ModelsPruned;
use LogicException;
use Throwable;

trait Prunable
{
Expand All @@ -21,9 +23,21 @@ public function pruneAll(int $chunkSize = 1000)
->when(in_array(SoftDeletes::class, class_uses_recursive(static::class)), function ($query) {
$query->withTrashed();
})->chunkById($chunkSize, function ($models) use (&$total) {
$models->each->prune();
$models->each(function ($model) use (&$total) {
try {
$model->prune();

$total += $models->count();
$total++;
} catch (Throwable $e) {
$handler = app(ExceptionHandler::class);

if ($handler) {
$handler->report($e);
} else {
throw $e;
}
}
});

event(new ModelsPruned(static::class, $total));
});
Expand Down
41 changes: 41 additions & 0 deletions tests/Integration/Database/EloquentPrunableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Illuminate\Tests\Integration\Database;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Exceptions;
use Illuminate\Support\Facades\Schema;
use LogicException;

Expand All @@ -20,6 +22,7 @@ protected function afterRefreshingDatabase()
'prunable_soft_delete_test_models',
'prunable_test_model_missing_prunable_methods',
'prunable_with_custom_prune_method_test_models',
'prunable_with_exceptions',
])->each(function ($table) {
Schema::create($table, function (Blueprint $table) {
$table->increments('id');
Expand Down Expand Up @@ -97,6 +100,27 @@ public function testPruneWithCustomPruneMethod()

Event::assertDispatched(ModelsPruned::class, 1);
}

public function testPruneWithExceptionAtOneOfModels()
{
Event::fake();
Exceptions::fake();

collect(range(1, 5000))->map(function ($id) {
return ['name' => 'foo'];
})->chunk(200)->each(function ($chunk) {
PrunableWithException::insert($chunk->all());
});

$count = (new PrunableWithException)->pruneAll();

$this->assertEquals(999, $count);

Event::assertDispatched(ModelsPruned::class, 1);
Event::assertDispatched(fn (ModelsPruned $event) => $event->count === 999);
Exceptions::assertReportedCount(1);
Exceptions::assertReported(fn (Exception $exception) => $exception->getMessage() === 'foo bar');
}
}

class PrunableTestModel extends Model
Expand Down Expand Up @@ -136,6 +160,23 @@ public function prune()
}
}

class PrunableWithException extends Model
{
use Prunable;

public function prunable()
{
return $this->where('id', '<=', 1000);
}

public function prune()
{
if ($this->id === 500) {
throw new Exception('foo bar');
}
}
}

class PrunableTestModelMissingPrunableMethod extends Model
{
use Prunable;
Expand Down
Loading