GithubHelp home page GithubHelp logo

spatie / laravel-model-cleanup Goto Github PK

View Code? Open in Web Editor NEW
399.0 13.0 42.0 170 KB

Clean up unneeded records

Home Page: https://freek.dev/410-a-laravel-package-to-clean-up-models

License: MIT License

PHP 100.00%
laravel php eloquent models performance

laravel-model-cleanup's Introduction

馃毃 THIS PACKAGE IS NO LONGER MAINTAINED, WE RECOMMEND TO USE LARAVEL'S BUILT IN PRUNABLE 馃毃

Clean up unneeded records

Latest Version on Packagist Tests Psalm Total Downloads MIT Licensed

This package will clean up old records.

The models you wish to clean up should have a method cleanUp which returns the configuration how the model should be cleaned up. Here's an example where all records older than 5 days will be cleaned up.

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelCleanup\CleanupConfig;
use Spatie\ModelCleanup\GetsCleanedUp;

class YourModel extends Model implements GetsCleanedUp
{
    ...
    
     public function cleanUp(CleanupConfig $config): void
     {
         $config->olderThanDays(5);
     }
}

After registering the model in the config file, running the clean:models artisan command will delete all records that have been created more than 5 days ago.

The package contains various other methods for specifying which records should be deleted.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-model-cleanup

Next, you must publish the config file:

php artisan vendor:publish --provider="Spatie\ModelCleanup\ModelCleanupServiceProvider"

This is the content of the published config file model-cleanup.php.

return [

    /*
     * All models in this array that implement `Spatie\ModelCleanup\GetsCleanedUp`
     * will be cleaned.
     */
    'models' => [
        // App\Models\YourModel::class,
    ],
];

Optionally, you can schedule the Spatie\ModelCleanup\Commands\CleanUpModelsCommand to run at a frequency of which you want to clean up models. Here's an example where all models will be cleaned up every day at midnight.

// in app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command(\Spatie\ModelCleanup\Commands\CleanUpModelsCommand::class)->daily();
}

Usage

All models that you want to clean up must implement the GetsCleanedUp-interface. In the required cleanUp-method you can specify which records are considered old and should be deleted.

Here's an example where all records older than 5 days will be cleaned up.

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelCleanup\CleanupConfig;
use Spatie\ModelCleanup\GetsCleanedUp;

class YourModel extends Model implements GetsCleanedUp
{
    ...
    
     public function cleanUp(CleanupConfig $config): void
     {
        $config->olderThanDays(5);
     }
}

Next, you should register this model in the models key of the model-cleanup config file.

// in config/model-cleanup.php

return [
    'models' => [
        App\Models\YourModel::class,
    ],
    
    // ...
]

When running the console command clean:models all models older than 5 days will be deleted.

Soft deleted models

This package also supports cleaning up models that have soft deleting enabled. Models that use the Illuminate\Database\Eloquent\SoftDeletes trait and are considered old, will be permanently removed from your database instead of being marked as deleted.

Available methods on CleanupConfig

olderThanDays

Using this method you can mark records that have a created_at value older than a given number of days as old.

Here's an example where all models older than 5 days are considered old.

 public function cleanUp(CleanupConfig $config): void
 {
    $config->olderThanDays(5);
 }

olderThan

The olderThan method accepts an instance of Carbon. All models with a created_at value before that instance, will be considered old.

Here's an example where all models older than a year are considered old.

 public function cleanUp(CleanupConfig $config): void
 {
    $config->olderThan(now()->subYear());
 }

useDateAttribute

When using olderThanDays and olderThan methods, the deletion query that is built up behind the scenes will use the created_at column. You can specify an alternative column, using the useDateAttribute method.

 public function cleanUp(CleanupConfig $config): void
 {
    $config
        ->olderThanDays(5)
        ->useDateAttribute('custom_date_column');
 }

scope

Using the scope method you can make the query that will delete old records more specific.

Assume that your model has a status attribute. Only records with a status inactive may be cleaned up. Here's an example where all records with an inactive status that are older than 5 days will be cleaned up.

 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->scope(fn (Illuminate\Database\Eloquent\Builder $query) => $query->where('status', 'inactive'));
}

