Skip to content

Commit 7b92c07

Browse files
Fix output of numeric defaults (#445)
1 parent 301a556 commit 7b92c07

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected function buildDefinition(Model $model)
213213
if (is_array($modifier)) {
214214
$modifierKey = key($modifier);
215215
$modifierValue = addslashes(current($modifier));
216-
if (in_array($dataType, ['boolean', 'tinyinteger']) && $modifierKey === 'default') {
216+
if ($modifierKey === 'default' && ($modifierValue === 'null' || $dataType === 'boolean' || $this->isNumericDefault($dataType, $modifierValue))) {
217217
$column_definition .= sprintf("->%s(%s)", $modifierKey, $modifierValue);
218218
} else {
219219
$column_definition .= sprintf("->%s('%s')", $modifierKey, $modifierValue);
@@ -435,4 +435,20 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column)
435435
return config('blueprint.use_constraints')
436436
&& ($column->dataType() === 'id' || $column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'));
437437
}
438+
439+
protected function isNumericDefault(string $type, string $value): bool
440+
{
441+
if (! is_numeric($value)) {
442+
return false;
443+
}
444+
445+
if (Str::startsWith($type, 'unsigned')) {
446+
$type = Str::after($type, 'unsigned');
447+
}
448+
449+
return collect(self::UNSIGNABLE_TYPES)
450+
->contains(function ($value) use ($type) {
451+
return strtolower($value) === strtolower($type);
452+
});
453+
}
438454
}

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ public function modelTreeDataProvider()
854854
['drafts/readme-example.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/readme-example.php'],
855855
['drafts/model-identities.yaml', 'database/migrations/timestamp_create_relationships_table.php', 'migrations/identity-columns.php'],
856856
['drafts/model-modifiers.yaml', 'database/migrations/timestamp_create_modifiers_table.php', 'migrations/model-modifiers.php'],
857+
['drafts/model-numeric-defaults.yaml', 'database/migrations/timestamp_create_numerics_table.php', 'migrations/model-numeric-defaults.php'],
857858
['drafts/soft-deletes.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/soft-deletes.php'],
858859
['drafts/with-timezones.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
859860
['drafts/relationships.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/relationships.php'],
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
models:
2+
Numeric:
3+
id: increments
4+
foo: bigInteger default:100
5+
bar: boolean default:0
6+
baz: decimal default:1.0
7+
qui: integer default:1
8+
qux: mediumInteger default:1
9+
quux: smallInteger default:1
10+
corge: tinyInteger default:1
11+
grault: unsignedInteger default:1
12+
garply: integer default:null nullable
13+
waldo: unsignedDecimal default:i
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateNumericsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('numerics', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->bigInteger('foo')->default(100);
19+
$table->boolean('bar')->default(0);
20+
$table->decimal('baz')->default(1.0);
21+
$table->integer('qui')->default(1);
22+
$table->mediumInteger('qux')->default(1);
23+
$table->smallInteger('quux')->default(1);
24+
$table->tinyInteger('corge')->default(1);
25+
$table->unsignedInteger('grault')->default(1);
26+
$table->integer('garply')->default(null)->nullable();
27+
$table->unsignedDecimal('waldo')->default('i');
28+
$table->timestamps();
29+
});
30+
}
31+
32+
/**
33+
* Reverse the migrations.
34+
*
35+
* @return void
36+
*/
37+
public function down()
38+
{
39+
Schema::dropIfExists('numerics');
40+
}
41+
}

0 commit comments

Comments
 (0)