Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply ->primary() in migration files
Browse files Browse the repository at this point in the history
cable8mm committed Jan 7, 2025

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent d3146a7 commit b194099
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
@@ -63,6 +63,10 @@ public function last(string $migration): string
$migration .= '->default("'.$this->column->default.'")';
}

if ($this->column->primaryKey === true && $this->column->field !== 'id') {
$migration .= '->primary()';
}

return $migration.';';
}

16 changes: 16 additions & 0 deletions tests/Unit/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -104,4 +104,20 @@ public function test_primary_key_is_empty_when_field_name_is_id(): void

$this->assertStringNotContainsString('primaryKey', $file);
}

public function test_primary_key_is_empty_when_field_name_is_not_id(): void
{
File::system()->delete(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php');

ModelGenerator::make(
new Table('samples', [
Column::make('play_code', 'varchar', unsigned: false, autoIncrement: false, primaryKey: true),
]),
destination: Path::testgen()
)->run();

$file = File::system()->read(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php');

$this->assertStringContainsString('protected $primaryKey = \'play_code\';', $file);
}
}

0 comments on commit b194099

Please sign in to comment.