Skip to content

Commit 0a8646c

Browse files
authored
Optimize unsigned columns (#110)
1 parent 38edd34 commit 0a8646c

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ class MigrationGenerator implements Generator
1212
{
1313
const INDENT = ' ';
1414

15+
const NULLABLE_TYPES = [
16+
'morphs',
17+
'uuidMorphs',
18+
];
19+
20+
const UNSIGNABLE_TYPES = [
21+
'bigInteger',
22+
'decimal',
23+
'integer',
24+
'mediumInteger',
25+
'smallInteger',
26+
'tinyInteger',
27+
];
28+
1529
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
1630
private $files;
1731

@@ -61,6 +75,14 @@ protected function buildDefinition(Model $model)
6175
$dataType = 'unsignedBigInteger';
6276
}
6377

78+
if (in_array($dataType, self::UNSIGNABLE_TYPES) && in_array('unsigned', $column->modifiers())) {
79+
$dataType = 'unsigned' . ucfirst($dataType);
80+
}
81+
82+
if (in_array($dataType, self::NULLABLE_TYPES) && in_array('nullable', $column->modifiers())) {
83+
$dataType = 'nullable' . ucfirst($dataType);
84+
}
85+
6486
if ($dataType === 'bigIncrements' && $this->isLaravel7orNewer()) {
6587
$definition .= self::INDENT . '$table->id(';
6688
} else {
@@ -86,6 +108,10 @@ protected function buildDefinition(Model $model)
86108
} else {
87109
$definition .= '->' . key($modifier) . '(' . current($modifier) . ')';
88110
}
111+
} elseif ($modifier === 'unsigned' && Str::startsWith($dataType, 'unsigned')) {
112+
continue;
113+
} elseif ($modifier === 'nullable' && Str::startsWith($dataType, 'nullable')) {
114+
continue;
89115
} else {
90116
$definition .= '->' . $modifier . '()';
91117
}

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function modelTreeDataProvider()
135135
['definitions/with-timezones.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
136136
['definitions/relationships.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/relationships.php'],
137137
['definitions/unconventional.bp', 'database/migrations/timestamp_create_teams_table.php', 'migrations/unconventional.php'],
138+
['definitions/optimize.bp', 'database/migrations/timestamp_create_optimizes_table.php', 'migrations/optimize.php'],
138139
['definitions/model-key-constraints.bp', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],
139140
];
140141
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
models:
2+
Optimize:
3+
tiny: tinyInteger unsigned
4+
small: smallInteger unsigned
5+
medium: mediumInteger unsigned
6+
int: integer unsigned
7+
dec: decimal:8,2 unsigned
8+
big: bigInteger unsigned
9+
foo: morphs nullable
10+
foobar: uuidMorphs nullable
11+
timestamps: false
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
11
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateOptimizesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('optimizes', function (Blueprint $table) {
17+
$table->id();
18+
$table->unsignedTinyInteger('tiny');
19+
$table->unsignedSmallInteger('small');
20+
$table->unsignedMediumInteger('medium');
21+
$table->unsignedInteger('int');
22+
$table->unsignedDecimal('dec', 8, 2);
23+
$table->unsignedBigInteger('big');
24+
$table->nullableMorphs('foo');
25+
$table->nullableUuidMorphs('foobar');
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('optimizes');
37+
}
38+
}

0 commit comments

Comments
 (0)