chunk

By default, models get cleaned up by performing a single delete query. When you want to clean up a very large table, this single query could lock your table for a long time. It even might not be possible to get the lock in the first place.

To solve this, the package can delete records in chunks using the chunk method.

In this example, all records older than 5 days will be deleted in chucks of a 1000 records.

 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->chunk(1000);
}

The package will stop deleting records when there are no more left that should be deleted.

If you need more fine-grained control over when to stop deleting, you can pass a closure as a second argument to chunk. Returning false in the closure will stop the deletion process.

In the example below, the deletion process will continue until all records older than 5 days are deleted or the record count of the model goes below 5000.

 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->chunk(1000, fn() => YourModel::count() > 5000);
}

Events

After the model has been cleaned Spatie\ModelCleanup\Events\ModelCleanedUp will be fired even if there were no records deleted.

It has two public properties: model, which contains an instance of the model which was cleaned up. and numberOfDeletedRecords.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-model-cleanup's People

Contributors

adrianmrn avatar akoepcke avatar backendtea avatar bolechen avatar brendt avatar carusogabriel avatar devinfd avatar dmason30 avatar drfraker avatar dwightwatson avatar francislavoie avatar freekmurze avatar jochensengier avatar lloy0076 avatar michelecurletta avatar nunomaduro avatar omranic avatar pktharindu avatar riasvdv avatar sebastiandedeyne avatar skalero01 avatar tnorthcutt avatar tobiasfroberg avatar wardhache avatar willemwollebrants avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-model-cleanup's Issues

Can I take v2 and maintain it as a separate package?

Hi 馃憢,

I have upgraded to v3 on my projects that are PHP 7.4 compatible and its great. However, some of my work projects are stuck on PHP 7.3 for a while yet due to security protocols but we still upgrade laravel when we can (e.g. 8.x) and the directories feature was useful in our DDD projects.

I was wondering If I could please extract what was the last released v2 code and release it as a separate package and maintain it. I will obviously credit and link to this package where needed. There might be some refactoring but the code will be largely the same. I just wanted to ask permission so that I don't end up in a Laracatch situation 馃憖.

Thanks,

Dan

[PROPOSAL] Allow configurable delete command

  • I have read and understood the contributors guide.
  • I have checked the pull requests tab for existing solutions/implementations to my issue/suggestion.
  • I have checked that the bug-fix I am reporting can be replicated.

It would be fantastic if you could configure the method to delete these items. Right now it calls

$class::cleanUp($class::query())->delete();

If we could configure the ->delete() method of that it would allow you to run a periodic check to ->purge() soft deleted items.

For instance I could say "1 year after I have soft-deleted this model, clean it up by purging it from the database".

Right now I'm not sure of the best way to implement this for a PR.

Option A could be a $cleanup_method variable on the model with a default of 'delete';

Option B could be adding a cleanupMethod() function to the interface but that would be a breaking change for anyone already using this... However it is only a few hours old so I'm not sure how you feel about it...

Error with larastan.

