Skip to content

Commit c04701c

Browse files
authored
[Feature] Added ability to truncate the results table (alexjustesen#1362)
1 parent 9e53492 commit c04701c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

app/Filament/Resources/ResultResource.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Filament\Resources\ResultResource\Pages;
99
use App\Helpers\Number;
1010
use App\Helpers\TimeZoneHelper;
11+
use App\Jobs\TruncateResults;
1112
use App\Models\Result;
1213
use App\Settings\DataMigrationSettings;
1314
use App\Settings\GeneralSettings;
@@ -316,6 +317,16 @@ public static function table(Table $table): Table
316317
->modalHeading('Migrate History')
317318
->modalDescription(new HtmlString('<p>v0.16.0 archived the old <code>"results"</code> table, to migrate your history click the button below.</p><p>For more information read the <a href="#" target="_blank" rel="nofollow" class="underline">docs</a>.</p>'))
318319
->modalSubmitActionLabel('Yes, migrate it'),
320+
Tables\Actions\ActionGroup::make([
321+
Tables\Actions\Action::make('truncate')
322+
->action(fn () => TruncateResults::dispatch(Auth::user()))
323+
->requiresConfirmation()
324+
->modalHeading('Truncate Results')
325+
->modalDescription('Are you sure you want to truncate all results data? This can\'t be undone.')
326+
->color('danger')
327+
->icon('heroicon-o-trash')
328+
->hidden(fn (): bool => ! Auth::user()->is_admin),
329+
])->dropdownPlacement('bottom-end'),
319330
])
320331
->defaultSort('created_at', 'desc')
321332
->paginated([5, 15, 25, 50, 100])

app/Jobs/TruncateResults.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\User;
6+
use Filament\Notifications\Notification;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
use Illuminate\Support\Facades\DB;
13+
14+
class TruncateResults implements ShouldQueue
15+
{
16+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
/**
19+
* The number of times the job may be attempted.
20+
*
21+
* @var int
22+
*/
23+
public $tries = 1;
24+
25+
public function __construct(
26+
public User $user,
27+
) {
28+
}
29+
30+
/**
31+
* Execute the job.
32+
*/
33+
public function handle(): void
34+
{
35+
try {
36+
DB::table('results')->truncate();
37+
} catch (\Throwable $th) {
38+
$this->fail($th);
39+
40+
return;
41+
}
42+
43+
Notification::make()
44+
->title('Results table truncated!')
45+
->success()
46+
->sendToDatabase($this->user);
47+
}
48+
}

0 commit comments

Comments
 (0)