From bec16365e720efa26625623982c32d284e0de21f Mon Sep 17 00:00:00 2001 From: NastuzziSamy Date: Sat, 3 Jul 2021 11:23:41 +0200 Subject: [PATCH] Fix constraints based on self --- resources/views/migration/block.blade.php | 8 +++++++- src/Migrations/MetaNode.php | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/resources/views/migration/block.blade.php b/resources/views/migration/block.blade.php index d9f911b..49d5c4d 100644 --- a/resources/views/migration/block.blade.php +++ b/resources/views/migration/block.blade.php @@ -1,8 +1,14 @@ Schema::{{ $type === 'create' ? $type : 'table' }}({!! json_encode($table) !!}, function (Blueprint {{ '$table' }}) { -@foreach ($blocks as $block) +@foreach ($blocks as $key => $block) +@if ($key === 'self_constraints') }); + + Schema::table({!! json_encode($table) !!}, function (Blueprint {{ '$table' }}) { + @include('laramore::migration.partials.commands', ['commands' => $block]) +@else @if (!$loop->first) @endif @include('laramore::migration.partials.commands', ['commands' => $block]) +@endif @endforeach }); diff --git a/src/Migrations/MetaNode.php b/src/Migrations/MetaNode.php index 841965c..ee3c22c 100644 --- a/src/Migrations/MetaNode.php +++ b/src/Migrations/MetaNode.php @@ -158,6 +158,7 @@ public function getCommands(): array 'fields' => [], 'constraints' => [], 'indexes' => [], + 'self_constraints' => [], ]; foreach ($fieldCommands as $command) { @@ -165,7 +166,11 @@ public function getCommands(): array } foreach ($constraintCommands as $command) { - $commands[($command instanceof IsADropCommand ? 'drop_constraints' : 'constraints')][] = $command; + if (! ($command instanceof IsADropCommand) && $command->getTableName() === $command->getProperties()['on']) { + $commands['self_constraints'][] = $command; + } else { + $commands[($command instanceof IsADropCommand ? 'drop_constraints' : 'constraints')][] = $command; + } } foreach ($indexCommands as $command) { @@ -208,6 +213,7 @@ public function getReverseCommands(): array 'fields' => [], 'constraints' => [], 'indexes' => [], + 'self_constraints' => [], ]; foreach ($fieldCommands as $command) { @@ -215,7 +221,11 @@ public function getReverseCommands(): array } foreach ($constraintCommands as $command) { - $commands[($command instanceof IsADropCommand ? 'drop_constraints' : 'constraints')][] = $command; + if (! ($command instanceof IsADropCommand) && $command->getTableName() === $command->getProperties()['on']) { + $commands['self_constraints'][] = $command; + } else { + $commands[($command instanceof IsADropCommand ? 'drop_constraints' : 'constraints')][] = $command; + } } foreach ($indexCommands as $command) {