1
|
|
|
<?php namespace Xethron\MigrationsGenerator; |
2
|
|
|
|
3
|
|
|
use Way\Generators\Commands\GeneratorCommand; |
4
|
|
|
use Symfony\Component\Console\Input\InputOption; |
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
|
7
|
|
|
use Way\Generators\Generator; |
8
|
|
|
use Way\Generators\Filesystem\Filesystem; |
9
|
|
|
use Way\Generators\Compilers\TemplateCompiler; |
10
|
|
|
use Illuminate\Database\Migrations\MigrationRepositoryInterface; |
11
|
|
|
|
12
|
|
|
use Xethron\MigrationsGenerator\Generators\SchemaGenerator; |
13
|
|
|
use Xethron\MigrationsGenerator\Syntax\AddToTable; |
14
|
|
|
use Xethron\MigrationsGenerator\Syntax\DroppedTable; |
15
|
|
|
use Xethron\MigrationsGenerator\Syntax\AddForeignKeysToTable; |
16
|
|
|
use Xethron\MigrationsGenerator\Syntax\RemoveForeignKeysFromTable; |
17
|
|
|
|
18
|
|
|
use Illuminate\Config\Repository as Config; |
19
|
|
|
|
20
|
|
|
class MigrateGenerateCommand extends GeneratorCommand { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command name. |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name = 'migrate:generate'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Generate a migration from an existing table structure.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Way\Generators\Filesystem\Filesystem |
36
|
|
|
*/ |
37
|
|
|
protected $file; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Way\Generators\Compilers\TemplateCompiler |
41
|
|
|
*/ |
42
|
|
|
protected $compiler; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
46
|
|
|
*/ |
47
|
|
|
protected $repository; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \Illuminate\Config\Repository $config |
51
|
|
|
*/ |
52
|
|
|
protected $config; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var \Xethron\MigrationsGenerator\Generators\SchemaGenerator |
56
|
|
|
*/ |
57
|
|
|
protected $schemaGenerator; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Array of Fields to create in a new Migration |
61
|
|
|
* Namely: Columns, Indexes and Foreign Keys |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $fields = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* List of Migrations that has been done |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
protected $migrations = array(); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
protected $log = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var int |
79
|
|
|
*/ |
80
|
|
|
protected $batch; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Filename date prefix (Y_m_d_His) |
84
|
|
|
* @var string |
85
|
|
|
*/ |
86
|
|
|
protected $datePrefix; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $migrationName; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var string |
95
|
|
|
*/ |
96
|
|
|
protected $method; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var string |
100
|
|
|
*/ |
101
|
|
|
protected $table; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @var string|null |
105
|
|
|
*/ |
106
|
|
|
protected $connection = null; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \Way\Generators\Generator $generator |
110
|
|
|
* @param \Way\Generators\Filesystem\Filesystem $file |
111
|
|
|
* @param \Way\Generators\Compilers\TemplateCompiler $compiler |
112
|
|
|
* @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
113
|
|
|
* @param \Illuminate\Config\Repository $config |
114
|
|
|
*/ |
115
|
|
|
public function __construct( |
116
|
|
|
Generator $generator, |
117
|
|
|
Filesystem $file, |
118
|
|
|
TemplateCompiler $compiler, |
119
|
|
|
MigrationRepositoryInterface $repository, |
120
|
|
|
Config $config |
121
|
|
|
) |
122
|
|
|
{ |
123
|
|
|
$this->file = $file; |
124
|
|
|
$this->compiler = $compiler; |
125
|
|
|
$this->repository = $repository; |
126
|
|
|
$this->config = $config; |
127
|
|
|
|
128
|
|
|
parent::__construct( $generator ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Execute the console command. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function fire() |
137
|
|
|
{ |
138
|
|
|
$this->info( 'Using connection: '. $this->option( 'connection' ) ."\n" ); |
139
|
|
|
if ($this->option('connection') !== $this->config->get('database.default')) { |
140
|
|
|
$this->connection = $this->option('connection'); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
$this->schemaGenerator = new SchemaGenerator( |
143
|
|
|
$this->option('connection'), |
144
|
|
|
$this->option('defaultIndexNames'), |
145
|
|
|
$this->option('defaultFKNames') |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
if ( $this->argument( 'tables' ) ) { |
149
|
|
|
$tables = explode( ',', $this->argument( 'tables' ) ); |
150
|
|
|
} elseif ( $this->option('tables') ) { |
151
|
|
|
$tables = explode( ',', $this->option( 'tables' ) ); |
152
|
|
|
} else { |
153
|
|
|
$tables = $this->schemaGenerator->getTables(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$tables = $this->removeExcludedTables($tables); |
157
|
|
|
$this->info( 'Generating migrations for: '. implode( ', ', $tables ) ); |
158
|
|
|
|
159
|
|
|
if (!$this->option( 'no-interaction' )) { |
160
|
|
|
$this->log = $this->askYn('Do you want to log these migrations in the migrations table?'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ( $this->log ) { |
164
|
|
|
$this->repository->setSource( $this->option( 'connection' ) ); |
165
|
|
|
if ( ! $this->repository->repositoryExists() ) { |
166
|
|
|
$options = array('--database' => $this->option( 'connection' ) ); |
167
|
|
|
$this->call('migrate:install', $options); |
168
|
|
|
} |
169
|
|
|
$batch = $this->repository->getNextBatchNumber(); |
170
|
|
|
$this->batch = $this->askNumeric( 'Next Batch Number is: '. $batch .'. We recommend using Batch Number 0 so that it becomes the "first" migration', 0 ); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->info( "Setting up Tables and Index Migrations" ); |
174
|
|
|
$this->datePrefix = date( 'Y_m_d_His' ); |
175
|
|
|
$this->generateTablesAndIndices( $tables ); |
176
|
|
|
$this->info( "\nSetting up Foreign Key Migrations\n" ); |
177
|
|
|
$this->datePrefix = date( 'Y_m_d_His', strtotime( '+1 second' ) ); |
178
|
|
|
$this->generateForeignKeys( $tables ); |
179
|
|
|
$this->info( "\nFinished!\n" ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Ask for user input: Yes/No |
184
|
|
|
* @param string $question Question to ask |
185
|
|
|
* @return boolean Answer from user |
186
|
|
|
*/ |
187
|
|
|
protected function askYn( $question ) { |
188
|
|
|
$answer = $this->ask( $question .' [Y/n] '); |
189
|
|
|
while ( ! in_array( strtolower( $answer ), [ 'y', 'n', 'yes', 'no' ] ) ) { |
190
|
|
|
$answer = $this->ask('Please choose either yes or no. '); |
191
|
|
|
} |
192
|
|
|
return in_array( strtolower( $answer ), [ 'y', 'yes' ] ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Ask user for a Numeric Value, or blank for default |
197
|
|
|
* @param string $question Question to ask |
198
|
|
|
* @param int|float $default Default Value (optional) |
199
|
|
|
* @return int|float Answer |
200
|
|
|
*/ |
201
|
|
|
protected function askNumeric( $question, $default = null ) { |
202
|
|
|
$ask = 'Your answer needs to be a numeric value'; |
203
|
|
|
|
204
|
|
|
if ( ! is_null( $default ) ) { |
205
|
|
|
$question .= ' [Default: '. $default .'] '; |
206
|
|
|
$ask .= ' or blank for default'; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$answer = $this->ask( $question ); |
210
|
|
|
|
211
|
|
|
while ( ! is_numeric( $answer ) and ! ( $answer == '' and ! is_null( $default ) ) ) { |
212
|
|
|
$answer = $this->ask( $ask .'. '); |
213
|
|
|
} |
214
|
|
|
if ( $answer == '' ) { |
215
|
|
|
$answer = $default; |
216
|
|
|
} |
217
|
|
|
return $answer; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Generate tables and index migrations. |
222
|
|
|
* |
223
|
|
|
* @param array $tables List of tables to create migrations for |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
View Code Duplication |
protected function generateTablesAndIndices( array $tables ) |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
$this->method = 'create'; |
229
|
|
|
|
230
|
|
|
foreach ( $tables as $table ) { |
231
|
|
|
$this->table = $table; |
232
|
|
|
$this->migrationName = 'create_'. $this->table .'_table'; |
233
|
|
|
$this->fields = $this->schemaGenerator->getFields( $this->table ); |
|
|
|
|
234
|
|
|
|
235
|
|
|
$this->generate(); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Generate foreign key migrations. |
241
|
|
|
* |
242
|
|
|
* @param array $tables List of tables to create migrations for |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
View Code Duplication |
protected function generateForeignKeys( array $tables ) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$this->method = 'table'; |
248
|
|
|
|
249
|
|
|
foreach ( $tables as $table ) { |
250
|
|
|
$this->table = $table; |
251
|
|
|
$this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table'; |
252
|
|
|
$this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table ); |
253
|
|
|
|
254
|
|
|
$this->generate(); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Generate Migration for the current table. |
260
|
|
|
* |
261
|
|
|
* @return void |
262
|
|
|
*/ |
263
|
|
|
protected function generate() |
264
|
|
|
{ |
265
|
|
|
if ( $this->fields ) { |
|
|
|
|
266
|
|
|
parent::fire(); |
|
|
|
|
267
|
|
|
|
268
|
|
|
if ( $this->log ) { |
269
|
|
|
$file = $this->datePrefix . '_' . $this->migrationName; |
270
|
|
|
$this->repository->log($file, $this->batch); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* The path where the file will be created |
277
|
|
|
* |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
protected function getFileGenerationPath() |
281
|
|
|
{ |
282
|
|
|
$path = $this->getPathByOptionOrConfig( 'path', 'migration_target_path' ); |
283
|
|
|
$migrationName = str_replace('/', '_', $this->migrationName); |
284
|
|
|
$fileName = $this->getDatePrefix() . '_' . $migrationName . '.php'; |
285
|
|
|
|
286
|
|
|
return "{$path}/{$fileName}"; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get the date prefix for the migration. |
291
|
|
|
* |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
protected function getDatePrefix() |
295
|
|
|
{ |
296
|
|
|
return $this->datePrefix; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Fetch the template data |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
|
|
protected function getTemplateData() |
305
|
|
|
{ |
306
|
|
|
if ( $this->method == 'create' ) { |
307
|
|
|
$up = (new AddToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection, 'create'); |
|
|
|
|
308
|
|
|
$down = (new DroppedTable)->drop($this->table, $this->connection); |
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if ( $this->method == 'table' ) { |
312
|
|
|
$up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection); |
|
|
|
|
313
|
|
|
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return [ |
317
|
|
|
'CLASS' => ucwords(camel_case($this->migrationName)), |
318
|
|
|
'UP' => $up, |
|
|
|
|
319
|
|
|
'DOWN' => $down |
|
|
|
|
320
|
|
|
]; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get path to template for generator |
325
|
|
|
* |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
|
|
protected function getTemplatePath() |
329
|
|
|
{ |
330
|
|
|
return $this->getPathByOptionOrConfig( 'templatePath', 'migration_template_path' ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the console command arguments. |
335
|
|
|
* |
336
|
|
|
* @return array |
337
|
|
|
*/ |
338
|
|
|
protected function getArguments() |
339
|
|
|
{ |
340
|
|
|
return [ |
341
|
|
|
['tables', InputArgument::OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], |
342
|
|
|
]; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Get the console command options. |
347
|
|
|
* |
348
|
|
|
* @return array |
349
|
|
|
*/ |
350
|
|
|
protected function getOptions() |
351
|
|
|
{ |
352
|
|
|
return [ |
|
|
|
|
353
|
|
|
['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get( 'database.default' )], |
354
|
|
|
['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], |
355
|
|
|
['ignore', 'i', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to ignore, separated by a comma: users,posts,comments' ], |
356
|
|
|
['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'], |
357
|
|
|
['templatePath', 'tp', InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'], |
358
|
|
|
['defaultIndexNames', null, InputOption::VALUE_NONE, 'Don\'t use db index names for migrations'], |
359
|
|
|
['defaultFKNames', null, InputOption::VALUE_NONE, 'Don\'t use db foreign key names for migrations'], |
360
|
|
|
]; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Remove all the tables to exclude from the array of tables |
365
|
|
|
* |
366
|
|
|
* @param array $tables |
367
|
|
|
* |
368
|
|
|
* @return array |
369
|
|
|
*/ |
370
|
|
|
protected function removeExcludedTables( array $tables ) |
371
|
|
|
{ |
372
|
|
|
$excludes = $this->getExcludedTables(); |
373
|
|
|
$tables = array_diff($tables, $excludes); |
374
|
|
|
|
375
|
|
|
return $tables; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Get a list of tables to exclude |
380
|
|
|
* |
381
|
|
|
* @return array |
382
|
|
|
*/ |
383
|
|
|
protected function getExcludedTables() |
384
|
|
|
{ |
385
|
|
|
$excludes = ['migrations']; |
386
|
|
|
$ignore = $this->option('ignore'); |
387
|
|
|
if ( ! empty($ignore)) { |
388
|
|
|
return array_merge($excludes, explode(',', $ignore)); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $excludes; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.