Repository: laravel-shift/blueprint (php)

Issue:
Default values for boolean and integer come out as strings in migrations,

- Laravel Version: 8.31.0
- PHP Version: 7.4.14
- Blueprint Version: dev-master
- Platform: Mac | Windows | Linux

### Issue:
default for boolean and integer come out as strings, should be numeric or boolean per docs.



### draft.yaml:
```yaml
models:
  AppointmentQuestions:
    appointment_type_id: id foreign
    question_type_id: id foreign
    order: integer default:1
    required: bool default:true
    placeholder: string nullable
    question: string
    hint: string nullable
```

```php
  Schema::create('appointment_questions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('appointment_type_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
            $table->foreignId('question_type_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
            $table->integer('order')->default('1');
            $table->string('required')->default('true');
            $table->string('placeholder')->nullable();
            $table->string('question');
            $table->string('hint')->nullable();
            $table->timestamps();
        });
```

Pull request:
Fix output of numeric defaults
Properly output a numeric default for all numeric column data types.

---
Closes #440

Patch:
diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php
index bfe6b48..eb93b3f 100644
--- a/src/Generators/MigrationGenerator.php
+++ b/src/Generators/MigrationGenerator.php
@@ -213,7 +213,7 @@ class MigrationGenerator implements Generator
                 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);
@@ -435,4 +435,20 @@ class MigrationGenerator implements Generator
         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);
+            });
+    }
 }


Test patch:
diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php
index 1f7af06..37d4c77 100644
--- a/tests/Feature/Generators/MigrationGeneratorTest.php
+++ b/tests/Feature/Generators/MigrationGeneratorTest.php
@@ -854,6 +854,7 @@ class MigrationGeneratorTest extends TestCase
             ['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'],
diff --git a/tests/fixtures/drafts/model-numeric-defaults.yaml b/tests/fixtures/drafts/model-numeric-defaults.yaml
new file mode 100644
index 0000000..618b9e7
--- /dev/null
+++ b/tests/fixtures/drafts/model-numeric-defaults.yaml
@@ -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
diff --git a/tests/fixtures/migrations/model-numeric-defaults.php b/tests/fixtures/migrations/model-numeric-defaults.php
new file mode 100644
index 0000000..ab2b075
--- /dev/null
+++ b/tests/fixtures/migrations/model-numeric-defaults.php
@@ -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');
+    }
+}