| @@ 11-116 (lines=106) @@ | ||
| 8 | use Symfony\Component\Console\Input\InputArgument; |
|
| 9 | use Symfony\Component\Console\Input\InputOption; |
|
| 10 | ||
| 11 | class MigrateResetCommand extends Command |
|
| 12 | { |
|
| 13 | use MigrationLoaderTrait; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * The console command name. |
|
| 17 | * |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | protected $name = 'module:migrate-reset'; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * The console command description. |
|
| 24 | * |
|
| 25 | * @var string |
|
| 26 | */ |
|
| 27 | protected $description = 'Reset the modules migrations.'; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var \Nwidart\Modules\Repository |
|
| 31 | */ |
|
| 32 | protected $module; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Execute the console command. |
|
| 36 | * |
|
| 37 | * @return mixed |
|
| 38 | */ |
|
| 39 | public function fire() |
|
| 40 | { |
|
| 41 | $this->module = $this->laravel['modules']; |
|
| 42 | ||
| 43 | $name = $this->argument('module'); |
|
| 44 | ||
| 45 | if (!empty($name)) { |
|
| 46 | $this->reset($name); |
|
| 47 | ||
| 48 | return; |
|
| 49 | } |
|
| 50 | ||
| 51 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { |
|
| 52 | $this->line('Running for module: <info>' . $module->getName() . '</info>'); |
|
| 53 | ||
| 54 | $this->reset($module); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * Rollback migration from the specified module. |
|
| 60 | * |
|
| 61 | * @param $module |
|
| 62 | */ |
|
| 63 | public function reset($module) |
|
| 64 | { |
|
| 65 | if (is_string($module)) { |
|
| 66 | $module = $this->module->findOrFail($module); |
|
| 67 | } |
|
| 68 | ||
| 69 | $migrator = new Migrator($module); |
|
| 70 | ||
| 71 | $database = $this->option('database'); |
|
| 72 | ||
| 73 | if (!empty($database)) { |
|
| 74 | $migrator->setDatabase($database); |
|
| 75 | } |
|
| 76 | ||
| 77 | $migrated = $migrator->reset(); |
|
| 78 | ||
| 79 | if (count($migrated)) { |
|
| 80 | foreach ($migrated as $migration) { |
|
| 81 | $this->line("Rollback: <info>{$migration}</info>"); |
|
| 82 | } |
|
| 83 | ||
| 84 | return; |
|
| 85 | } |
|
| 86 | ||
| 87 | $this->comment('Nothing to rollback.'); |
|
| 88 | } |
|
| 89 | ||
| 90 | /** |
|
| 91 | * Get the console command arguments. |
|
| 92 | * |
|
| 93 | * @return array |
|
| 94 | */ |
|
| 95 | protected function getArguments() |
|
| 96 | { |
|
| 97 | return array( |
|
| 98 | array('module', InputArgument::OPTIONAL, 'The name of module will be used.'), |
|
| 99 | ); |
|
| 100 | } |
|
| 101 | ||
| 102 | /** |
|
| 103 | * Get the console command options. |
|
| 104 | * |
|
| 105 | * @return array |
|
| 106 | */ |
|
| 107 | protected function getOptions() |
|
| 108 | { |
|
| 109 | return array( |
|
| 110 | array('direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'), |
|
| 111 | array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'), |
|
| 112 | array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'), |
|
| 113 | array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'), |
|
| 114 | ); |
|
| 115 | } |
|
| 116 | } |
|
| 117 | ||
| @@ 11-116 (lines=106) @@ | ||
| 8 | use Symfony\Component\Console\Input\InputArgument; |
|
| 9 | use Symfony\Component\Console\Input\InputOption; |
|
| 10 | ||
| 11 | class MigrateRollbackCommand extends Command |
|
| 12 | { |
|
| 13 | use MigrationLoaderTrait; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * The console command name. |
|
| 17 | * |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | protected $name = 'module:migrate-rollback'; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * The console command description. |
|
| 24 | * |
|
| 25 | * @var string |
|
| 26 | */ |
|
| 27 | protected $description = 'Rollback the modules migrations.'; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var \Nwidart\Modules\Repository |
|
| 31 | */ |
|
| 32 | protected $module; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Execute the console command. |
|
| 36 | * |
|
| 37 | * @return mixed |
|
| 38 | */ |
|
| 39 | public function fire() |
|
| 40 | { |
|
| 41 | $this->module = $this->laravel['modules']; |
|
| 42 | ||
| 43 | $name = $this->argument('module'); |
|
| 44 | ||
| 45 | if (!empty($name)) { |
|
| 46 | $this->rollback($name); |
|
| 47 | ||
| 48 | return; |
|
| 49 | } |
|
| 50 | ||
| 51 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { |
|
| 52 | $this->line('Running for module: <info>' . $module->getName() . '</info>'); |
|
| 53 | ||
| 54 | $this->rollback($module); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * Rollback migration from the specified module. |
|
| 60 | * |
|
| 61 | * @param $module |
|
| 62 | */ |
|
| 63 | public function rollback($module) |
|
| 64 | { |
|
| 65 | if (is_string($module)) { |
|
| 66 | $module = $this->module->findOrFail($module); |
|
| 67 | } |
|
| 68 | ||
| 69 | $migrator = new Migrator($module); |
|
| 70 | ||
| 71 | $database = $this->option('database'); |
|
| 72 | ||
| 73 | if (!empty($database)) { |
|
| 74 | $migrator->setDatabase($database); |
|
| 75 | } |
|
| 76 | ||
| 77 | $migrated = $migrator->rollback(); |
|
| 78 | ||
| 79 | if (count($migrated)) { |
|
| 80 | foreach ($migrated as $migration) { |
|
| 81 | $this->line("Rollback: <info>{$migration}</info>"); |
|
| 82 | } |
|
| 83 | ||
| 84 | return; |
|
| 85 | } |
|
| 86 | ||
| 87 | $this->comment('Nothing to rollback.'); |
|
| 88 | } |
|
| 89 | ||
| 90 | /** |
|
| 91 | * Get the console command arguments. |
|
| 92 | * |
|
| 93 | * @return array |
|
| 94 | */ |
|
| 95 | protected function getArguments() |
|
| 96 | { |
|
| 97 | return array( |
|
| 98 | array('module', InputArgument::OPTIONAL, 'The name of module will be used.'), |
|
| 99 | ); |
|
| 100 | } |
|
| 101 | ||
| 102 | /** |
|
| 103 | * Get the console command options. |
|
| 104 | * |
|
| 105 | * @return array |
|
| 106 | */ |
|
| 107 | protected function getOptions() |
|
| 108 | { |
|
| 109 | return array( |
|
| 110 | array('direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'), |
|
| 111 | array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'), |
|
| 112 | array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'), |
|
| 113 | array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'), |
|
| 114 | ); |
|
| 115 | } |
|
| 116 | } |
|
| 117 | ||