@@ -172,10 +172,10 @@ public function fire()
172
172
173
173
$ this ->info ( "Setting up Tables and Index Migrations " );
174
174
$ this ->datePrefix = date ( 'Y_m_d_His ' );
175
- $ this ->generate ( ' create ' , $ tables );
175
+ $ this ->generateTablesAndIndices ( $ tables );
176
176
$ this ->info ( "\nSetting up Foreign Key Migrations \n" );
177
177
$ this ->datePrefix = date ( 'Y_m_d_His ' , strtotime ( '+1 second ' ) );
178
- $ this ->generate ( ' foreign_keys ' , $ tables );
178
+ $ this ->generateForeignKeys ( $ tables );
179
179
$ this ->info ( "\nFinished! \n" );
180
180
}
181
181
@@ -218,37 +218,56 @@ protected function askNumeric( $question, $default = null ) {
218
218
}
219
219
220
220
/**
221
- * Generate Migrations
221
+ * Generate tables and index migrations.
222
222
*
223
- * @param string $method Create Tables or Foreign Keys ['create', 'foreign_keys']
224
- * @param array $tables List of tables to create migrations for
225
- * @throws MethodNotFoundException
223
+ * @param array $tables List of tables to create migrations for
226
224
* @return void
227
225
*/
228
- protected function generate ( $ method , $ tables )
226
+ protected function generateTablesAndIndices ( array $ tables )
229
227
{
230
- if ( $ method == 'create ' ) {
231
- $ function = 'getFields ' ;
232
- $ prefix = 'create ' ;
233
- } elseif ( $ method = 'foreign_keys ' ) {
234
- $ function = 'getForeignKeyConstraints ' ;
235
- $ prefix = 'add_foreign_keys_to ' ;
236
- $ method = 'table ' ;
237
- } else {
238
- throw new MethodNotFoundException ( $ method );
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 ();
239
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
+ protected function generateForeignKeys ( array $ tables )
246
+ {
247
+ $ this ->method = 'table ' ;
240
248
241
249
foreach ( $ tables as $ table ) {
242
- $ this ->migrationName = $ prefix .'_ ' . $ table .'_table ' ;
243
- $ this ->method = $ method ;
244
250
$ this ->table = $ table ;
245
- $ this ->fields = $ this ->schemaGenerator ->{$ function }( $ table );
246
- if ( $ this ->fields ) {
247
- parent ::fire ();
248
- if ( $ this ->log ) {
249
- $ file = $ this ->datePrefix . '_ ' . $ this ->migrationName ;
250
- $ this ->repository ->log ($ file , $ this ->batch );
251
- }
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 );
252
271
}
253
272
}
254
273
}
@@ -287,9 +306,11 @@ protected function getTemplateData()
287
306
if ( $ this ->method == 'create ' ) {
288
307
$ up = (new AddToTable ($ this ->file , $ this ->compiler ))->run ($ this ->fields , $ this ->table , $ this ->connection , 'create ' );
289
308
$ down = (new DroppedTable )->drop ($ this ->table , $ this ->connection );
290
- } else {
291
- $ up = (new AddForeignKeysToTable ($ this ->file , $ this ->compiler ))->run ($ this ->fields ,$ this ->table , $ this ->connection );
292
- $ down = (new RemoveForeignKeysFromTable ($ this ->file , $ this ->compiler ))->run ($ this ->fields ,$ 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 );
293
314
}
294
315
295
316
return [
@@ -342,11 +363,11 @@ protected function getOptions()
342
363
/**
343
364
* Remove all the tables to exclude from the array of tables
344
365
*
345
- * @param $tables
366
+ * @param array $tables
346
367
*
347
368
* @return array
348
369
*/
349
- protected function removeExcludedTables ($ tables )
370
+ protected function removeExcludedTables ( array $ tables )
350
371
{
351
372
$ excludes = $ this ->getExcludedTables ();
352
373
$ tables = array_diff ($ tables , $ excludes );
0 commit comments