Skip to content

Commit 4b310d0

Browse files
authored
Merge pull request #26 from me-shaon/schedule-data-purning
Scheduled task for pruning old data
2 parents ed93e7d + 004e9a3 commit 4b310d0

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,57 @@ return [
5252
'ignore-paths' => [
5353

5454
],
55+
56+
'pruning' => [
57+
'enabled' => env('REQUEST_ANALYTICS_PRUNING_ENABLED', true),
58+
'days' => env('REQUEST_ANALYTICS_PRUNING_DAYS', 90),
59+
],
5560
];
5661
```
62+
### Data Purning
63+
You can delete your data from your database automatically.
64+
65+
If you are using Laravel 11+ then you may use `model:prune` command.
66+
Add this to your `routes/console.php`
67+
68+
```php
69+
use Illuminate\Support\Facades\Schedule;
70+
71+
Schedule::command('model:prune', [
72+
'--model' => 'MeShaon\RequestAnalytics\Models\RequestAnalytics',
73+
])->daily();
74+
```
75+
Or try this `bootstarp/app.php`
76+
```php
77+
use Illuminate\Console\Scheduling\Schedule;
78+
->withSchedule(function (Schedule $schedule) {
79+
$schedule->command('model:prune', [
80+
'--model' => 'MeShaon\RequestAnalytics\Models\RequestAnalytics',
81+
])->daily();
82+
})
83+
```
84+
85+
If you are using Laravel 10 or below then you may use `model:prune` command.
86+
You may define all of your scheduled tasks in the schedule method of your application's `App\Console\Kernel` class
87+
```php
88+
<?php
89+
90+
namespace App\Console;
91+
92+
use Illuminate\Console\Scheduling\Schedule;
93+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
94+
95+
class Kernel extends ConsoleKernel
96+
{
97+
protected function schedule(Schedule $schedule): void
98+
{
99+
$schedule->command('model:prune', [
100+
'--model' => 'MeShaon\RequestAnalytics\Models\RequestAnalytics',
101+
])->daily();
102+
}
103+
}
104+
```
105+
57106
You can publish the assets with this command:
58107
```bash
59108
php artisan vendor:publish --tag="request-analytics-assets"

config/request-analytics.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
'ignore-paths' => [
1919

2020
],
21+
22+
'pruning' => [
23+
'enabled' => env('REQUEST_ANALYTICS_PRUNING_ENABLED', true),
24+
'days' => env('REQUEST_ANALYTICS_PRUNING_DAYS', 90),
25+
],
2126
];

src/Models/RequestAnalytics.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,33 @@
22

33
namespace MeShaon\RequestAnalytics\Models;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\MassPrunable;
68
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Carbon;
710

811
class RequestAnalytics extends Model
912
{
10-
use HasFactory;
13+
use HasFactory, MassPrunable;
1114

1215
public const UPDATED_AT = null;
1316

1417
public const CREATED_AT = null;
1518

1619
protected $guarded = ['id', 'created_at', 'updated_at'];
20+
21+
/**
22+
* Get the prunable model query.
23+
*/
24+
public function prunable(): Builder
25+
{
26+
if (! config('request-analytics.pruning.enabled', false)) {
27+
return $this->whereRaw('1 = 0');
28+
}
29+
30+
$days = config('request-analytics.pruning.days', 90);
31+
32+
return static::where('visited_at', '<=', Carbon::now()->subDays($days));
33+
}
1734
}

0 commit comments

Comments
 (0)