Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected function buildDefinition(Model $model)
if (is_array($modifier)) {
$modifierKey = key($modifier);
$modifierValue = addslashes(current($modifier));
if (in_array($dataType, ['boolean', 'tinyinteger']) && $modifierKey === 'default') {
if ($modifierKey === 'default' && ($modifierValue === 'null' || $dataType === 'boolean' || $this->isNumericDefault($dataType, $modifierValue))) {
$column_definition .= sprintf("->%s(%s)", $modifierKey, $modifierValue);
} else {
$column_definition .= sprintf("->%s('%s')", $modifierKey, $modifierValue);
Expand Down Expand Up @@ -435,4 +435,20 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column)
return config('blueprint.use_constraints')
&& ($column->dataType() === 'id' || $column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'));
}

protected function isNumericDefault(string $type, string $value): bool
{
if (! is_numeric($value)) {
return false;
}

if (Str::startsWith($type, 'unsigned')) {
$type = Str::after($type, 'unsigned');
}

return collect(self::UNSIGNABLE_TYPES)
->contains(function ($value) use ($type) {
return strtolower($value) === strtolower($type);
});
}
}
1 change: 1 addition & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ public function modelTreeDataProvider()
['drafts/readme-example.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/readme-example.php'],
['drafts/model-identities.yaml', 'database/migrations/timestamp_create_relationships_table.php', 'migrations/identity-columns.php'],
['drafts/model-modifiers.yaml', 'database/migrations/timestamp_create_modifiers_table.php', 'migrations/model-modifiers.php'],
['drafts/model-numeric-defaults.yaml', 'database/migrations/timestamp_create_numerics_table.php', 'migrations/model-numeric-defaults.php'],
['drafts/soft-deletes.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/soft-deletes.php'],
['drafts/with-timezones.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
['drafts/relationships.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/relationships.php'],
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/drafts/model-numeric-defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
models:
Numeric:
id: increments
foo: bigInteger default:100
bar: boolean default:0
baz: decimal default:1.0
qui: integer default:1
qux: mediumInteger default:1
quux: smallInteger default:1
corge: tinyInteger default:1
grault: unsignedInteger default:1
garply: integer default:null nullable
waldo: unsignedDecimal default:i
41 changes: 41 additions & 0 deletions tests/fixtures/migrations/model-numeric-defaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNumericsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('numerics', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('foo')->default(100);
$table->boolean('bar')->default(0);
$table->decimal('baz')->default(1.0);
$table->integer('qui')->default(1);
$table->mediumInteger('qux')->default(1);
$table->smallInteger('quux')->default(1);
$table->tinyInteger('corge')->default(1);
$table->unsignedInteger('grault')->default(1);
$table->integer('garply')->default(null)->nullable();
$table->unsignedDecimal('waldo')->default('i');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('numerics');
}
}