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\Contracts\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
|
|
|
* @var bool |
110
|
|
|
*/ |
111
|
|
|
protected $ignoreCreatedMigration = false; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param \Way\Generators\Generator $generator |
115
|
|
|
* @param \Way\Generators\Filesystem\Filesystem $file |
116
|
|
|
* @param \Way\Generators\Compilers\TemplateCompiler $compiler |
117
|
|
|
* @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
118
|
|
|
* @param \Illuminate\Config\Repository $config |
119
|
|
|
*/ |
120
|
|
|
public function __construct( |
121
|
|
|
Generator $generator, |
122
|
|
|
Filesystem $file, |
123
|
|
|
TemplateCompiler $compiler, |
124
|
|
|
MigrationRepositoryInterface $repository, |
125
|
|
|
Config $config |
126
|
|
|
) |
127
|
|
|
{ |
128
|
|
|
$this->file = $file; |
129
|
|
|
$this->compiler = $compiler; |
130
|
|
|
$this->repository = $repository; |
131
|
|
|
$this->config = $config; |
|
|
|
|
132
|
|
|
|
133
|
|
|
parent::__construct( $generator ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Execute the console command. Added for Laravel 5.5 |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function handle() |
142
|
|
|
{ |
143
|
|
|
$this->fire(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Execute the console command. |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
public function fire() |
152
|
|
|
{ |
153
|
|
|
$this->info( 'Using connection: '. $this->option( 'connection' ) ."\n" ); |
154
|
|
|
if ($this->option('connection') !== $this->config->get('database.default')) { |
155
|
|
|
$this->connection = $this->option('connection'); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
$this->schemaGenerator = new SchemaGenerator( |
158
|
|
|
$this->option('connection'), |
159
|
|
|
$this->option('defaultIndexNames'), |
160
|
|
|
$this->option('defaultFKNames') |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
if ( $this->argument( 'tables' ) ) { |
164
|
|
|
$tables = explode( ',', $this->argument( 'tables' ) ); |
165
|
|
|
} elseif ( $this->option('tables') ) { |
166
|
|
|
$tables = explode( ',', $this->option( 'tables' ) ); |
167
|
|
|
} else { |
168
|
|
|
$tables = $this->schemaGenerator->getTables(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$tables = $this->removeExcludedTables($tables); |
172
|
|
|
$this->info( 'Generating migrations for: '. implode( ', ', $tables ) ); |
173
|
|
|
|
174
|
|
|
if (!$this->option( 'no-interaction' )) { |
175
|
|
|
$this->log = $this->askYn('Do you want to log these migrations in the migrations table?'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ( $this->log ) { |
179
|
|
|
$this->repository->setSource( $this->option( 'connection' ) ); |
180
|
|
|
if ( ! $this->repository->repositoryExists() ) { |
181
|
|
|
$options = array('--database' => $this->option( 'connection' ) ); |
182
|
|
|
$this->call('migrate:install', $options); |
183
|
|
|
} |
184
|
|
|
$batch = $this->repository->getNextBatchNumber(); |
185
|
|
|
$this->batch = $this->askNumeric( 'Next Batch Number is: '. $batch .'. We recommend using Batch Number 0 so that it becomes the "first" migration', 0 ); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if($this->hasOption('ignore-created-migration')) { |
189
|
|
|
$this->ignoreCreatedMigration = true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->info( "Setting up Tables and Index Migrations" ); |
193
|
|
|
$this->datePrefix = date( 'Y_m_d_His' ); |
194
|
|
|
$this->generateTablesAndIndices( $tables ); |
195
|
|
|
$this->info( "\nSetting up Foreign Key Migrations\n" ); |
196
|
|
|
$this->datePrefix = date( 'Y_m_d_His', strtotime( '+1 second' ) ); |
197
|
|
|
$this->generateForeignKeys( $tables ); |
198
|
|
|
$this->info( "\nFinished!\n" ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Ask for user input: Yes/No |
203
|
|
|
* @param string $question Question to ask |
204
|
|
|
* @return boolean Answer from user |
205
|
|
|
*/ |
206
|
|
|
protected function askYn( $question ) { |
207
|
|
|
$answer = $this->ask( $question .' [Y/n] '); |
208
|
|
|
while ( ! in_array( strtolower( $answer ), [ 'y', 'n', 'yes', 'no' ] ) ) { |
209
|
|
|
$answer = $this->ask('Please choose either yes or no. '); |
210
|
|
|
} |
211
|
|
|
return in_array( strtolower( $answer ), [ 'y', 'yes' ] ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Ask user for a Numeric Value, or blank for default |
216
|
|
|
* @param string $question Question to ask |
217
|
|
|
* @param int|float $default Default Value (optional) |
218
|
|
|
* @return int|float Answer |
219
|
|
|
*/ |
220
|
|
|
protected function askNumeric( $question, $default = null ) { |
221
|
|
|
$ask = 'Your answer needs to be a numeric value'; |
222
|
|
|
|
223
|
|
|
if ( ! is_null( $default ) ) { |
224
|
|
|
$question .= ' [Default: '. $default .'] '; |
225
|
|
|
$ask .= ' or blank for default'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$answer = $this->ask( $question ); |
229
|
|
|
|
230
|
|
|
while ( ! is_numeric( $answer ) and ! ( $answer == '' and ! is_null( $default ) ) ) { |
231
|
|
|
$answer = $this->ask( $ask .'. '); |
232
|
|
|
} |
233
|
|
|
if ( $answer == '' ) { |
234
|
|
|
$answer = $default; |
235
|
|
|
} |
236
|
|
|
return $answer; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Generate tables and index migrations. |
241
|
|
|
* |
242
|
|
|
* @param array $tables List of tables to create migrations for |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
protected function generateTablesAndIndices( array $tables ) |
246
|
|
|
{ |
247
|
|
|
$this->method = 'create'; |
248
|
|
|
|
249
|
|
|
foreach ( $tables as $table ) { |
250
|
|
|
$this->table = $table; |
251
|
|
|
$this->migrationName = 'create_'. $this->table .'_table'; |
252
|
|
|
|
253
|
|
|
if($this->ignoreCreatedMigration && $this->migrationExist($this->table)) { |
254
|
|
|
$this->info( "Migration $this->migrationName already created: Skipping..." ); |
255
|
|
|
continue; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$this->fields = $this->schemaGenerator->getFields( $this->table ); |
|
|
|
|
259
|
|
|
|
260
|
|
|
$this->generate(); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/**Return a substring between 2 strings |
265
|
|
|
* @param $string |
266
|
|
|
* @param $start |
267
|
|
|
* @param $end |
268
|
|
|
* @param bool $trim |
269
|
|
|
* @return false|string |
270
|
|
|
*/ |
271
|
|
|
public function getStringBetween($string, $start, $end, $trim = true) { |
272
|
|
|
$string = ' ' . $string; |
273
|
|
|
$ini = strpos($string, $start); |
274
|
|
|
if ($ini == 0) return ''; |
275
|
|
|
$ini += strlen($start); |
276
|
|
|
$len = strpos($string, $end, $ini) - $ini; |
277
|
|
|
$output = substr($string, $ini, $len); |
278
|
|
|
return ($trim) ? trim(strip_tags($output)) : $output; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/**Check if migration for table already exists |
282
|
|
|
* @param $table |
283
|
|
|
* @param string $prefix |
284
|
|
|
* @param string $suffix |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
protected function migrationExist($table, $prefix = 'create_', $suffix = '_table') |
288
|
|
|
{ |
289
|
|
|
$exists = false; |
290
|
|
|
$path = $this->getPathByOptionOrConfig( 'path', 'migration_target_path' ); |
291
|
|
|
$files = scandir($path); |
292
|
|
|
|
293
|
|
|
foreach ($files as $file) |
294
|
|
|
{ |
295
|
|
|
if($this->getStringBetween($file, $prefix, $suffix) == $table) { |
296
|
|
|
$exists = true; |
297
|
|
|
break; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $exists; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Generate foreign key migrations. |
306
|
|
|
* |
307
|
|
|
* @param array $tables List of tables to create migrations for |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
|
|
protected function generateForeignKeys( array $tables ) |
311
|
|
|
{ |
312
|
|
|
$this->method = 'table'; |
313
|
|
|
$prefix = 'add_foreign_keys_to_'; |
314
|
|
|
$suffix = '_table'; |
315
|
|
|
|
316
|
|
|
foreach ( $tables as $table ) { |
317
|
|
|
$this->table = $table; |
318
|
|
|
$this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table'; |
319
|
|
|
|
320
|
|
|
$this->migrationName = $prefix . $this->table . $suffix; |
321
|
|
|
if($this->ignoreCreatedMigration && $this->migrationExist($this->table, $prefix, $suffix)) { |
322
|
|
|
$this->info( "Migration $this->migrationName already created: Skipping..." ); |
323
|
|
|
continue; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table ); |
327
|
|
|
|
328
|
|
|
$this->generate(); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Generate Migration for the current table. |
334
|
|
|
* |
335
|
|
|
* @return void |
336
|
|
|
*/ |
337
|
|
|
protected function generate() |
338
|
|
|
{ |
339
|
|
|
if ( $this->fields ) { |
|
|
|
|
340
|
|
|
parent::fire(); |
|
|
|
|
341
|
|
|
|
342
|
|
|
if ( $this->log ) { |
343
|
|
|
$file = $this->datePrefix . '_' . $this->migrationName; |
344
|
|
|
$this->repository->log($file, $this->batch); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* The path where the file will be created |
351
|
|
|
* |
352
|
|
|
* @return string |
353
|
|
|
*/ |
354
|
|
|
protected function getFileGenerationPath() |
355
|
|
|
{ |
356
|
|
|
$path = $this->getPathByOptionOrConfig( 'path', 'migration_target_path' ); |
357
|
|
|
$migrationName = str_replace('/', '_', $this->migrationName); |
358
|
|
|
$fileName = $this->getDatePrefix() . '_' . $migrationName . '.php'; |
359
|
|
|
|
360
|
|
|
return "{$path}/{$fileName}"; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Get the date prefix for the migration. |
365
|
|
|
* |
366
|
|
|
* @return string |
367
|
|
|
*/ |
368
|
|
|
protected function getDatePrefix() |
369
|
|
|
{ |
370
|
|
|
return $this->datePrefix; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Fetch the template data |
375
|
|
|
* |
376
|
|
|
* @return array |
377
|
|
|
*/ |
378
|
|
|
protected function getTemplateData() |
379
|
|
|
{ |
380
|
|
|
if ( $this->method == 'create' ) { |
381
|
|
|
$up = (new AddToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection, 'create'); |
|
|
|
|
382
|
|
|
$down = (new DroppedTable)->drop($this->table, $this->connection); |
|
|
|
|
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
if ( $this->method == 'table' ) { |
386
|
|
|
$up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection); |
|
|
|
|
387
|
|
|
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection); |
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return [ |
391
|
|
|
'CLASS' => ucwords(camel_case($this->migrationName)), |
|
|
|
|
392
|
|
|
'UP' => $up, |
|
|
|
|
393
|
|
|
'DOWN' => $down |
|
|
|
|
394
|
|
|
]; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Get path to template for generator |
399
|
|
|
* |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
protected function getTemplatePath() |
403
|
|
|
{ |
404
|
|
|
return $this->getPathByOptionOrConfig( 'templatePath', 'migration_template_path' ); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Get the console command arguments. |
409
|
|
|
* |
410
|
|
|
* @return array |
411
|
|
|
*/ |
412
|
|
|
protected function getArguments() |
413
|
|
|
{ |
414
|
|
|
return [ |
415
|
|
|
['tables', InputArgument::OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], |
416
|
|
|
]; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Get the console command options. |
421
|
|
|
* |
422
|
|
|
* @return array |
423
|
|
|
*/ |
424
|
|
|
protected function getOptions() |
425
|
|
|
{ |
426
|
|
|
return [ |
|
|
|
|
427
|
|
|
['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get( 'database.default' )], |
428
|
|
|
['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], |
429
|
|
|
['ignore', 'i', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to ignore, separated by a comma: users,posts,comments' ], |
430
|
|
|
['ignore-created-migration', 'icm', null, 'Flag to ignore migrations already created' ], |
431
|
|
|
['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'], |
432
|
|
|
['templatePath', 'tp', InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'], |
433
|
|
|
['defaultIndexNames', null, InputOption::VALUE_NONE, 'Don\'t use db index names for migrations'], |
434
|
|
|
['defaultFKNames', null, InputOption::VALUE_NONE, 'Don\'t use db foreign key names for migrations'], |
435
|
|
|
]; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Remove all the tables to exclude from the array of tables |
440
|
|
|
* |
441
|
|
|
* @param array $tables |
442
|
|
|
* |
443
|
|
|
* @return array |
444
|
|
|
*/ |
445
|
|
|
protected function removeExcludedTables( array $tables ) |
446
|
|
|
{ |
447
|
|
|
$excludes = $this->getExcludedTables(); |
448
|
|
|
$tables = array_diff($tables, $excludes); |
449
|
|
|
|
450
|
|
|
return $tables; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Get a list of tables to exclude |
455
|
|
|
* |
456
|
|
|
* @return array |
457
|
|
|
*/ |
458
|
|
|
protected function getExcludedTables() |
459
|
|
|
{ |
460
|
|
|
$excludes = ['migrations']; |
461
|
|
|
$ignore = $this->option('ignore'); |
462
|
|
|
if ( ! empty($ignore)) { |
463
|
|
|
return array_merge($excludes, explode(',', $ignore)); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return $excludes; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
} |
470
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..