From f6511d477f73b3033ef2336257f4cac5f20594a0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 5 Apr 2017 10:36:27 -0500 Subject: [PATCH] add migrate:fresh command --- .../Console/Migrations/FreshCommand.php | 110 ++++++++++++++++++ .../Providers/ArtisanServiceProvider.php | 14 +++ 2 files changed, 124 insertions(+) create mode 100644 src/Illuminate/Database/Console/Migrations/FreshCommand.php diff --git a/src/Illuminate/Database/Console/Migrations/FreshCommand.php b/src/Illuminate/Database/Console/Migrations/FreshCommand.php new file mode 100644 index 000000000000..fc2ab8fa0f82 --- /dev/null +++ b/src/Illuminate/Database/Console/Migrations/FreshCommand.php @@ -0,0 +1,110 @@ +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.'], + ]; + } +} diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index 9cc46653fa87..2802b0f59e79 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -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; @@ -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', @@ -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. *