-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Adds the possibility of having "Prunable" models #37889
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nunomaduro this looks fantastic and will clean some codebases out there (mine included)!
Just a few suggestions on wording and with some typos also fixed (that i could find at least).
…mework into feat/prunable-models
Nice! Would it be possible to make the models directory configurable? |
Great work on this Nuno. |
That is awesome Nuno |
Good one Nuno! 🚀 |
The only one thing I don't like - console command name. It looks like we are pruning database, not models. Eloquent allows us to store records anywhere, even in filesystem. What about |
I believe the name changing for model prune suits better for proposed.
…On Fri, Jul 2, 2021, 21:44 Anton Komarev ***@***.***> wrote:
The only one thing I don't like - console command name. It looks like we
are pruning database, not models. Eloquent allows us to store records
anywhere, even in filesystem. What about artisan model:prune?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#37889 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAI3QQZJ3LP2I7MSYQGVDHTTVYQLVANCNFSM47WVKTHA>
.
|
It's a feature I've implemented manually in many applications. I think it would be great to have it built into the core. |
@antonkomarev You could argue the same about |
db:seed command can be plain SQLs, it's standalone feature. db:prune can't, it's model related feature. |
I seem to be in the minority here, but this feels more appropriate as a package, not something that needs to be in the framework. It looks great, but I don’t mind a quick |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚢 🚀
Why not both? Command wide option changes the default value globally and models wide option changes the value only for this model. When both are provided the chunk size is either the model specified value if any either the command specified one if not. That allows having a custom global default chuck size and also customizing it model by model. |
Pretty handy! |
Renamed command to |
Added configurable chunk size (on command level and model level). |
Looking at the implementation, I am wondering why there is a |
Nice work! I suggest clarifying that Laravel 8.5 is required. |
Custom implementation of MassPrunable is required to prevent using the limit. #2591 added an exception when limited is used because MongoDB Delete operation doesn't support it. - MassPrunable::pruneAll() is called by the command model:prune. - Using the parent trait is required because it's used to detect prunable models. - Prunable feature was introducted in Laravel 8.x by laravel/framework#37889. Users have to be aware that MassPrunable can break relationships as it doesn't call model methods to remove links.
Custom implementation of MassPrunable is required to prevent using the limit. #2591 added an exception when limited is used because MongoDB Delete operation doesn't support it. - MassPrunable::pruneAll() is called by the command model:prune. - Using the parent trait is required because it's used to detect prunable models. - Prunable feature was introducted in Laravel 8.x by laravel/framework#37889. Users have to be aware that MassPrunable can break relationships as it doesn't call model methods to remove links.
This pull request adds a convenient way to remove obsolete model records by adding the possibility of making Laravel models prunable. When making models prunable, Laravel will automatically remove obsolete model records from the database via a scheduled command.
Usage
To get started, add the
Illuminate\Database\Eloquent\Prunable
orIlluminate\Database\Eloquent\MassPrunable
trait to the model you would like to make prunable.Next, implement a
prunable
method to determine the prunable query:Finally, schedule the
db:prune
artisan command in yourApp\Console\Kernel
class:Behind the scenes, the
db:prune
artisan command will automatically detectPrunable
models in theapp/models
folder. Yet, if your models are in a different location, you may use the--model
artisan option to specify the class names:Soft Deleted Models
If your Eloquent model is
Prunable
orMassPrunable
, and usesSoftDeletes
, all soft deleted models are automatically be included on the prunable query. Meaning that if you return a prunable query on theprunable()
method that deleted Posts that are 30 days old, soft deleted posts that are 30 days old will be automatically deleted as well. This behavior, regarding automatically deleting soft deleted models, can not (and should not) be modified.Configuring Prune Method
When using the
Illuminate\Database\Eloquent\Prunable
trait, aprune()
method will be called on each model. By default, this method will call the$this->delete()
or the$this->forceDelete()
- when using theSoftDeletes
trait - methods. If you would like to customize theprune
method, you may override it on the model itself:Open questions
db:prune
ormodel:prune
?