Skip to content

Commit be2663c

Browse files
authored
Prunable Models (laravel-workflow#182)
1 parent b60f98e commit be2663c

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/Models/StoredWorkflow.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Workflow\Models;
66

7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Prunable;
79
use Spatie\ModelStates\HasStates;
810
use Workflow\States\WorkflowStatus;
911
use Workflow\WorkflowStub;
@@ -14,6 +16,7 @@
1416
class StoredWorkflow extends Model
1517
{
1618
use HasStates;
19+
use Prunable;
1720

1821
/**
1922
* @var string
@@ -78,4 +81,39 @@ public function children(): \Illuminate\Database\Eloquent\Relations\BelongsToMan
7881
'child_workflow_id'
7982
)->withPivot(['parent_index', 'parent_now']);
8083
}
84+
85+
public function prunable(): Builder
86+
{
87+
return static::where('status', 'completed')
88+
->where('created_at', '<=', now()->sub(config('workflows.prune_age', '1 month')))
89+
->whereDoesntHave('parents');
90+
}
91+
92+
protected function pruning(): void
93+
{
94+
$this->recursivePrune($this);
95+
}
96+
97+
protected function recursivePrune(self $workflow): void
98+
{
99+
$workflow->children()
100+
->each(function ($child) {
101+
$this->recursivePrune($child);
102+
});
103+
104+
$workflow->parents()
105+
->detach();
106+
$workflow->exceptions()
107+
->delete();
108+
$workflow->logs()
109+
->delete();
110+
$workflow->signals()
111+
->delete();
112+
$workflow->timers()
113+
->delete();
114+
115+
if ($workflow->id !== $this->id) {
116+
$workflow->delete();
117+
}
118+
}
81119
}

src/config/workflows.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
'workflow_relationships_table' => 'workflow_relationships',
2121

22+
'prune_age' => '1 month',
23+
2224
'monitor' => env('WORKFLOW_MONITOR', false),
2325

2426
'monitor_url' => env('WORKFLOW_MONITOR_URL'),

tests/Unit/Models/StoredWorkflowTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Unit\Models;
66

7+
use Illuminate\Support\Carbon;
78
use Tests\TestCase;
89
use Workflow\Models\StoredWorkflow;
910

@@ -13,6 +14,7 @@ public function testModel(): void
1314
{
1415
$workflow = StoredWorkflow::create([
1516
'class' => 'class',
17+
'status' => 'completed',
1618
]);
1719

1820
$workflow->exceptions()
@@ -39,9 +41,33 @@ public function testModel(): void
3941
'stop_at' => now(),
4042
]);
4143

44+
$workflow->children()
45+
->create([
46+
'class' => 'class',
47+
'status' => 'completed',
48+
], [
49+
'parent_index' => 0,
50+
'parent_now' => now(),
51+
]);
52+
4253
$this->assertSame(1, $workflow->exceptions()->count());
4354
$this->assertSame(1, $workflow->logs()->count());
4455
$this->assertSame(1, $workflow->signals()->count());
4556
$this->assertSame(1, $workflow->timers()->count());
57+
$this->assertSame(1, $workflow->children()->count());
58+
59+
Carbon::setTestNow(now()->addMonth()->addSecond());
60+
61+
$this->artisan('model:prune', [
62+
'--model' => 'Workflow\Models\StoredWorkflow',
63+
])
64+
->doesntExpectOutputToContain('No prunable models found.')
65+
->assertExitCode(0);
66+
67+
$this->assertSame(0, $workflow->exceptions()->count());
68+
$this->assertSame(0, $workflow->logs()->count());
69+
$this->assertSame(0, $workflow->signals()->count());
70+
$this->assertSame(0, $workflow->timers()->count());
71+
$this->assertSame(0, $workflow->children()->count());
4672
}
4773
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy