From 6331d4fac00bd0bfafde27b288b91cf6bc1af75a Mon Sep 17 00:00:00 2001 From: Pedro X Date: Thu, 22 Jul 2021 14:05:40 +0100 Subject: [PATCH] allow non-id columns to be nullable when using foreign --- src/Generators/MigrationGenerator.php | 11 ++---- .../Generators/MigrationGeneratorTest.php | 27 +++++++++++++ .../drafts/nullable-columns-with-foreign.yaml | 7 ++++ .../nullable-columns-with-foreign.php | 38 +++++++++++++++++++ 4 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/drafts/nullable-columns-with-foreign.yaml create mode 100644 tests/fixtures/migrations/nullable-columns-with-foreign.php diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 4d8abc71..723d17ba 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -63,10 +63,9 @@ public function output(Tree $tree, $overwrite = false): array $tables = ['tableNames' => [], 'pivotTableNames' => []]; $stub = $this->filesystem->stub('migration.stub'); - /** - * @var \Blueprint\Models\Model $model -*/ + * @var \Blueprint\Models\Model $model + */ foreach ($tree->models() as $model) { $tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model); @@ -98,7 +97,6 @@ protected function createMigrations(array $tables, $overwrite = false): array $path = $this->getTablePath($tableName, $sequential_timestamp->addSecond(), $overwrite); $action = $this->filesystem->exists($path) ? 'updated' : 'created'; $this->filesystem->put($path, $data); - $output[$action][] = $path; } @@ -109,7 +107,6 @@ protected function createMigrations(array $tables, $overwrite = false): array $output[$action][] = $path; } - return $output; } @@ -208,12 +205,12 @@ protected function buildDefinition(Model $model) // TODO: unset the proper modifier $modifiers = collect($modifiers)->reject( - function ($modifier) { + function ($modifier) use ($column) { return (is_array($modifier) && key($modifier) === 'foreign') || (is_array($modifier) && key($modifier) === 'onDelete') || (is_array($modifier) && key($modifier) === 'onUpdate') || $modifier === 'foreign' - || ($modifier === 'nullable' && $this->isLaravel7orNewer()); + || ($modifier === 'nullable' && $this->isLaravel7orNewer() && $column->dataType() === 'id'); } ); } diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 50bf723d..5ee1fba5 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -643,6 +643,33 @@ public function output_creates_foreign_keys_with_nullable_chained_correctly() $this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree)); } + /** + * @test + */ + public function output_creates_nullable_foreign_key_without_column_type_beeing_id() + { + $this->filesystem->expects('stub') + ->with('migration.stub') + ->andReturn($this->stub('migration.stub')); + + $now = Carbon::now(); + Carbon::setTestNow($now); + + $model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_comments_table.php'); + + $this->filesystem->expects('exists')->with($model_migration)->andReturn(false); + + $this->files + ->expects('put') + ->with($model_migration, $this->fixture('migrations/nullable-columns-with-foreign.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/nullable-columns-with-foreign.yaml')); + + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree)); + } + /** * @test * @environment-setup useLaravel6 diff --git a/tests/fixtures/drafts/nullable-columns-with-foreign.yaml b/tests/fixtures/drafts/nullable-columns-with-foreign.yaml new file mode 100644 index 00000000..4eefdadb --- /dev/null +++ b/tests/fixtures/drafts/nullable-columns-with-foreign.yaml @@ -0,0 +1,7 @@ +models: + Comment: + id + user_id: id foreign:users.id nullable + integer: integer foreign:monsteras.id nullable + timestamps + \ No newline at end of file diff --git a/tests/fixtures/migrations/nullable-columns-with-foreign.php b/tests/fixtures/migrations/nullable-columns-with-foreign.php new file mode 100644 index 00000000..b4a6bf99 --- /dev/null +++ b/tests/fixtures/migrations/nullable-columns-with-foreign.php @@ -0,0 +1,38 @@ +id(); + $table->foreignId('user_id')->nullable()->constrained(); + $table->integer('integer')->nullable(); + $table->foreign('integer')->references('id')->on('monsteras'); + $table->timestamps(); + }); + + Schema::enableForeignKeyConstraints(); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('comments'); + } +}