Skip to content

Commit

Permalink
Apply ->primary() in migration files (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
cable8mm authored Jan 7, 2025
1 parent d3146a7 commit 0ce1cea
Show file tree
Hide file tree
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
Expand Up @@ -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.';';
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 0ce1cea

Please sign in to comment.