From 7ff860fecaed32273b5a14dafcfb40e11e6ddf79 Mon Sep 17 00:00:00 2001 From: F4ilji Date: Fri, 18 Sep 2026 02:54:42 +0500 Subject: [PATCH] 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. --- app/Ship/Providers/EventServiceProvider.php | 9 ++++++++- routes/console.php | 7 ------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/Ship/Providers/EventServiceProvider.php b/app/Ship/Providers/EventServiceProvider.php index 9348a1c..8175f78 100755 --- a/app/Ship/Providers/EventServiceProvider.php +++ b/app/Ship/Providers/EventServiceProvider.php @@ -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."); + } + }); + } } /** diff --git a/routes/console.php b/routes/console.php index a948dd0..e05f4c9 100755 --- a/routes/console.php +++ b/routes/console.php @@ -1,7 +1,6 @@ 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.'); -}