Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use foreignId for consistency #653

Merged
merged 1 commit into from
Nov 24, 2023
Merged
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
7 changes: 1 addition & 6 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ protected function buildDefinition(Model $model): string
if ($column->name() === 'id' && $dataType === 'id') {
$dataType = 'bigIncrements';
} elseif ($dataType === 'id') {
if ($model->isPivot()) {
// TODO: what if constraints are enabled?
$dataType = 'foreignId';
} else {
$dataType = 'unsignedBigInteger';
}
$dataType = 'foreignId';
}

if (in_array($dataType, self::UNSIGNABLE_TYPES) && in_array('unsigned', $column->modifiers())) {
Expand Down
19 changes: 17 additions & 2 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private function buildModel(string $name, array $columns): Model
if ($type === 'belongsTo') {
$column = $this->columnNameFromRelationship($relationship);
if (isset($columns[$column]) && !str_contains($columns[$column], ' foreign') && !str_contains($columns[$column], ' id')) {
$columns[$column] .= ' id:' . Str::before($relationship, ':');
$columns[$column] = trim($this->removeDataTypes($columns[$column]) . ' id:' . Str::before($relationship, ':'));
}
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ private function buildColumn(string $name, string $definition): Column
$data_type = null;
$modifiers = [];

$tokens = preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition);
$tokens = $this->parseColumn($definition);
foreach ($tokens as $token) {
$parts = explode(':', $token);
$value = $parts[0];
Expand Down Expand Up @@ -330,4 +330,19 @@ private function hasBelongsToRelationship(Model $model, string $reference): bool

return false;
}

private function removeDataTypes(string $definition): string
{
$tokens = array_filter(
$this->parseColumn($definition),
fn ($token) => strtolower($token) !== 'unsigned' && !isset(self::$dataTypes[strtolower($token)])
);

return implode(' ', $tokens);
}

private function parseColumn(string $definition): array
{
return preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/Lexers/ModelLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ public function it_infers_belongsTo_columns(): void
$this->assertEquals([], $columns['id']->modifiers());
$this->assertEquals('venue_id', $columns['venue_id']->name());
$this->assertEquals('id', $columns['venue_id']->dataType());
$this->assertEquals(['unsigned'], $columns['venue_id']->modifiers());
$this->assertEquals([], $columns['venue_id']->modifiers());
$this->assertEquals(['Venue'], $columns['venue_id']->attributes());
$this->assertEquals('region_id', $columns['region_id']->name());
$this->assertEquals('id', $columns['region_id']->dataType());
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/belongs-to-many.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function up(): void
Schema::create('journeys', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('user_id');
$table->foreignId('user_id');
$table->timestamps();
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/migrations/identity-columns-big-increments.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function up(): void
{
Schema::create('relationships', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('another');
$table->foreignId('post_id');
$table->foreignId('another');
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/migrations/identity-columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function up(): void
{
Schema::create('relationships', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('another');
$table->foreignId('post_id');
$table->foreignId('another');
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/migrations/indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function up(): void
Schema::create('posts', function (Blueprint $table) {
$table->unsignedInteger('id');
$table->string('title');
$table->unsignedBigInteger('parent_post_id');
$table->unsignedBigInteger('author_id');
$table->foreignId('parent_post_id');
$table->foreignId('author_id');
$table->timestamp('published_at')->nullable();
$table->unsignedInteger('word_count');
$table->geometry('location');
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/migrations/infer-belongsto.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function up(): void
Schema::create('conferences', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('venue_id');
$table->unsignedBigInteger('region_id');
$table->foreignId('venue_id');
$table->foreignId('region_id');
$table->timestamps();
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/models-with-custom-namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up(): void
$table->id();
$table->string('name', 30);
$table->string('image');
$table->unsignedBigInteger('parent_id')->nullable();
$table->foreignId('parent_id')->nullable();
$table->boolean('active')->default(true);
$table->timestamps();
$table->softDeletes();
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/readme-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up(): void
$table->string('title', 400);
$table->longText('content');
$table->timestamp('published_at')->nullable();
$table->unsignedBigInteger('author_id');
$table->foreignId('author_id');
$table->timestamps();
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/migrations/relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('author_id');
$table->foreignId('post_id');
$table->foreignId('author_id');
$table->timestamps();
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/soft-deletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('post_id');
$table->foreignId('post_id');
$table->timestamps();
$table->softDeletes();
});
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/migrations/unconventional.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function up(): void
Schema::create('teams', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('owner');
$table->unsignedBigInteger('manager');
$table->foreignId('owner');
$table->foreignId('manager');
$table->json('options');
$table->timestamps();
});
Expand Down