Skip to content

Commit 9c5b55e

Browse files
committed
Reworks things
1 parent d122058 commit 9c5b55e

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

database/migrations/optimize_database_settings.php.stub

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ return new class extends Migration
1010
*/
1111
public function up(): void
1212
{
13-
if (config('database.default') === 'sqlite') {
14-
DB::statement('PRAGMA journal_mode = WAL');
15-
DB::statement('PRAGMA synchronous = NORMAL');
16-
DB::statement('PRAGMA page_size = 32768;');
17-
DB::statement('PRAGMA cache_size = -20000;');
18-
DB::statement('PRAGMA auto_vacuum = incremental;');
19-
DB::statement('PRAGMA foreign_keys = ON;');
13+
if (DB::getDriverName() === 'sqlite') {
14+
DB::unprepared(<<<SQL
15+
PRAGMA auto_vacuum = incremental;
16+
PRAGMA journal_mode = WAL;
17+
PRAGMA page_size = 32768;
18+
SQL
19+
);
2020
}
2121
}
2222
};

src/Commands/DbOptimizeCommand.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace NunoMaduro\LaravelOptimizeDatabase\Commands;
66

77
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\DB;
89
use NunoMaduro\LaravelOptimizeDatabase\LaravelOptimizeDatabaseServiceProvider;
10+
use function Laravel\Prompts\table;
911

1012
/**
1113
* @internal
@@ -31,13 +33,32 @@ public function handle(): void
3133
'--provider' => LaravelOptimizeDatabaseServiceProvider::class,
3234
]);
3335

34-
if (config('database.default') !== 'sqlite') {
36+
37+
if (DB::getDriverName() !== 'sqlite') {
3538
$this->error('Optimization is only available for SQLite databases.');
3639

3740
return;
3841
}
3942

40-
$this->components->warn('Please validate the migration before running it in production. It may contain settings that are not compatible with your application.');
41-
$this->components->info('Once validated, you may run [php artisan migrate] to apply the migration.');
43+
$this->components->info('The following settings will be applied to your SQLite database:');
44+
45+
table([
46+
'Setting',
47+
'Value',
48+
'Via'
49+
], [
50+
['PRAGMA auto_vacuum', 'incremental', 'Migration'],
51+
['PRAGMA journal_mode', 'WAL', 'Migration'],
52+
['PRAGMA page_size', '32768', 'Migration'],
53+
['PRAGMA busy_timeout', '5000', 'Runtime'],
54+
['PRAGMA cache_size', '-20000', 'Runtime'],
55+
['PRAGMA foreign_keys', 'ON', 'Runtime'],
56+
['PRAGMA incremental_vacuum', '', 'Runtime'],
57+
['PRAGMA mmap_size', '2147483648', 'Runtime'],
58+
['PRAGMA temp_store', 'MEMORY', 'Runtime'],
59+
['PRAGMA synchronous', 'NORMAL', 'Runtime'],
60+
]);
61+
62+
$this->components->info('While [Runtime] settings are applied automatically, [Migration] settings will only be applied after running [php artisan migrate].');
4263
}
4364
}

src/LaravelOptimizeDatabaseServiceProvider.php

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace NunoMaduro\LaravelOptimizeDatabase;
66

7+
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\ServiceProvider;
89
use NunoMaduro\LaravelOptimizeDatabase\Commands\DbOptimizeCommand;
910

@@ -31,6 +32,19 @@ private function registerCommands(): void
3132
DbOptimizeCommand::class,
3233
]);
3334
}
35+
36+
if (DB::getDriverName() === 'sqlite') {
37+
DB::unprepared(<<<SQL
38+
PRAGMA busy_timeout = 5000;
39+
PRAGMA cache_size = -20000;
40+
PRAGMA foreign_keys = ON;
41+
PRAGMA incremental_vacuum;
42+
PRAGMA mmap_size = 2147483648;
43+
PRAGMA temp_store = MEMORY;
44+
PRAGMA synchronous = NORMAL;
45+
SQL,
46+
);
47+
}
3448
}
3549

3650
/**

0 commit comments

Comments
 (0)