Coder Social home page Coder Social logo

spatie / laravel-model-cleanup Goto Github PK

View Code? Open in Web Editor NEW
399.0 12.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 Issues

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 ;) )

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/*'),

[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.

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

[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...

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

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

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

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!

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());
    }
路路路

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!

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

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.