Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Do not wipe database if it does not exists #50838

Merged
merged 2 commits into from Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/Illuminate/Database/Console/Migrations/FreshCommand.php
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 src/Illuminate/Database/MigrationServiceProvider.php
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