Skip to content

Commit

Permalink
[11.x] Do not wipe database if it does not exists (#50838)
Browse files Browse the repository at this point in the history
* Do not wipe database if it does not exists

* wip
  • Loading branch information
driesvints authored Mar 29, 2024
1 parent 7fc7742 commit 375859f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
37 changes: 30 additions & 7 deletions Console/Migrations/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\DatabaseRefreshed;
use Illuminate\Database\Migrations\Migrator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -28,6 +29,26 @@ class FreshCommand extends Command
*/
protected $description = 'Drop all tables and re-run all migrations';

/**
* The migrator instance.
*
* @var \Illuminate\Database\Migrations\Migrator
*/
protected $migrator;

/**
* Create a new fresh command instance.
*
* @param \Illuminate\Database\Migrations\Migrator $migrator
* @return void
*/
public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* Execute the console command.
*
Expand All @@ -41,14 +62,16 @@ public function handle()

$database = $this->input->getOption('database');

$this->newLine();
if ($this->migrator->repositoryExists()) {
$this->newLine();

$this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
'--database' => $database,
'--drop-views' => $this->option('drop-views'),
'--drop-types' => $this->option('drop-types'),
'--force' => true,
])) == 0);
$this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
'--database' => $database,
'--drop-views' => $this->option('drop-views'),
'--drop-types' => $this->option('drop-types'),
'--force' => true,
])) == 0);
}

$this->newLine();

Expand Down
4 changes: 3 additions & 1 deletion MigrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ protected function registerMigrateCommand()
*/
protected function registerMigrateFreshCommand()
{
$this->app->singleton(FreshCommand::class);
$this->app->singleton(FreshCommand::class, function ($app) {
return new FreshCommand($app['migrator']);
});
}

/**
Expand Down

0 comments on commit 375859f

Please sign in to comment.