@@ -82,7 +82,6 @@ public function handle()
8282 } elseif (!Config::useSmartMigration () || (!$ migration ->isMigrated () && $ migration ->isCreate && !$ migration ->isVirtual )) {
8383 //At this point the current migration is the first one and is not migrated
8484 //Create update the migration using the current resource then recreate the migration file
85-
8685 $ migration ->setResource ($ resource );
8786 $ migration ->path = $ this ->getMigrationFullName ($ migration ->name , $ input ->tableName ); // make sure we use the same path
8887 $ tracker ->updateMigration ($ capsule ->tableName , $ migration );
@@ -281,8 +280,9 @@ protected function getDestenationFile($tableName)
281280 /**
282281 * Creates the table properties.
283282 *
284- * @param array $fields
283+ * @param CrestApps\CodeGenerator\Models\Resource $resource
285284 * @param CrestApps\CodeGenerator\Models\MigrationInput $input
285+ *
286286 * @return string
287287 */
288288 protected function getCreateTableBlueprint (Resource $ resource , MigrationInput $ input )
@@ -296,12 +296,35 @@ protected function getCreateTableBlueprint(Resource $resource, MigrationInput $i
296296 ->addTimestamps ($ properties , $ input ->withoutTimestamps )
297297 ->addSoftDelete ($ properties , $ input ->withSoftDelete )
298298 ->addFieldProperties ($ properties , $ resource ->fields , $ input ->withoutTimestamps , $ input ->withSoftDelete )
299- ->addIndexes ($ properties , $ resource ->indexes )
299+ ->addIndexes ($ properties , $ resource ->indexes , $ this -> getColumnsUsedInMigration ( $ resource , $ input ) )
300300 ->addForeignConstraints ($ properties , $ constraints );
301301
302302 return $ properties ;
303303 }
304304
305+ /**
306+ * Gets a list of all column that are used in the giving migration
307+ *
308+ * @param CrestApps\CodeGenerator\Models\Resource $resource
309+ * @param CrestApps\CodeGenerator\Models\MigrationInput $input
310+ *
311+ * @return string
312+ */
313+ protected function getColumnsUsedInMigration (Resource $ resource , MigrationInput $ input )
314+ {
315+ $ columns = collect ($ resource ->fields )->pluck ('name ' )->toArray ();
316+
317+ if (!$ input ->withoutTimestamps ) {
318+ $ columns [] = 'created_at ' ;
319+ $ columns [] = 'updated_at ' ;
320+ }
321+
322+ if ($ input ->withSoftDelete ) {
323+ $ columns [] = 'deleted_at ' ;
324+ }
325+
326+ return $ columns ;
327+ }
305328 /**
306329 * Adds foreign key constraint to a giving properties.
307330 *
@@ -430,7 +453,7 @@ protected function addOnUpdateConstraint(&$properties, ForeignConstraint $constr
430453 * @param array $indexes
431454 * @return $this
432455 */
433- protected function addIndexes (&$ properties , array $ indexes )
456+ protected function addIndexes (&$ properties , array $ indexes, array $ validColumns )
434457 {
435458 foreach ($ indexes as $ index ) {
436459 if (!$ index ->hasColumns ()) {
@@ -443,10 +466,22 @@ protected function addIndexes(&$properties, array $indexes)
443466 }
444467
445468 if ($ index ->hasMultipleColumns ()) {
469+ $ invalidColumns = array_diff ($ index ->getColumns (), $ validColumns );
470+
471+ if (count ($ invalidColumns ) > 0 ) {
472+ throw new Exception ('Non-Existing columns are being using an index. Invalid columns are " ' . implode (', ' , $ invalidColumns ) . '" ' );
473+ }
474+
446475 $ columns = Helpers::wrapItems ($ index ->getColumns ());
476+
447477 $ indexColumn = sprintf ('[%s] ' , implode (', ' , $ columns ));
448478 } else {
449- $ indexColumn = sprintf ("'%s' " , $ index ->getFirstColumn ());
479+ $ column = $ index ->getFirstColumn ();
480+
481+ if (!in_array ($ column , $ validColumns )) {
482+ throw new Exception ('Non-Existing column is being using an index. Invalid columns is ' . $ column );
483+ }
484+ $ indexColumn = sprintf ("'%s' " , $ column );
450485 }
451486
452487 $ properties .= sprintf ('%s(%s%s) ' , $ this ->getPropertyBase ($ index ->getType ()), $ indexColumn , $ indexName );
@@ -472,7 +507,8 @@ protected function addFieldProperties(&$properties, array $fields, $withoutTimes
472507 $ primaryField = $ this ->getPrimaryField ($ fields );
473508
474509 foreach ($ fields as $ field ) {
475- if ($ field instanceof Field && $ field != $ primaryField && !is_null ($ primaryField )) {
510+
511+ if ($ field instanceof Field && $ field != $ primaryField ) {
476512 if (!$ withoutTimestamps && $ field ->isAutoManagedOnUpdate ()) {
477513 continue ;
478514 }
@@ -1116,6 +1152,8 @@ protected function addPrimaryField(&$property, Field $field = null)
11161152 $ eloquentMethodName = $ this ->getPrimaryMethodName ($ field ->getEloquentDataMethod ());
11171153 $ property .= sprintf ("%s('%s') " , $ this ->getPropertyBase ($ eloquentMethodName ), $ field ->name );
11181154 $ this ->addFieldPropertyClousure ($ property );
1155+ } else {
1156+ $ this ->warn ('Generating a migration without a primary key. ' );
11191157 }
11201158
11211159 return $ this ;
0 commit comments