Skip to content

Commit 09769ce

Browse files
authored
Disable id column (#111)
1 parent 0a8646c commit 09769ce

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function buildDefinition(Model $model)
6969
/** @var \Blueprint\Models\Column $column */
7070
foreach ($model->columns() as $column) {
7171
$dataType = $column->dataType();
72-
if ($column->name() === 'id' && $dataType === 'id') {
72+
if ($column->name() === 'id' && $dataType === 'id' && $model->usesPrimaryKey()) {
7373
$dataType = 'bigIncrements';
7474
} elseif ($dataType === 'id') {
7575
$dataType = 'unsignedBigInteger';

src/Lexers/ModelLexer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ private function buildModel(string $name, array $columns)
117117
{
118118
$model = new Model($name);
119119

120+
if (isset($columns['id'])) {
121+
if ($columns['id'] === false) {
122+
$model->disablePrimaryKey();
123+
}
124+
}
125+
120126
if (isset($columns['timestamps'])) {
121127
if ($columns['timestamps'] === false) {
122128
$model->disableTimestamps();

src/Models/Model.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Model
88
{
99
private $name;
1010
private $namespace;
11+
private $primaryKey = 'id';
1112
private $timestamps = 'timestamps';
1213
private $softDeletes = false;
1314
private $columns = [];
@@ -73,7 +74,17 @@ public function relationships(): array
7374

7475
public function primaryKey()
7576
{
76-
return 'id';
77+
return $this->primaryKey;
78+
}
79+
80+
public function usesPrimaryKey()
81+
{
82+
return $this->primaryKey !== false;
83+
}
84+
85+
public function disablePrimaryKey()
86+
{
87+
$this->primaryKey = false;
7788
}
7889

7990
public function tableName()

tests/Feature/BlueprintTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ public function it_parses_shorthands()
9595
], $this->subject->parse($blueprint));
9696
}
9797

98+
/**
99+
* @test
100+
*/
101+
public function it_parses_shorthands_that_disables_id()
102+
{
103+
$blueprint = $this->fixture('definitions/without-id.bp');
104+
105+
$this->assertEquals([
106+
'models' => [
107+
'Name' => [
108+
'softdeletes' => 'softDeletes',
109+
'id' => false,
110+
'timestamps' => 'timestamps',
111+
],
112+
],
113+
'controllers' => [
114+
'Context' => [
115+
'resource' => 'all'
116+
]
117+
]
118+
], $this->subject->parse($blueprint));
119+
}
120+
98121
/**
99122
* @test
100123
*/

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ public function it_defaults_the_id_column()
131131
$this->assertEquals(['nullable'], $columns['title']->modifiers());
132132
}
133133

134+
/**
135+
* @test
136+
*/
137+
public function it_disables_primary_keys()
138+
{
139+
$tokens = [
140+
'models' => [
141+
'Model' => [
142+
'id' => false,
143+
]
144+
],
145+
];
146+
147+
$actual = $this->subject->analyze($tokens);
148+
149+
$this->assertIsArray($actual['models']);
150+
$this->assertCount(1, $actual['models']);
151+
152+
$model = $actual['models']['Model'];
153+
154+
$this->assertEquals('Model', $model->name());
155+
$this->assertFalse($model->usesPrimaryKey());
156+
}
157+
134158
/**
135159
* @test
136160
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
models:
2+
Name:
3+
softDeletes
4+
id: false
5+
timestamps
6+
7+
controllers:
8+
Context:
9+
resource

0 commit comments

Comments
 (0)