spatie / laravel-model-cleanup Goto Github PK
View Code? Open in Web Editor NEWClean up unneeded records
Home Page: https://freek.dev/410-a-laravel-package-to-clean-up-models
License: MIT License
Clean up unneeded records
Home Page: https://freek.dev/410-a-laravel-package-to-clean-up-models
License: MIT License
In raising this issue, I confirm the following (please check boxes):
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: 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/*'),
In raising this issue, I confirm the following (please check boxes):
Extend CleanUpModelsCommand
to allow perform forceDelete
for models. I see several ways to do it:
forceDelete
on cleanUp
in config file.forceDelete
must be used.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.
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
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...
[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:
What do you think about?
@freekmurze If you agree, I'll start working on a PR.
Thanks
It appears the forceCleanUp interface is no longer available so thought I ask :).
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
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
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!
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());
}
路路路
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!
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google 鉂わ笍 Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.