codenco-dev/laravel-eloquent-pruning

This package is abandoned and no longer maintained. No replacement package was suggested.

We often have to clean up our oldest or unnecessary records. This package does it easily for us.

v1.2.0 2020-11-27 18:05 UTC

This package is auto-updated.

Last update: 2023-01-27 22:45:31 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

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 pruned
  • hours - The hours count that will determine if a record should pruned relative to pruning_column and now datetime
  • with_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.