Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Commands/DatabaseDropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DatabaseMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DatabaseResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DatabaseRollbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DatabaseSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
50 changes: 32 additions & 18 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down