feat: block destructive DB commands in production

Listen for CommandStarting event and throw RuntimeException for
migrate:fresh, migrate:refresh, migrate:reset, db:wipe, db:seed
when APP_ENV=production. Prevents accidental data loss even with --force.

Also removes old non-functional closure-based override in routes/console.php.
This commit is contained in:
F4ilji
2026-09-18 02:54:42 +05:00
parent 4a776a42e2
commit 7ff860feca
2 changed files with 8 additions and 8 deletions
+8 -1
View File
@@ -27,7 +27,14 @@ class EventServiceProvider extends AbstractEventServiceProvider
*/
public function boot()
{
//
if (app()->environment('production')) {
Event::listen(\Illuminate\Console\Events\CommandStarting::class, function ($event) {
$blocked = ['migrate:fresh', 'migrate:refresh', 'migrate:reset', 'db:wipe', 'db:seed'];
if (in_array($event->command, $blocked)) {
throw new \RuntimeException("BLOCKED: {$event->command} is disabled in production.");
}
});
}
}
/**
-7
View File
@@ -1,7 +1,6 @@
<?php
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
/*
@@ -18,9 +17,3 @@ use Illuminate\Support\Facades\Artisan;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');
if ('production' === App::environment()) {
Artisan::command('migrate:fresh', function () {
$this->comment('You are not allowed to do this in production.');
})->describe('Override default command in production.');
}