codenco-dev / laravel-eloquent-pruning
We often have to clean up our oldest or unnecessary records. This package does it easily for us.
Fund package maintenance!
thomasdominic
Requires
- php: ^7.2.5
- illuminate/support: ^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^8.0
- psalm/plugin-laravel: ^1.3
- vimeo/psalm: ^3.11
README
We often have to clean up the records of our databases by deleting the oldest and useless data. This package allows, via Eloquent models, to make this easier.
Installation
You can install the package via composer:
composer require codenco-dev/laravel-eloquent-pruning
Configuration
We have several options that can be configure.
pruning_column
- The name of the column that will be used to determine if a record should be prunedhours
- The hours count that will determine if a record should pruned relative to pruning_column and now datetimewith_delete_events
- If the value is true, the delete method of model will be call, allowing fire events. If the value is false, the delete action will be done with query builder, without event.chunk_size
- The size of delete query if with_delete_events is false
If all prunable models have the same configuration, you can do it in the laravel-eloquent-pruning
file.
You can publish configuration file with this command line
php artisan vendor:publish --provider="CodencoDev\LaravelEloquentPruning\LaravelEloquentPruningServiceProvider" --tag="config"
If all prunable models don't have the same configuration, each options for a model can be defined in the model file like this :
class MyModel extends Model { use Prunable; protected $fillable = ['id']; protected $hours = 1; protected $pruningColumn = 'updated_at'; protected $chunkSize = 1000; protected $withDeleteEvents = false; }
If you need something more elaborate, you be able to overwrite, Prunable
trait methods.
class MyModel extends Model { use Prunable; protected $fillable = ['id']; public function getHours(): int { return 1; } public function getPruningColumn(): string { return 'created_at'; } public function getChunkSize(): int { return 1000; } public function getWithDeleteEvents(): bool { return false; } }
In configuration file, you can manage default value for the Pruning Package. You must define models that will be affected by pruning.
'models' => [ App\MyModel::class, App\MySecondModel::class, ],
If some records can't be prune, with business logic, you can use this methods :
If with_delete_events
is false, the business logic must be put in scopeCouldBePruned
like this
/** * Scope that allows filter records for pruning. */ public function scopeCouldBePruned(Builder $query): Builder { return $query->where('status','done'); }
If with_delete_events
is true, the business logic can be put in scopeCouldBePruned
but also by overwrite canBePruned
method like this
/** * Define if the active record can be pruned, if the ProcessWithDeleteEvents is true. */ public function canBePruned(): bool { //tests, extern call etc. return MyService::checkMyModelIsDeletable($this->id); }
Usage
You can add global pruning on your schedule by modifying app/Console/Kernel.php
like this for example
protected function schedule(Schedule $schedule) { $schedule->command('pruning:start')->hourly(); }
If you want bypass model hour configuration, you can call this command with hours
option like this :
protected function schedule(Schedule $schedule) { $schedule->command('pruning:start --hours=48])->hourly(); }
This call allows to prune all data created more than 48 hours ago.
Of course, you can use a schedule by model (or create a dedicated command if you want) :
protected function schedule(Schedule $schedule) { $schedule->call(function(){ (new MyModel)->prune(); })->daily(); }
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email dthomas@codenco.io instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
Inspiration
This package was inspired by laravel/telescope pruning.