Skip to content

Commit f66c4b2

Browse files
Configure default data types (#761)
1 parent 5fc8c0f commit f66c4b2

File tree

3 files changed

+163
-2
lines changed

3 files changed

+163
-2
lines changed

config/blueprint.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@
106106
*/
107107
'use_guarded' => false,
108108

109+
/*
110+
|--------------------------------------------------------------------------
111+
| Default Data Types
112+
|--------------------------------------------------------------------------
113+
|
114+
| By default, Blueprint will set the primary key data type to `id` and the
115+
| timestamps data type to `timestamp`. While you may customize these in
116+
| your draft file, you may also configure your own defaults below.
117+
|
118+
*/
119+
'types' => [
120+
'primary' => 'id',
121+
'timestamps' => 'timestamp',
122+
],
123+
109124
/*
110125
|--------------------------------------------------------------------------
111126
| Singular route names

src/Lexers/ModelLexer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ private function buildModel(string $name, array $columns): Model
165165
} elseif (isset($columns['timestampstz'])) {
166166
$model->enableTimestamps(true);
167167
unset($columns['timestampstz']);
168+
} elseif (config('blueprint.types.timestamps') === false) {
169+
$model->disableTimestamps();
170+
} else {
171+
$model->enableTimestamps(strtolower(config('blueprint.types.timestamps', 'timestamp')) === 'timestamptz');
168172
}
169173

170174
if (isset($columns['softdeletes'])) {
@@ -203,8 +207,12 @@ private function buildModel(string $name, array $columns): Model
203207
}
204208

205209
if (!isset($columns['id']) && $model->usesPrimaryKey()) {
206-
$column = $this->buildColumn('id', 'id');
207-
$model->addColumn($column);
210+
if (config('blueprint.types.primary')) {
211+
$column = $this->buildColumn('id', config('blueprint.types.primary', 'id'));
212+
$model->addColumn($column);
213+
} else {
214+
$model->disablePrimaryKey();
215+
}
208216
}
209217

210218
foreach ($columns as $name => $definition) {

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Feature\Lexers;
44

55
use Blueprint\Lexers\ModelLexer;
6+
use Illuminate\Support\Arr;
67
use PHPUnit\Framework\Attributes\DataProvider;
78
use PHPUnit\Framework\Attributes\Test;
89
use Tests\TestCase;
@@ -124,6 +125,42 @@ public function it_defaults_the_id_column(): void
124125
$this->assertEquals(['nullable'], $columns['title']->modifiers());
125126
}
126127

128+
#[Test]
129+
public function it_defaults_the_id_column_based_on_configuration(): void
130+
{
131+
$type = Arr::random(['id', 'uuid', 'ulid']);
132+
config(['blueprint.types.primary' => $type]);
133+
134+
$tokens = [
135+
'models' => [
136+
'Model' => [
137+
'title' => 'string nullable',
138+
],
139+
],
140+
];
141+
142+
$actual = $this->subject->analyze($tokens);
143+
144+
$this->assertIsArray($actual['models']);
145+
$this->assertCount(1, $actual['models']);
146+
147+
$model = $actual['models']['Model'];
148+
$this->assertEquals('Model', $model->name());
149+
$this->assertTrue($model->usesTimestamps());
150+
$this->assertFalse($model->usesSoftDeletes());
151+
152+
$columns = $model->columns();
153+
$this->assertCount(2, $columns);
154+
$this->assertEquals('id', $columns['id']->name());
155+
$this->assertEquals($type, $columns['id']->dataType());
156+
$this->assertEquals([], $columns['id']->attributes());
157+
$this->assertEquals([], $columns['id']->modifiers());
158+
$this->assertEquals('title', $columns['title']->name());
159+
$this->assertEquals('string', $columns['title']->dataType());
160+
$this->assertEquals([], $columns['title']->attributes());
161+
$this->assertEquals(['nullable'], $columns['title']->modifiers());
162+
}
163+
127164
#[Test]
128165
public function it_disables_the_id_column(): void
129166
{
@@ -147,6 +184,73 @@ public function it_disables_the_id_column(): void
147184
$this->assertFalse($model->usesPrimaryKey());
148185
}
149186

187+
#[Test]
188+
public function it_disables_the_id_column_based_on_configuration(): void
189+
{
190+
config(['blueprint.types.primary' => false]);
191+
192+
$tokens = [
193+
'models' => [
194+
'Model' => [
195+
'name' => 'string',
196+
],
197+
],
198+
];
199+
200+
$actual = $this->subject->analyze($tokens);
201+
202+
$this->assertIsArray($actual['models']);
203+
$this->assertCount(1, $actual['models']);
204+
205+
$model = $actual['models']['Model'];
206+
$this->assertEquals('Model', $model->name());
207+
$this->assertFalse($model->usesPrimaryKey());
208+
$this->assertCount(1, $model->columns());
209+
210+
$columns = $model->columns();
211+
$this->assertEquals('name', $columns['name']->name());
212+
$this->assertEquals('string', $columns['name']->dataType());
213+
$this->assertEquals([], $columns['name']->attributes());
214+
$this->assertEquals([], $columns['name']->modifiers());
215+
}
216+
217+
#[Test]
218+
public function it_sets_timestamps_data_type_based_on_configuration(): void
219+
{
220+
$type = Arr::random(['timestampTz', 'timestamptz']);
221+
config(['blueprint.types.timestamps' => $type]);
222+
223+
$tokens = [
224+
'models' => [
225+
'Model' => [
226+
'name' => 'string',
227+
],
228+
],
229+
];
230+
231+
$actual = $this->subject->analyze($tokens);
232+
233+
$this->assertIsArray($actual['models']);
234+
$this->assertCount(1, $actual['models']);
235+
236+
$model = $actual['models']['Model'];
237+
$this->assertEquals('Model', $model->name());
238+
$this->assertTrue($model->usesTimestamps());
239+
$this->assertSame('timestampsTz', $model->timestampsDataType());
240+
$this->assertFalse($model->usesSoftDeletes());
241+
$this->assertCount(2, $model->columns());
242+
243+
$columns = $model->columns();
244+
$this->assertEquals('id', $columns['id']->name());
245+
$this->assertEquals('id', $columns['id']->dataType());
246+
$this->assertEquals([], $columns['id']->attributes());
247+
$this->assertEquals([], $columns['id']->modifiers());
248+
$this->assertEquals('name', $columns['name']->name());
249+
$this->assertEquals('string', $columns['name']->dataType());
250+
$this->assertEquals([], $columns['name']->attributes());
251+
$this->assertEquals([], $columns['name']->modifiers());
252+
}
253+
150254
#[Test]
151255
public function it_disables_timestamps(): void
152256
{
@@ -169,6 +273,40 @@ public function it_disables_timestamps(): void
169273
$this->assertFalse($model->usesSoftDeletes());
170274
}
171275

276+
#[Test]
277+
public function it_disables_timestamps_based_on_configuration(): void
278+
{
279+
config(['blueprint.types.timestamps' => false]);
280+
281+
$tokens = [
282+
'models' => [
283+
'Model' => [
284+
'name' => 'string',
285+
],
286+
],
287+
];
288+
289+
$actual = $this->subject->analyze($tokens);
290+
291+
$this->assertIsArray($actual['models']);
292+
$this->assertCount(1, $actual['models']);
293+
294+
$model = $actual['models']['Model'];
295+
$this->assertEquals('Model', $model->name());
296+
$this->assertFalse($model->usesSoftDeletes());
297+
$this->assertCount(2, $model->columns());
298+
299+
$columns = $model->columns();
300+
$this->assertEquals('id', $columns['id']->name());
301+
$this->assertEquals('id', $columns['id']->dataType());
302+
$this->assertEquals([], $columns['id']->attributes());
303+
$this->assertEquals([], $columns['id']->modifiers());
304+
$this->assertEquals('name', $columns['name']->name());
305+
$this->assertEquals('string', $columns['name']->dataType());
306+
$this->assertEquals([], $columns['name']->attributes());
307+
$this->assertEquals([], $columns['name']->modifiers());
308+
}
309+
172310
#[Test]
173311
public function it_defaults_to_string_datatype(): void
174312
{

0 commit comments

Comments
 (0)