Skip to content

Commit

Permalink
Add ability to pass models to purge in argument of the purge command …
Browse files Browse the repository at this point in the history
…+ Add 'since' option for the purge command
  • Loading branch information
alajusticia committed Jul 14, 2023
1 parent 5489543 commit 444fdb0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,35 @@ if ($subscription->isExpired()) {

### Purge expired records

This package comes with a command to delete expired records from the database.
This package comes with a command to delete expired records from the database:

In order to indicate that a model should be purged, add its class to the `purge` array of
the configuration file:
```
php artisan expirable:purge
```

You have two ways to indicate which models should be purged:

- add their class to the `purge` array of the configuration file:

```php
'purge' => [
\App\Models\Subscription::class,
],
```

Then, run this command: `php artisan expirable:purge`
- pass one or several classes in argument of the purge command:

```
php artisan expirable:purge "App\Models\Subscription" "App\Models\Coupon"
```

Models passed as arguments take precedence (the `purge` array in the configuration file will be ignored).

You can also specify a period of time to delete models expired since that given period, using the `since` option (the value of this option is passed to the [expiredSince query scope](#retrieving-expired-models-since)):

```
php artisan expirable:purge "App\Models\Subscription" --since="2 months"
```

## License

Expand Down
26 changes: 20 additions & 6 deletions src/Commands/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class PurgeCommand extends Command
*
* @var string
*/
protected $signature = 'expirable:purge';
protected $signature = 'expirable:purge
{model?* : Optional list of models to purge. If not provided, will take the models in the purge array of the configuration file.}
{--since= : Time since expiration.}';

/**
* The console command description.
Expand All @@ -30,21 +32,33 @@ class PurgeCommand extends Command
*/
public function handle()
{
$models = Config::get('expirable.purge', []);
$models = $this->argument('model');

if (empty($models)) {
$models = Config::get('expirable.purge', []);
}

if (count($models)) {

$expiredSince = $this->option('since');

$this->line('');
$this->comment('Deleting expired records...');
$this->line('');

foreach (Config::get('expirable.purge', []) as $purgeable) {
foreach ($models as $purgeable) {

$this->line($purgeable . ': ');

if (in_array(Expirable::class, class_uses_recursive($purgeable))) {

$total = call_user_func($purgeable . '::onlyExpired')->forceDelete();
if (!$expiredSince) {
$query = call_user_func($purgeable . '::onlyExpired');
} else {
$query = call_user_func($purgeable . '::expiredSince', $expiredSince);
}

$total = $query->forceDelete();

if ($total > 0) {
$this->info($total . ' ' . Str::plural('record', $total) . ' deleted.');
Expand All @@ -62,8 +76,8 @@ public function handle()
$this->info('Purge completed!');
$this->line('');
} else {
$this->comment('There is no model in the purge array.');
$this->comment('Add models you want to purge in the expirable.php configuration file.');
$this->comment('There is no model to purge.');
$this->comment('Add models you want to purge in the expirable.php configuration file or pass models in argument of this command.');
}
}
}

0 comments on commit 444fdb0

Please sign in to comment.