forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVacuumDatabase.php
More file actions
42 lines (32 loc) · 1.1 KB
/
VacuumDatabase.php
File metadata and controls
42 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace App\Actions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction;
class VacuumDatabase
{
use AsAction;
/**
* Reclaim unused pages and refresh query planner stats on SQLite databases.
* No-op for other drivers, which handle this internally.
*/
public function handle(): void
{
$connection = DB::connection();
if ($connection->getDriverName() !== 'sqlite') {
return;
}
// VACUUM cannot run inside a transaction. Bail out rather than fail hard
// if one is somehow active (e.g. tests wrapped in RefreshDatabase).
if ($connection->transactionLevel() > 0) {
Log::warning('Skipping SQLite maintenance: active transaction detected.');
return;
}
$start = microtime(true);
$connection->statement('PRAGMA optimize;');
$connection->statement('VACUUM;');
Log::info('SQLite maintenance completed', [
'duration_ms' => round((microtime(true) - $start) * 1000, 2),
]);
}
}