From 0859ccce3b2c8b06fe6e93f9b65551ab59834925 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Sun, 7 May 2023 05:31:41 +0300 Subject: [PATCH] Omit length for integer values in migrations --- src/Generators/MigrationGenerator.php | 31 ++++++++++++++-- .../Generators/MigrationGeneratorTest.php | 25 +++++++++++++ .../drafts/omits-length-for-integers.yaml | 12 ++++++ .../migrations/omits-length-for-integers.php | 37 +++++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/drafts/omits-length-for-integers.yaml create mode 100644 tests/fixtures/migrations/omits-length-for-integers.php diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index c4f15987..65d4751a 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -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 @@ -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); } } @@ -199,7 +222,7 @@ protected function buildDefinition(Model $model) $column->name(), $foreign_modifier === 'foreign' ? null : $foreign_modifier, $column->dataType(), - $column->attributes(), + $columnAttributes, $column->modifiers() ); diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index fff0413f..80430985 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -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 [ diff --git a/tests/fixtures/drafts/omits-length-for-integers.yaml b/tests/fixtures/drafts/omits-length-for-integers.yaml new file mode 100644 index 00000000..fa258d67 --- /dev/null +++ b/tests/fixtures/drafts/omits-length-for-integers.yaml @@ -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 diff --git a/tests/fixtures/migrations/omits-length-for-integers.php b/tests/fixtures/migrations/omits-length-for-integers.php new file mode 100644 index 00000000..8c514595 --- /dev/null +++ b/tests/fixtures/migrations/omits-length-for-integers.php @@ -0,0 +1,37 @@ +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'); + } +};