-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d910bc8
commit f6511d4
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/Illuminate/Database/Console/Migrations/FreshCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters