Skip to content

Commit d427d17

Browse files
author
Brian Faust
authored
Omit length for integer values in migrations (#628)
1 parent 1a62e81 commit d427d17

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ class MigrationGenerator extends AbstractClassGenerator implements Generator
4343
'tinyInteger',
4444
];
4545

46+
const INTEGER_TYPES = [
47+
'integer',
48+
'tinyInteger',
49+
'smallInteger',
50+
'mediumInteger',
51+
'bigInteger',
52+
'unsignedInteger',
53+
'unsignedTinyInteger',
54+
'unsignedSmallInteger',
55+
'unsignedMediumInteger',
56+
'unsignedBigInteger',
57+
];
58+
4659
private $hasForeignKeyConstraints = false;
4760

4861
public function output(Tree $tree, $overwrite = false): array
@@ -177,12 +190,22 @@ protected function buildDefinition(Model $model)
177190
$column_definition .= '$table->' . $dataType . "('{$column->name()}'";
178191
}
179192

180-
if (!empty($column->attributes()) && !$this->isIdOrUuid($column->dataType())) {
193+
$columnAttributes = $column->attributes();
194+
195+
if (in_array($dataType, self::INTEGER_TYPES)) {
196+
$columnAttributes = array_filter(
197+
$columnAttributes,
198+
fn ($columnAttribute) => !is_numeric($columnAttribute),
199+
);
200+
}
201+
202+
if (!empty($columnAttributes) && !$this->isIdOrUuid($column->dataType())) {
181203
$column_definition .= ', ';
204+
182205
if (in_array($column->dataType(), ['set', 'enum'])) {
183-
$column_definition .= json_encode($column->attributes());
206+
$column_definition .= json_encode($columnAttributes);
184207
} else {
185-
$column_definition .= implode(', ', $column->attributes());
208+
$column_definition .= implode(', ', $columnAttributes);
186209
}
187210
}
188211

@@ -199,7 +222,7 @@ protected function buildDefinition(Model $model)
199222
$column->name(),
200223
$foreign_modifier === 'foreign' ? null : $foreign_modifier,
201224
$column->dataType(),
202-
$column->attributes(),
225+
$columnAttributes,
203226
$column->modifiers()
204227
);
205228

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,31 @@ public function output_generates_custom_pivot_tables(): void
581581
$this->assertEquals(['created' => [$user_migration, $team_migration, $pivot_migration]], $this->subject->output($tree));
582582
}
583583

584+
#[Test]
585+
public function output_omits_length_for_integers(): void
586+
{
587+
$this->filesystem->expects('stub')
588+
->with('migration.stub')
589+
->andReturn($this->stub('migration.stub'));
590+
591+
$now = Carbon::now();
592+
Carbon::setTestNow($now);
593+
594+
$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_omits_table.php';
595+
596+
$this->filesystem->expects('exists')
597+
->with($timestamp_path)
598+
->andReturn(false);
599+
600+
$this->filesystem->expects('put')
601+
->with($timestamp_path, $this->fixture('migrations/omits-length-for-integers.php'));
602+
603+
$tokens = $this->blueprint->parse($this->fixture('drafts/omits-length-for-integers.yaml'));
604+
$tree = $this->blueprint->analyze($tokens);
605+
606+
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
607+
}
608+
584609
public function modelTreeDataProvider()
585610
{
586611
return [
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
models:
2+
Omit:
3+
integer: integer:3 unique
4+
tiny_integer: tinyInteger:3 unique
5+
small_integer: smallInteger:3 unique
6+
medium_integer: mediumInteger:3 unique
7+
big_integer: bigInteger:3 unique
8+
unsigned_integer: unsignedInteger:3 unique
9+
unsigned_tiny_integer: unsignedTinyInteger:3 unique
10+
unsigned_small_integer: unsignedSmallInteger:3 unique
11+
unsigned_medium_integer: unsignedMediumInteger:3 unique
12+
unsigned_big_integer: unsignedBigInteger:3 unique
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('omits', function (Blueprint $table) {
15+
$table->id();
16+
$table->integer('integer')->unique();
17+
$table->tinyInteger('tiny_integer')->unique();
18+
$table->smallInteger('small_integer')->unique();
19+
$table->mediumInteger('medium_integer')->unique();
20+
$table->bigInteger('big_integer')->unique();
21+
$table->unsignedInteger('unsigned_integer')->unique();
22+
$table->unsignedTinyInteger('unsigned_tiny_integer')->unique();
23+
$table->unsignedSmallInteger('unsigned_small_integer')->unique();
24+
$table->unsignedMediumInteger('unsigned_medium_integer')->unique();
25+
$table->unsignedBigInteger('unsigned_big_integer')->unique();
26+
$table->timestamps();
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*/
33+
public function down(): void
34+
{
35+
Schema::dropIfExists('omits');
36+
}
37+
};

0 commit comments

Comments
 (0)