Skip to content

Commit 5d14631

Browse files
authored
Add foreignUuid method to replace uuid() and foreign() methods in MigrationGenerator. (#502)
1 parent d0cebf8 commit 5d14631

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Blueprint\Models\Model;
88
use Blueprint\Tree;
99
use Carbon\Carbon;
10-
use Illuminate\Support\Facades\App;
1110
use Illuminate\Support\Str;
1211
use Symfony\Component\Finder\SplFileInfo;
1312
use Illuminate\Filesystem\Filesystem;
@@ -173,7 +172,7 @@ protected function buildDefinition(Model $model)
173172
$column_definition .= '$table->' . $dataType . "('{$column->name()}'";
174173
}
175174

176-
if (! empty($column->attributes()) && ! in_array($column->dataType(), ['id', 'uuid'])) {
175+
if (! empty($column->attributes()) && ! $this->isIdOrUuid($column->dataType())) {
177176
$column_definition .= ', ';
178177
if (in_array($column->dataType(), ['set', 'enum'])) {
179178
$column_definition .= json_encode($column->attributes());
@@ -198,7 +197,7 @@ protected function buildDefinition(Model $model)
198197
$column->modifiers()
199198
);
200199

201-
if ($column->dataType() === 'id') {
200+
if ($this->isIdOrUuid($column->dataType())) {
202201
$column_definition = $foreign;
203202
$foreign = '';
204203
}
@@ -210,7 +209,7 @@ function ($modifier) use ($column) {
210209
|| (is_array($modifier) && key($modifier) === 'onDelete')
211210
|| (is_array($modifier) && key($modifier) === 'onUpdate')
212211
|| $modifier === 'foreign'
213-
|| ($modifier === 'nullable' && $column->dataType() === 'id');
212+
|| ($modifier === 'nullable' && $this->isIdOrUuid($column->dataType()));
214213
}
215214
);
216215
}
@@ -237,7 +236,6 @@ function ($modifier) use ($column) {
237236
if (! empty($foreign)) {
238237
$column_definition .= $foreign . ';' . PHP_EOL;
239238
}
240-
241239
$definition .= $column_definition;
242240
}
243241

@@ -301,7 +299,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
301299
$column = Str::afterLast($column_name, '_');
302300
}
303301

304-
if ($type === 'id' && ! empty($attributes)) {
302+
if ($this->isIdOrUuid($type) && ! empty($attributes)) {
305303
$table = Str::lower(Str::plural($attributes[0]));
306304
}
307305

@@ -318,10 +316,16 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
318316
$on_update_suffix = self::ON_UPDATE_CLAUSES[$on_update_clause];
319317
}
320318

321-
if ($type === 'id') {
319+
if ($this->isIdOrUuid($type)) {
320+
if ($type === 'uuid') {
321+
$method = 'foreignUuid';
322+
} else {
323+
$method = 'foreignId';
324+
}
325+
322326
$prefix = in_array('nullable', $modifiers)
323-
? '$table->foreignId' . "('{$column_name}')->nullable()"
324-
: '$table->foreignId' . "('{$column_name}')";
327+
? '$table->' . "{$method}('{$column_name}')->nullable()"
328+
: '$table->' . "{$method}('{$column_name}')";
325329

326330
if ($on_delete_clause === 'cascade') {
327331
$on_delete_suffix = '->cascadeOnDelete()';
@@ -439,7 +443,7 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column)
439443
}
440444

441445
return config('blueprint.use_constraints')
442-
&& ($column->dataType() === 'id' || $column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'));
446+
&& ($this->isIdOrUuid($column->dataType()) && Str::endsWith($column->name(), '_id'));
443447
}
444448

445449
protected function isNumericDefault(string $type, string $value): bool
@@ -459,4 +463,9 @@ function ($value) use ($type) {
459463
}
460464
);
461465
}
466+
467+
protected function isIdOrUuid(string $dataType)
468+
{
469+
return in_array($dataType, ['id', 'uuid']);
470+
}
462471
}

tests/fixtures/migrations/model-key-constraints.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public function up()
1919
$table->uuid('id')->primary();
2020
$table->foreignId('user_id')->constrained();
2121
$table->string('external_id')->nullable()->index();
22-
$table->uuid('sub_id');
23-
$table->foreign('sub_id')->references('id')->on('subscriptions');
22+
$table->foreignUuid('sub_id')->constrained('subscriptions');
2423
$table->timestamp('expires_at')->nullable()->index();
2524
$table->json('meta')->default('[]');
2625
$table->timestamps();

tests/fixtures/migrations/uuid-shorthand-constraint.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public function up()
1717

1818
Schema::create('people', function (Blueprint $table) {
1919
$table->uuid('id')->primary();
20-
$table->uuid('company_id');
21-
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
20+
$table->foreignUuid('company_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
2221
$table->timestamps();
2322
});
2423

0 commit comments

Comments
 (0)