diff --git a/src/Commands/DatabaseDropCommand.php b/src/Commands/DatabaseDropCommand.php index be90af8..a92033f 100644 --- a/src/Commands/DatabaseDropCommand.php +++ b/src/Commands/DatabaseDropCommand.php @@ -22,7 +22,7 @@ protected function handle() foreach ($migrations as $migration) { $currentFileName = path($migration)->basename(); - if ($fileToMigrate && rtrim($currentFileName, '.yml') !== rtrim($fileToMigrate, '.yml')) { + if ($fileToMigrate && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToMigrate)) { continue; } diff --git a/src/Commands/DatabaseMigrationCommand.php b/src/Commands/DatabaseMigrationCommand.php index 0c0072e..9478f20 100755 --- a/src/Commands/DatabaseMigrationCommand.php +++ b/src/Commands/DatabaseMigrationCommand.php @@ -28,7 +28,7 @@ protected function handle() foreach ($migrations as $migration) { $currentFileName = path($migration)->basename(); - if ($fileToMigrate && rtrim($currentFileName, '.yml') !== rtrim($fileToMigrate, '.yml')) { + if ($fileToMigrate && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToMigrate)) { continue; } diff --git a/src/Commands/DatabaseResetCommand.php b/src/Commands/DatabaseResetCommand.php index ffb0dcd..6e5bc06 100644 --- a/src/Commands/DatabaseResetCommand.php +++ b/src/Commands/DatabaseResetCommand.php @@ -23,7 +23,7 @@ protected function handle() foreach ($migrations as $migration) { $currentFileName = path($migration)->basename(); - if ($fileToMigrate && rtrim($currentFileName, '.yml') !== rtrim($fileToMigrate, '.yml')) { + if ($fileToMigrate && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToMigrate)) { continue; } diff --git a/src/Commands/DatabaseRollbackCommand.php b/src/Commands/DatabaseRollbackCommand.php index 4ecac1e..ec2c241 100755 --- a/src/Commands/DatabaseRollbackCommand.php +++ b/src/Commands/DatabaseRollbackCommand.php @@ -23,7 +23,7 @@ protected function handle() foreach ($migrations as $migration) { $currentFileName = path($migration)->basename(); - if ($fileToMigrate && rtrim($currentFileName, '.yml') !== rtrim($fileToMigrate, '.yml')) { + if ($fileToMigrate && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToMigrate)) { continue; } diff --git a/src/Commands/DatabaseSeedCommand.php b/src/Commands/DatabaseSeedCommand.php index ed60390..d0632d7 100755 --- a/src/Commands/DatabaseSeedCommand.php +++ b/src/Commands/DatabaseSeedCommand.php @@ -22,7 +22,7 @@ protected function handle() foreach ($seeds as $seed) { $currentFileName = path($seed)->basename(); - if ($fileToSeed && rtrim($currentFileName, '.yml') !== rtrim($fileToSeed, '.yml')) { + if ($fileToSeed && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToSeed)) { continue; } diff --git a/src/Schema.php b/src/Schema.php index d0ac140..282a31e 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -36,7 +36,7 @@ public static function getConnection(string $fileName): ?string public static function migrate(string $fileToMigrate): bool { $data = Yaml::parseFile($fileToMigrate); - $tableName = str_replace('.yml', '', path($fileToMigrate)->basename()); + $tableName = preg_replace('/\.yml$/', '', path($fileToMigrate)->basename()); $currentConnection = $data['connection'] ?? null; @@ -59,13 +59,7 @@ public static function migrate(string $fileToMigrate): bool $table->increments('id'); } - foreach ($relationships as $model) { - if (strpos($model, 'App\Models') === false) { - $model = "App\Models\\$model"; - } - - $table->foreignIdFor($model); - } + static::assignRelationships(table: $table, relationships: $relationships); foreach ($columns as $columnName => $columnValue) { static::createColumn($table, $columnName, $columnValue); @@ -109,13 +103,7 @@ public static function migrate(string $fileToMigrate): bool $newRelationships = array_diff($relationships, $lastMigration['relationships'] ?? []); $removedRelationships = array_diff($lastMigration['relationships'] ?? [], $relationships); - foreach ($newRelationships as $model) { - if (strpos($model, 'App\Models') === false) { - $model = "App\Models\\$model"; - } - - $table->foreignIdFor($model); - } + static::assignRelationships(table: $table, relationships: $newRelationships); foreach ($removedRelationships as $model) { if (strpos($model, 'App\Models') === false) { @@ -280,6 +268,32 @@ public static function migrate(string $fileToMigrate): bool return true; } + /** + * Assign table relationships including referential constraints + * + * @param Blueprint $table + * @param array $relationships + * @return void + */ + public static function assignRelationships(Blueprint $table, array $relationships): void + { + foreach ($relationships as $relationship) { + $useConstraints = is_array($relationship); + $model = $useConstraints ? array_key_first($model) : $relationship; + + if (strpos($model, 'App\Models') === false) { + $model = "App\Models\\$model"; + } + + if ($useConstraints === true) { + $foreignColumn = $relationship[$model]; + $table->foreignIdFor($model, $foreignColumn)->constrained(); + } else { + $table->foreignIdFor($model); + } + } + } + /** * Seed a database table from schema file * @param string $fileToSeed The name of the schema file @@ -288,7 +302,7 @@ public static function migrate(string $fileToMigrate): bool public static function seed(string $fileToSeed): bool { $data = Yaml::parseFile($fileToSeed); - $tableName = str_replace('.yml', '', path($fileToSeed)->basename()); + $tableName = preg_replace('/\.yml$/', '', path($fileToSeed)->basename()); $seeds = $data['seeds'] ?? []; $currentConnection = $data['connection'] ?? null; @@ -441,7 +455,7 @@ public static function reset(string $fileToReset): bool public static function drop(string $fileToDrop): bool { $data = Yaml::parseFile($fileToDrop); - $tableName = str_replace('.yml', '', path($fileToDrop)->basename()); + $tableName = preg_replace('/\.yml$/', '', path($fileToDrop)->basename()); $currentConnection = $data['connection'] ?? null; @@ -468,7 +482,7 @@ public static function drop(string $fileToDrop): bool */ public static function rollback(string $fileToRollback, int $step = 1): bool { - $tableName = rtrim(path($fileToRollback)->basename(), '.yml'); + $tableName = preg_replace('/\.yml$/', '', path($fileToRollback)->basename()); if (!storage()->exists(StoragePath("database/$tableName"))) { return false;