Skip to content

Commit 2d82549

Browse files
Re-apply fixes from #661 (#681)
1 parent 09fc6c5 commit 2d82549

12 files changed

+282
-2
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ protected function buildPivotTableDefinition(array $segments, array $models = []
390390

391391
if (config('blueprint.use_constraints')) {
392392
$this->hasForeignKeyConstraints = true;
393-
$definition .= $this->buildForeignKey($foreign, $on, 'id') . ';' . PHP_EOL;
393+
394+
$type = isset($models[$segment]) ? $models[$segment]->idType() : 'id';
395+
$definition .= $this->buildForeignKey($foreign, $on, $type) . ';' . PHP_EOL;
394396
} else {
395397
$definition .= $this->generateForeignKeyDefinition($segment, $foreign, $models);
396398
}

src/Generators/ModelGenerator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ protected function buildClassPhpDoc(Model $model): string
9191
$phpDoc .= PHP_EOL;
9292
$phpDoc .= ' * @property string|null $' . $column->name() . '_type';
9393
$phpDoc .= PHP_EOL;
94+
} elseif ($column->dataType() === 'ulidMorphs') {
95+
$phpDoc .= ' * @property string $' . $column->name() . '_id';
96+
$phpDoc .= PHP_EOL;
97+
$phpDoc .= ' * @property string $' . $column->name() . '_type';
98+
$phpDoc .= PHP_EOL;
99+
} elseif ($column->dataType() === 'nullableUlidMorphs') {
100+
$phpDoc .= ' * @property string|null $' . $column->name() . '_id';
101+
$phpDoc .= PHP_EOL;
102+
$phpDoc .= ' * @property string|null $' . $column->name() . '_type';
103+
$phpDoc .= PHP_EOL;
94104
} elseif ($column->dataType() === 'uuidMorphs') {
95105
$phpDoc .= ' * @property string $' . $column->name() . '_id';
96106
$phpDoc .= PHP_EOL;

src/Models/Model.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public function usesUuids(): bool
111111
return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid';
112112
}
113113

114+
public function idType(): ?string
115+
{
116+
if (!$this->usesPrimaryKey()) {
117+
return null;
118+
}
119+
120+
return $this->columns[$this->primaryKey]->dataType();
121+
}
122+
114123
public function disablePrimaryKey(): void
115124
{
116125
$this->primaryKey = false;

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,70 @@ public function output_also_updates_pivot_table_migration(): void
245245
$this->assertEquals(['updated' => [$model_migration, $pivot_migration]], $this->subject->output($tree, true));
246246
}
247247

248+
#[Test]
249+
public function output_also_creates_constraints_for_pivot_table_migration_for_ulids(): void
250+
{
251+
$this->app->config->set('blueprint.use_constraints', true);
252+
253+
$this->filesystem->expects('stub')
254+
->with('migration.stub')
255+
->andReturn($this->stub('migration.stub'));
256+
257+
$now = Carbon::now();
258+
Carbon::setTestNow($now);
259+
260+
$journey_model_migration = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
261+
$diary_model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_diaries_table.php');
262+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
263+
264+
$this->filesystem->expects('exists')->times(3)->andReturn(false);
265+
266+
$this->filesystem->expects('put')
267+
->with($journey_model_migration, $this->fixture('migrations/belongs-to-many-key-constraints-using-ulid-columns-journey-model.php'));
268+
$this->filesystem->expects('put')
269+
->with($diary_model_migration, $this->fixture('migrations/belongs-to-many-key-constraints-using-ulid-columns-diary-model.php'));
270+
271+
$this->filesystem->expects('put')
272+
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-pivot-key-constraints-using-ulid-columns.php'));
273+
274+
$tokens = $this->blueprint->parse($this->fixture('drafts/belongs-to-many-using-ulids.yaml'));
275+
$tree = $this->blueprint->analyze($tokens);
276+
277+
$this->assertEquals(['created' => [$journey_model_migration, $diary_model_migration, $pivot_migration]], $this->subject->output($tree));
278+
}
279+
280+
#[Test]
281+
public function output_also_creates_constraints_for_pivot_table_migration_for_uuids(): void
282+
{
283+
$this->app->config->set('blueprint.use_constraints', true);
284+
285+
$this->filesystem->expects('stub')
286+
->with('migration.stub')
287+
->andReturn($this->stub('migration.stub'));
288+
289+
$now = Carbon::now();
290+
Carbon::setTestNow($now);
291+
292+
$journey_model_migration = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
293+
$diary_model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_diaries_table.php');
294+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
295+
296+
$this->filesystem->expects('exists')->times(3)->andReturn(false);
297+
298+
$this->filesystem->expects('put')
299+
->with($journey_model_migration, $this->fixture('migrations/belongs-to-many-key-constraints-using-uuid-columns-journey-model.php'));
300+
$this->filesystem->expects('put')
301+
->with($diary_model_migration, $this->fixture('migrations/belongs-to-many-key-constraints-using-uuid-columns-diary-model.php'));
302+
303+
$this->filesystem->expects('put')
304+
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-pivot-key-constraints-using-uuid-columns.php'));
305+
306+
$tokens = $this->blueprint->parse($this->fixture('drafts/belongs-to-many-using-uuids.yaml'));
307+
$tree = $this->blueprint->analyze($tokens);
308+
309+
$this->assertEquals(['created' => [$journey_model_migration, $diary_model_migration, $pivot_migration]], $this->subject->output($tree));
310+
}
311+
248312
#[Test]
249313
public function output_also_creates_constraints_for_pivot_table_migration(): void
250314
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
models:
2+
Journey:
3+
uuid
4+
name: string
5+
relationships:
6+
belongsToMany: Diary
7+
Diary:
8+
uuid
9+
relationships:
10+
belongsToMany: Journey

tests/fixtures/drafts/belongs-to-many.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ models:
33
name: string
44
user_id: id
55
relationships:
6-
belongstoMany: Diary
6+
belongsToMany: Diary
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::disableForeignKeyConstraints();
15+
16+
Schema::create('diaries', function (Blueprint $table) {
17+
$table->ulid('id')->primary();
18+
$table->timestamps();
19+
});
20+
21+
Schema::enableForeignKeyConstraints();
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('diaries');
30+
}
31+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::disableForeignKeyConstraints();
15+
16+
Schema::create('journeys', function (Blueprint $table) {
17+
$table->ulid('id')->primary();
18+
$table->string('name');
19+
$table->foreignUlid('user_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
20+
$table->timestamps();
21+
});
22+
23+
Schema::enableForeignKeyConstraints();
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*/
29+
public function down(): void
30+
{
31+
Schema::dropIfExists('journeys');
32+
}
33+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::disableForeignKeyConstraints();
15+
16+
Schema::create('diaries', function (Blueprint $table) {
17+
$table->uuid('id')->primary();
18+
$table->timestamps();
19+
});
20+
21+
Schema::enableForeignKeyConstraints();
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('diaries');
30+
}
31+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('journeys', function (Blueprint $table) {
15+
$table->uuid('id')->primary();
16+
$table->string('name');
17+
$table->timestamps();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::dropIfExists('journeys');
27+
}
28+
};

0 commit comments

Comments
 (0)