Skip to content

Commit

Permalink
[0.8] Fix some pruning issues (lunarphp#1537)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Feb 9, 2024
1 parent 1c75826 commit 68398b1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
13 changes: 7 additions & 6 deletions packages/core/config/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,27 @@
'lines.purchasable.product',
'lines.cart.currency',
],

/*
|--------------------------------------------------------------------------
| Prune carts
|--------------------------------------------------------------------------
|
| Should the cart models be pruned to prevent data build up and
| Should the cart models be pruned to prevent data build up and
| some settings controlling how pruning should be determined
|
*/
'prune_tables' => [

'enabled' => false,

'pipelines' => [
Lunar\Pipelines\CartPrune\PruneAfter::class,
Lunar\Pipelines\CartPrune\WithoutOrders::class,
Lunar\Pipelines\CartPrune\WhereNotMerged::class,
],

'prune_interval' => 90, // days

],
];
25 changes: 14 additions & 11 deletions packages/core/src/Console/Commands/PruneCarts.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,30 @@ class PruneCarts extends Command
* @return void
*/
public function handle()
{
{
$this->info('Beginning prune');

$query = Cart::query();

$carts = app(Pipeline::class)
->send($query)
->through(
config('lunar.cart.prune_tables.pipelines', [])
)->then(function ($query) {
$query->chunk(200, function ($carts) {
$carts->each(function ($cart) {
Cart::where('merged_id', $cart->id)->update(['merged_id' => null]);

$cart->lines()->delete();
$cart->addresses()->delete();
$cart->delete();
});
$carts->each(fn ($cart) => $this->pruneCart($cart));
});
});

$this->info('Prune complete');
}

public function pruneCart(Cart $cart)
{
Cart::where('merged_id', $cart->id)->get()->each(fn ($merged) => $this->pruneCart($merged));

$cart->lines()->delete();
$cart->addresses()->delete();
$cart->delete();
}
}
16 changes: 16 additions & 0 deletions packages/core/src/Pipelines/CartPrune/WhereNotMerged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Lunar\Pipelines\CartPrune;

use Closure;
use Illuminate\Database\Eloquent\Builder;

final class WhereNotMerged
{
public function handle(Builder $query, Closure $next)
{
$query->unmerged();

return $next($query);
}
}

0 comments on commit 68398b1

Please sign in to comment.