When i use nunomaduro/larastan package (https://github.com/nunomaduro/larastan) to analyse my code, is have error

 ------ -------------------------------------------------------------------------------------------------
  Line   app/YouzanPush.php
 ------ -------------------------------------------------------------------------------------------------
  84     Method App\YouzanPush::cleanUp() should return Illuminate\Database\Eloquent\Builder but returns
         Illuminate\Database\Eloquent\Collection.
 ------ -------------------------------------------------------------------------------------------------

My code is below:

     * @param Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function cleanUp(Builder $query): Builder
    {
        return $query->onlyTrashed()->where('deleted_at', '<', Carbon::now()->subMonth());
    }
路路路

Type error on run in getFullyQualifiedClassNameFromFile

Type error: Return value of Spatie\ModelCleanup\CleanUpModelsCommand::getFullyQualifiedClassNameFromFile() must be of the type string, null returned

This seems to happen if when using a glob for the models config, there are some files that don't contain classes (such as traits for example). It should probably filter those out.

The config I'm using is app_path('Models/*'),

Upgrade required nikic/php-parser package to ^3.0

Hi Freek

Not really an issue or a bug, but would it be possible to switch to version ^3.0 of the required nikic/php-parser package instead of the current ^2.0 version?

Downgrading to v2.1.1 works fine for now.

Thanks for all the great packages!

Look for all models to clean up recursively within a directory

My models are located within their own subfolders, and right now there is no way to recursively search within one or multiple directories to clean up the models.

Would it be possible to add the functionality to search recursively within the specified directories in the config so you don't have to think of adding every new model to the array?

Thanks in advance!

olderThan() not working within scope()

I have the next code on my model

public function cleanUp(CleanupConfig $config): void
    {
        $config->olderThan(now()->subMonth())
            ->scope(function($query) {
                $query->where('causer_type', 'like', '%SystemReport%')->orWhereNull('causer_type');
            });
    }

But when executing php artisan clean:models is removing all the rows that gets with the scope function without checking the created_at column.

It works if i change the code to this:

public function cleanUp(CleanupConfig $config): void
    {
        $config->scope(function($query) {
                $query->where('causer_type', 'like', '%SystemReport%')->orWhereNull('causer_type')->where('created_at', '<', now()->subMonth());
            });
    }

Is this normal behavior? I am using the last version available

BUG / QUESTION InvalidArgumentException matching PHP version on composer require

In raising this issue, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked the pull requests tab for existing solutions/implementations to my issue/suggestion.
  • I have checked that the bug-fix I am reporting can be replicated.

Description of the problem

I am on a server with PHP 5.6.10.0
When I run: composer require spatie/laravel-model-cleanup it yields the error:

[InvalidArgumentException]
Could not find package spatie/laravel-model-cleanup at any version matching your PHP version 5.6.10.0

How come I cannot install this package?
What can I do to fix this? And where does it say what php version is required for this package?

Note: Composer works just fine in any other case (just pulled in your laravel-backup without problem ;) )

Can not pass with PHP Static Analysis Tool

Laravel v5.8.35

  82     Call to an undefined method Illuminate\Database\Eloquent\Builder::onlyTrashed().

the code is

    public static function cleanUp(Builder $query): Builder
    {
        return $query->onlyTrashed()->where('deleted_at', '<', Carbon::now()->subMonth());
    }

ref link larastan/larastan#40

[PROPOSAL] Force delete on models

In raising this issue, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked the pull requests tab for existing solutions/implementations to my issue/suggestion.
  • I have checked that the bug-fix I am reporting can be replicated.

Description of the proposal

Extend CleanUpModelsCommand to allow perform forceDelete for models. I see several ways to do it:

  1. Defined what models should use forceDelete on cleanUp in config file.
  2. Define model property\method which will mean that forceDelete must be used.
  3. Define it in console command optional attribute (not so flexible solution).
  4. One more complex idea is make something like cleanUpStrategies and garbage collector will choose which one should be used right now. For example every week common delete will be performed for model Document with draft state, but monthly all soft deleted Document records of draft state from previous month should be force deleted. Each model should have own strategies, described as methods.

That's proposal was introduced in #2 but I've described all the ways to implement it, and hope you will change your mind in a future and extend functionality.

unable to locate the CleanupConfig class

Hi
I am unable to locate the CleanupConfig class mentionned in the readme

public function cleanUp(CleanupConfig $config): void
     {
         $config->olderThanDays(5);
     }

phpstorm also doesnt find it.
Thank you for your help

Fire event for each cleaned model

[PROPOSAL]

Sometimes there is the need to perform some operation with data deriving from models cleanup process (e.g. sending a slack notification informing how many model records were deleted, update a statistic table, etc).
I'm thinking to add a specific event, fired whenever the quantity of deleted records is greater than zero.
The event will inform the listener with following data:

  • cleaned model class name
  • deleted records quantity

What do you think about?
@freekmurze If you agree, I'll start working on a PR.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.