Skip to content

Commit

Permalink
Merge pull request #26 from srdante/without-primary-key
Browse files Browse the repository at this point in the history
Add withoutPrimaryKey method for increment columns
  • Loading branch information
Carl Sverre authored Sep 20, 2022
2 parents 6f0e84c + 2681027 commit 4afaa50
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ Schema::create('test', function (Blueprint $table) {
});
```

### Increment Columns without Primary Key

Sometimes you may want to set a custom primary key. However if your table has an int `increment` column,
Laravel, by default, always sets this column as the primary key. Even if you manually set another one. This behavior can be disabled using the `withoutPrimaryKey` method.

```php
Schema::create('test', function (Blueprint $table) {
$table->id()->withoutPrimaryKey();
$table->uuid('uuid');

$table->primaryKey(['id', 'uuid']);
});
```

## Testing

Execute the tests using PHPUnit
Expand Down
22 changes: 20 additions & 2 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

class Grammar extends MySqlGrammar
{
use CompilesKeys, ModifiesColumns;
use CompilesKeys;
use ModifiesColumns;

public function __construct()
{
Expand Down Expand Up @@ -64,7 +65,8 @@ protected function compileCreateTable($blueprint, $command, $connection)
// We want to do as little as possible ourselves, so we rely on the parent
// to compile everything and then potentially sneak some modifiers in.
return $this->insertCreateTableModifiers(
$blueprint, parent::compileCreateTable($blueprint, $command, $connection)
$blueprint,
parent::compileCreateTable($blueprint, $command, $connection)
);
}

Expand Down Expand Up @@ -174,4 +176,20 @@ protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
// creating the indexes as a part of the create statement.
return str_replace(sprintf('alter table %s add ', $this->wrapTable($blueprint)), '', $compiled);
}

/**
* Get the SQL for an auto-increment column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
{
if (in_array($column->type, $this->serials) && $column->autoIncrement) {
return ($column->withoutPrimaryKey === true)
? ' auto_increment'
: ' auto_increment primary key';
}
}
}
47 changes: 47 additions & 0 deletions tests/Hybrid/CreateTable/IncrementWithoutPrimaryKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @author https://github.com/srdante
*/

namespace SingleStore\Laravel\Tests\Hybrid\CreateTable;

use SingleStore\Laravel\Schema\Blueprint;
use SingleStore\Laravel\Tests\BaseTest;
use SingleStore\Laravel\Tests\Hybrid\HybridTestHelpers;

class IncrementWithoutPrimaryKeyTest extends BaseTest
{
use HybridTestHelpers;

/** @test */
public function it_adds_a_big_increments_without_primary_key()
{
$blueprint = $this->createTable(function (Blueprint $table) {
$table->bigIncrements('id')->withoutPrimaryKey();
$table->uuid('uuid');

$table->primary(['id', 'uuid']);
});

$this->assertCreateStatement(
$blueprint,
'create table `test` (`id` bigint unsigned not null auto_increment, `uuid` char(36) not null, primary key `test_id_uuid_primary`(`id`, `uuid`))'
);
}

/** @test */
public function it_adds_an_id_without_primary_key()
{
$blueprint = $this->createTable(function (Blueprint $table) {
$table->id()->withoutPrimaryKey();
$table->uuid('uuid');

$table->primary(['id', 'uuid']);
});

$this->assertCreateStatement(
$blueprint,
'create table `test` (`id` bigint unsigned not null auto_increment, `uuid` char(36) not null, primary key `test_id_uuid_primary`(`id`, `uuid`))'
);
}
}

0 comments on commit 4afaa50

Please sign in to comment.