Skip to content

Commit

Permalink
add migrate:fresh command
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 5, 2017
1 parent d910bc8 commit f6511d4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/Illuminate/Database/Console/Migrations/FreshCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Illuminate\Database\Console\Migrations;

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Input\InputOption;

class FreshCommand extends Command
{
use ConfirmableTrait;

/**
* The console command name.
*
* @var string
*/
protected $name = 'migrate:fresh';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Drop all tables and re-run all migrations';

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
if (! $this->confirmToProceed()) {
return;
}

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

$this->call('migrate', [
'--database' => $database,
'--path' => $this->input->getOption('path'),
'--force' => $this->input->getOption('force'),
]);

if ($this->needsSeeding()) {
$this->runSeeder($database);
}
}

/**
* Drop all of the database tables.
*
* @param string $database
* @return void
*/
protected function dropAllTables($database)
{
$this->laravel['db']->connection($database)
->getSchemaBuilder()
->dropAllTables();
}

/**
* Determine if the developer has requested database seeding.
*
* @return bool
*/
protected function needsSeeding()
{
return $this->option('seed') || $this->option('seeder');
}

/**
* Run the database seeder command.
*
* @param string $database
* @return void
*/
protected function runSeeder($database)
{
$this->call('db:seed', [
'--database' => $database,
'--class' => $this->option('seeder') ?: 'DatabaseSeeder',
'--force' => $this->option('force'),
]);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],

['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],

['path', null, InputOption::VALUE_OPTIONAL, 'The path of migrations files to be executed.'],

['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],

['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder.'],
];
}
}
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand;
use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand;
use Illuminate\Database\Console\Migrations\ResetCommand as MigrateResetCommand;
use Illuminate\Database\Console\Migrations\FreshCommand as MigrateFreshCommand;
use Illuminate\Database\Console\Migrations\StatusCommand as MigrateStatusCommand;
use Illuminate\Database\Console\Migrations\InstallCommand as MigrateInstallCommand;
use Illuminate\Database\Console\Migrations\RefreshCommand as MigrateRefreshCommand;
Expand Down Expand Up @@ -86,6 +87,7 @@ class ArtisanServiceProvider extends ServiceProvider
'Environment' => 'command.environment',
'KeyGenerate' => 'command.key.generate',
'Migrate' => 'command.migrate',
'MigrateFresh' => 'command.migrate.fresh',
'MigrateInstall' => 'command.migrate.install',
'MigrateRefresh' => 'command.migrate.refresh',
'MigrateReset' => 'command.migrate.reset',
Expand Down Expand Up @@ -422,6 +424,18 @@ protected function registerMigrateCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerMigrateFreshCommand()
{
$this->app->singleton('command.migrate.fresh', function () {
return new MigrateFreshCommand;
});
}

/**
* Register the command.
*
Expand Down

0 comments on commit f6511d4

Please sign in to comment.