Skip to content

Commit 0ff38dd

Browse files
committed
Refactor disabling id within lexer
1 parent 09769ce commit 0ff38dd

File tree

9 files changed

+44
-37
lines changed

9 files changed

+44
-37
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ 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' && $model->usesPrimaryKey()) {
72+
73+
if ($column->name() === 'id' && $dataType === 'id') {
7374
$dataType = 'bigIncrements';
7475
} elseif ($dataType === 'id') {
7576
$dataType = 'unsignedBigInteger';

src/Lexers/ModelLexer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ private function buildModel(string $name, array $columns)
120120
if (isset($columns['id'])) {
121121
if ($columns['id'] === false) {
122122
$model->disablePrimaryKey();
123+
unset($columns['id']);
123124
}
124125
}
125126

@@ -154,7 +155,7 @@ private function buildModel(string $name, array $columns)
154155
unset($columns['relationships']);
155156
}
156157

157-
if (!isset($columns['id'])) {
158+
if (!isset($columns['id']) && $model->usesPrimaryKey()) {
158159
$column = $this->buildColumn('id', 'id');
159160
$model->addColumn($column);
160161
}

tests/Feature/BlueprintTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,6 @@ 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-
12198
/**
12299
* @test
123100
*/

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function modelTreeDataProvider()
137137
['definitions/unconventional.bp', 'database/migrations/timestamp_create_teams_table.php', 'migrations/unconventional.php'],
138138
['definitions/optimize.bp', 'database/migrations/timestamp_create_optimizes_table.php', 'migrations/optimize.php'],
139139
['definitions/model-key-constraints.bp', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],
140+
['definitions/disable-auto-columns.bp', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'],
140141
];
141142
}
142143
}

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function it_defaults_the_id_column()
134134
/**
135135
* @test
136136
*/
137-
public function it_disables_primary_keys()
137+
public function it_disables_the_id_column()
138138
{
139139
$tokens = [
140140
'models' => [
@@ -152,6 +152,7 @@ public function it_disables_primary_keys()
152152
$model = $actual['models']['Model'];
153153

154154
$this->assertEquals('Model', $model->name());
155+
$this->assertCount(0, $model->columns());
155156
$this->assertFalse($model->usesPrimaryKey());
156157
}
157158

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
models:
22
State:
3+
id: false
34
name: string
45
code: string
6+
country_id: biginteger unsigned
57
timestamps: false

tests/fixtures/definitions/without-id.bp

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateStatesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('states', function (Blueprint $table) {
17+
$table->string('name');
18+
$table->string('code');
19+
$table->unsignedBigInteger('country_id');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('states');
31+
}
32+
}

tests/fixtures/models/disable-auto-columns-phpdoc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Illuminate\Database\Eloquent\Model;
66

77
/**
8-
* @property int $id
98
* @property string $name
109
* @property string $code
10+
* @property int $country_id
1111
*/
1212
class State extends Model
1313
{
@@ -19,6 +19,7 @@ class State extends Model
1919
protected $fillable = [
2020
'name',
2121
'code',
22+
'country_id',
2223
];
2324

2425
/**
@@ -27,6 +28,6 @@ class State extends Model
2728
* @var array
2829
*/
2930
protected $casts = [
30-
'id' => 'integer',
31+
'country_id' => 'integer',
3132
];
3233
}

0 commit comments

Comments
 (0)