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

[8.x] Apply --except option in combination with --model for model:prune #40110

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/Illuminate/Database/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Finder\Finder;

class PruneCommand extends Command
Expand Down Expand Up @@ -85,28 +86,33 @@ public function handle(Dispatcher $events)
*/
protected function models()
{
if (! empty($models = $this->option('model'))) {
return collect($models);
}

$except = $this->option('except');
$models = collect($this->option('model'));
$except = collect($this->option('except'));

return collect((new Finder)->in(app_path('Models'))->files()->name('*.php'))
->map(function ($model) {
$namespace = $this->laravel->getNamespace();
if ($models->isNotEmpty() && $except->isNotEmpty()) {
throw new InvalidArgumentException("Combining the --except and --model options is not supported.");
}

return $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
);
})->when(! empty($except), function ($models) use ($except) {
return $models->reject(function ($model) use ($except) {
return in_array($model, $except);
if ($models->isEmpty()) {
collect((new Finder)->in(app_path('Models'))->files()->name('*.php'))
->map(function ($model) {
$namespace = $this->laravel->getNamespace();

return $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
);
});
})->filter(function ($model) {
return $this->isPrunable($model);
})->values();
}

return $models->when($except->isNotEmpty(), function ($models) use ($except) {
return $models->reject(function ($model) use ($except) {
return $except->contains($model);
});
})->filter(function ($model) {
return $this->isPrunable($model);
})->values();
}

/**
Expand Down
30 changes: 29 additions & 1 deletion tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testNonPrunableTest()
$output = $this->artisan(['--model' => NonPrunableTestModel::class]);

$this->assertEquals(<<<'EOF'
No prunable [Illuminate\Tests\Database\NonPrunableTestModel] records found.
No prunable models found.

EOF, str_replace("\r", '', $output->fetch()));
}
Expand Down Expand Up @@ -159,6 +159,34 @@ public function testTheCommandMayBePretendedOnSoftDeletedModel()
$this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
}

public function testCombinationOfModelAndExceptParameter()
{
$this->expectException(\InvalidArgumentException::class);

$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
$table->string('value')->nullable();
$table->datetime('deleted_at')->nullable();
});
DB::connection('default')->table('prunables')->insert([
['value' => 1, 'deleted_at' => null],
['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'],
['value' => 3, 'deleted_at' => null],
['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'],
]);

$output = $this->artisan([
'--model' => [PrunableTestModelWithPrunableRecords::class, PrunableTestSoftDeletedModelWithPrunableRecords::class],
'--except' => PrunableTestModelWithPrunableRecords::class,
]);
}

protected function artisan($arguments)
{
$input = new ArrayInput($arguments);
Expand Down