1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caffeinated\Modules\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use Caffeinated\Modules\Modules; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Console\ConfirmableTrait; |
9
|
|
|
use Illuminate\Database\Migrations\Migrator; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
|
14
|
|
|
class ModuleMigrateCommand extends Command |
15
|
|
|
{ |
16
|
|
|
use ConfirmableTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command name. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'module:migrate'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Run the database migrations for a specific or all modules'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Modules |
34
|
|
|
*/ |
35
|
|
|
protected $module; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Migrator |
39
|
|
|
*/ |
40
|
|
|
protected $migrator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new command instance. |
44
|
|
|
* |
45
|
|
|
* @param Migrator $migrator |
46
|
|
|
* @param Modules $module |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Migrator $migrator, Modules $module) |
49
|
|
|
{ |
50
|
|
|
parent::__construct(); |
51
|
|
|
|
52
|
|
|
$this->migrator = $migrator; |
53
|
|
|
$this->module = $module; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the console command. |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function fire() |
62
|
|
|
{ |
63
|
|
|
$this->prepareDatabase(); |
64
|
|
|
|
65
|
|
|
if (!empty($this->argument('slug'))) { |
66
|
|
|
$module = $this->module->where('slug', $this->argument('slug'))->first(); |
67
|
|
|
|
68
|
|
|
if ($this->module->isEnabled($module['slug'])) { |
69
|
|
|
return $this->migrate($module['slug']); |
70
|
|
|
} elseif ($this->option('force')) { |
71
|
|
|
return $this->migrate($module['slug']); |
72
|
|
|
} else { |
73
|
|
|
return $this->error('Nothing to migrate.'); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
if ($this->option('force')) { |
77
|
|
|
$modules = $this->module->all(); |
78
|
|
|
} else { |
79
|
|
|
$modules = $this->module->enabled(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($modules as $module) { |
83
|
|
|
$this->migrate($module['slug']); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Run migrations for the specified module. |
90
|
|
|
* |
91
|
|
|
* @param string $slug |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
protected function migrate($slug) |
96
|
|
|
{ |
97
|
|
|
if ($this->module->exists($slug)) { |
98
|
|
|
$pretend = Arr::get($this->option(), 'pretend', false); |
99
|
|
|
$path = $this->getMigrationPath($slug); |
100
|
|
|
|
101
|
|
|
if (floatval(App::version()) > 5.1) { |
102
|
|
|
$pretend = ['pretend' => $pretend]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->migrator->run($path, $pretend); |
106
|
|
|
|
107
|
|
|
// Once the migrator has run we will grab the note output and send it out to |
108
|
|
|
// the console screen, since the migrator itself functions without having |
109
|
|
|
// any instances of the OutputInterface contract passed into the class. |
110
|
|
|
foreach ($this->migrator->getNotes() as $note) { |
111
|
|
|
if (!$this->option('quiet')) { |
112
|
|
|
$this->line($note); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Finally, if the "seed" option has been given, we will re-run the database |
117
|
|
|
// seed task to re-populate the database, which is convenient when adding |
118
|
|
|
// a migration and a seed at the same time, as it is only this command. |
119
|
|
|
if ($this->option('seed')) { |
120
|
|
|
$this->call('module:seed', ['module' => $slug, '--force' => true]); |
121
|
|
|
} |
122
|
|
|
} else { |
123
|
|
|
return $this->error('Module does not exist.'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get migration directory path. |
129
|
|
|
* |
130
|
|
|
* @param string $slug |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
protected function getMigrationPath($slug) |
135
|
|
|
{ |
136
|
|
|
$path = $this->module->getModulePath($slug); |
137
|
|
|
|
138
|
|
|
return $path.'Database/Migrations/'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Prepare the migration database for running. |
143
|
|
|
*/ |
144
|
|
|
protected function prepareDatabase() |
145
|
|
|
{ |
146
|
|
|
$this->migrator->setConnection($this->option('database')); |
147
|
|
|
|
148
|
|
|
if (!$this->migrator->repositoryExists()) { |
149
|
|
|
$options = array('--database' => $this->option('database')); |
150
|
|
|
|
151
|
|
|
$this->call('migrate:install', $options); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the console command arguments. |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
protected function getArguments() |
161
|
|
|
{ |
162
|
|
|
return [['slug', InputArgument::OPTIONAL, 'Module slug.']]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the console command options. |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
protected function getOptions() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
return [ |
173
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], |
174
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'], |
175
|
|
|
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'], |
176
|
|
|
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'], |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.