Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit length for integer values in migrations #628

Merged
merged 1 commit into from
May 7, 2023
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
31 changes: 27 additions & 4 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ class MigrationGenerator extends AbstractClassGenerator implements Generator
'tinyInteger',
];

const INTEGER_TYPES = [
'integer',
'tinyInteger',
'smallInteger',
'mediumInteger',
'bigInteger',
'unsignedInteger',
'unsignedTinyInteger',
'unsignedSmallInteger',
'unsignedMediumInteger',
'unsignedBigInteger',
];

private $hasForeignKeyConstraints = false;

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

if (!empty($column->attributes()) && !$this->isIdOrUuid($column->dataType())) {
$columnAttributes = $column->attributes();

if (in_array($dataType, self::INTEGER_TYPES)) {
$columnAttributes = array_filter(
$columnAttributes,
fn ($columnAttribute) => !is_numeric($columnAttribute),
);
}

if (!empty($columnAttributes) && !$this->isIdOrUuid($column->dataType())) {
$column_definition .= ', ';

if (in_array($column->dataType(), ['set', 'enum'])) {
$column_definition .= json_encode($column->attributes());
$column_definition .= json_encode($columnAttributes);
} else {
$column_definition .= implode(', ', $column->attributes());
$column_definition .= implode(', ', $columnAttributes);
}
}

Expand All @@ -199,7 +222,7 @@ protected function buildDefinition(Model $model)
$column->name(),
$foreign_modifier === 'foreign' ? null : $foreign_modifier,
$column->dataType(),
$column->attributes(),
$columnAttributes,
$column->modifiers()
);

Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,31 @@ public function output_generates_custom_pivot_tables(): void
$this->assertEquals(['created' => [$user_migration, $team_migration, $pivot_migration]], $this->subject->output($tree));
}

#[Test]
public function output_omits_length_for_integers(): void
{
$this->filesystem->expects('stub')
->with('migration.stub')
->andReturn($this->stub('migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_omits_table.php';

$this->filesystem->expects('exists')
->with($timestamp_path)
->andReturn(false);

$this->filesystem->expects('put')
->with($timestamp_path, $this->fixture('migrations/omits-length-for-integers.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/omits-length-for-integers.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
}

public function modelTreeDataProvider()
{
return [
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/drafts/omits-length-for-integers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
models:
Omit:
integer: integer:3 unique
tiny_integer: tinyInteger:3 unique
small_integer: smallInteger:3 unique
medium_integer: mediumInteger:3 unique
big_integer: bigInteger:3 unique
unsigned_integer: unsignedInteger:3 unique
unsigned_tiny_integer: unsignedTinyInteger:3 unique
unsigned_small_integer: unsignedSmallInteger:3 unique
unsigned_medium_integer: unsignedMediumInteger:3 unique
unsigned_big_integer: unsignedBigInteger:3 unique
37 changes: 37 additions & 0 deletions tests/fixtures/migrations/omits-length-for-integers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('omits', function (Blueprint $table) {
$table->id();
$table->integer('integer')->unique();
$table->tinyInteger('tiny_integer')->unique();
$table->smallInteger('small_integer')->unique();
$table->mediumInteger('medium_integer')->unique();
$table->bigInteger('big_integer')->unique();
$table->unsignedInteger('unsigned_integer')->unique();
$table->unsignedTinyInteger('unsigned_tiny_integer')->unique();
$table->unsignedSmallInteger('unsigned_small_integer')->unique();
$table->unsignedMediumInteger('unsigned_medium_integer')->unique();
$table->unsignedBigInteger('unsigned_big_integer')->unique();
$table->timestamps();
});
}

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