@@ 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\Contracts\RepositoryInterface |
|
31 | */ |
|
32 | protected $module; |
|
33 | ||
34 | /** |
|
35 | * Execute the console command. |
|
36 | */ |
|
37 | public function handle() |
|
38 | { |
|
39 | $this->module = $this->laravel['modules']; |
|
40 | ||
41 | $name = $this->argument('module'); |
|
42 | ||
43 | if (!empty($name)) { |
|
44 | $this->reset($name); |
|
45 | ||
46 | return; |
|
47 | } |
|
48 | ||
49 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { |
|
50 | $this->line('Running for module: <info>' . $module->getName() . '</info>'); |
|
51 | ||
52 | $this->reset($module); |
|
53 | } |
|
54 | } |
|
55 | ||
56 | /** |
|
57 | * Rollback migration from the specified module. |
|
58 | * |
|
59 | * @param $module |
|
60 | */ |
|
61 | public function reset($module) |
|
62 | { |
|
63 | if (is_string($module)) { |
|
64 | $module = $this->module->findOrFail($module); |
|
65 | } |
|
66 | ||
67 | $migrator = new Migrator($module); |
|
68 | ||
69 | $database = $this->option('database'); |
|
70 | ||
71 | if (!empty($database)) { |
|
72 | $migrator->setDatabase($database); |
|
73 | } |
|
74 | ||
75 | $migrated = $migrator->reset(); |
|
76 | ||
77 | if (count($migrated)) { |
|
78 | foreach ($migrated as $migration) { |
|
79 | $this->line("Rollback: <info>{$migration}</info>"); |
|
80 | } |
|
81 | ||
82 | return; |
|
83 | } |
|
84 | ||
85 | $this->comment('Nothing to rollback.'); |
|
86 | } |
|
87 | ||
88 | /** |
|
89 | * Get the console command arguments. |
|
90 | * |
|
91 | * @return array |
|
92 | */ |
|
93 | protected function getArguments() |
|
94 | { |
|
95 | return [ |
|
96 | ['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
|
97 | ]; |
|
98 | } |
|
99 | ||
100 | /** |
|
101 | * Get the console command options. |
|
102 | * |
|
103 | * @return array |
|
104 | */ |
|
105 | protected function getOptions() |
|
106 | { |
|
107 | return [ |
|
108 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'], |
|
109 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], |
|
110 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], |
|
111 | ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], |
|
112 | ]; |
|
113 | } |
|
114 | } |
|
115 |
@@ 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\Contracts\RepositoryInterface |
|
31 | */ |
|
32 | protected $module; |
|
33 | ||
34 | /** |
|
35 | * Execute the console command. |
|
36 | */ |
|
37 | public function handle() |
|
38 | { |
|
39 | $this->module = $this->laravel['modules']; |
|
40 | ||
41 | $name = $this->argument('module'); |
|
42 | ||
43 | if (!empty($name)) { |
|
44 | $this->rollback($name); |
|
45 | ||
46 | return; |
|
47 | } |
|
48 | ||
49 | foreach ($this->module->getOrdered($this->option('direction')) as $module) { |
|
50 | $this->line('Running for module: <info>' . $module->getName() . '</info>'); |
|
51 | ||
52 | $this->rollback($module); |
|
53 | } |
|
54 | } |
|
55 | ||
56 | /** |
|
57 | * Rollback migration from the specified module. |
|
58 | * |
|
59 | * @param $module |
|
60 | */ |
|
61 | public function rollback($module) |
|
62 | { |
|
63 | if (is_string($module)) { |
|
64 | $module = $this->module->findOrFail($module); |
|
65 | } |
|
66 | ||
67 | $migrator = new Migrator($module); |
|
68 | ||
69 | $database = $this->option('database'); |
|
70 | ||
71 | if (!empty($database)) { |
|
72 | $migrator->setDatabase($database); |
|
73 | } |
|
74 | ||
75 | $migrated = $migrator->rollback(); |
|
76 | ||
77 | if (count($migrated)) { |
|
78 | foreach ($migrated as $migration) { |
|
79 | $this->line("Rollback: <info>{$migration}</info>"); |
|
80 | } |
|
81 | ||
82 | return; |
|
83 | } |
|
84 | ||
85 | $this->comment('Nothing to rollback.'); |
|
86 | } |
|
87 | ||
88 | /** |
|
89 | * Get the console command arguments. |
|
90 | * |
|
91 | * @return array |
|
92 | */ |
|
93 | protected function getArguments() |
|
94 | { |
|
95 | return [ |
|
96 | ['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
|
97 | ]; |
|
98 | } |
|
99 | ||
100 | /** |
|
101 | * Get the console command options. |
|
102 | * |
|
103 | * @return array |
|
104 | */ |
|
105 | protected function getOptions() |
|
106 | { |
|
107 | return [ |
|
108 | ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'], |
|
109 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], |
|
110 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], |
|
111 | ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], |
|
112 | ]; |
|
113 | } |
|
114 | } |
|
115 |