-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command for cart pruning (#1303)
This PR adds a command `lunar:prune:carts` and some new config params under `lunar.cart.prune_tables` to determine how pruning should be carried out. I opted for using a pipeline for amending the query, as we ultimately always seem to end up there. I've tried to default to some sensible settings (90 days, dont prune carts with orders associated). If pruning is enabled we automatically add a daily task rather then needing the developer to do it. --------- Co-authored-by: Glenn Jacobs <glenn@neondigital.co.uk>
- Loading branch information
1 parent
ad25097
commit a243a91
Showing
7 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Lunar\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Pipeline\Pipeline; | ||
use Lunar\Models\Cart; | ||
|
||
class PruneCarts extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'lunar:prune:carts'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Prune the carts table'; | ||
|
||
/** | ||
* Execute the console 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(); | ||
}); | ||
}); | ||
}); | ||
|
||
$this->info('Prune complete'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Lunar\Pipelines\CartPrune; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
final class PruneAfter | ||
{ | ||
public function handle(Builder $query, Closure $next) | ||
{ | ||
$days = config('lunar.cart.prune_tables.prune_after', 90); | ||
|
||
$query->where('updated_at', '<=', now()->subDays($days)) | ||
->whereDoesntHave('lines', function ($query) use ($days) { | ||
$query->where('updated_at', '>', now()->subDays($days)); | ||
}); | ||
|
||
return $next($query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 WithoutOrders | ||
{ | ||
public function handle(Builder $query, Closure $next) | ||
{ | ||
$query->whereDoesntHave('orders'); | ||
|
||
return $next($query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Lunar\Tests\Unit\Console; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Carbon; | ||
use Lunar\Models\Cart; | ||
use Lunar\Models\Channel; | ||
use Lunar\Models\Currency; | ||
use Lunar\Tests\TestCase; | ||
|
||
/** | ||
* @group commands | ||
*/ | ||
class CartPruneTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** @test */ | ||
public function can_prune_carts_with_default_settings() | ||
{ | ||
$currency = Currency::factory()->create(); | ||
$channel = Channel::factory()->create(); | ||
|
||
$cart = Cart::create([ | ||
'currency_id' => $currency->id, | ||
'channel_id' => $channel->id, | ||
'meta' => ['foo' => 'bar'], | ||
'updated_at' => Carbon::now()->subDay(120), | ||
]); | ||
|
||
$cart = Cart::create([ | ||
'currency_id' => $currency->id, | ||
'channel_id' => $channel->id, | ||
'meta' => ['foo' => 'bar'], | ||
'updated_at' => Carbon::now()->subDay(20), | ||
]); | ||
|
||
$this->assertCount(2, Cart::query()->get()); | ||
|
||
$this->artisan('lunar:prune:carts'); | ||
|
||
$this->assertCount(1, Cart::query()->get()); | ||
} | ||
} |