Skip to content

Commit

Permalink
Auto add primary key if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ce-brex committed Jan 30, 2020
1 parent 8629f7d commit dfcba8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ public function migrate()
$tableName = $this->getTable();

static::resolveConnection()->getSchemaBuilder()->create($tableName, function ($table) use ($firstRow) {
if ($this->incrementing && ! in_array($this->primaryKey, $firstRow)) {
$table->increments($this->primaryKey);
}

foreach ($firstRow as $column => $value) {
$type = is_numeric($value) ? 'integer' : 'string';

if ($column === 'id' && $type == 'integer') {
$table->increments('id');
if ($column === $this->primaryKey && $type == 'integer') {
$table->increments($this->primaryKey);
continue;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ function use_same_cache_between_requests()
{
$this->markTestSkipped('I can\' find a good way to test this right now');
}

/** @test */
function adds_primary_key_if_needed()
{
$this->assertEquals(1, Foo::find(1)->getKey());
}

}

class Foo extends Model
Expand Down

0 comments on commit dfcba8b

Please sign in to comment.