Skip to content
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
11 changes: 4 additions & 7 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand All @@ -109,7 +107,6 @@ protected function createMigrations(array $tables, $overwrite = false): array

$output[$action][] = $path;
}

return $output;
}

Expand Down Expand Up @@ -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');
}
);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/drafts/nullable-columns-with-foreign.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
models:
Comment:
id
user_id: id foreign:users.id nullable
integer: integer foreign:monsteras.id nullable
timestamps

38 changes: 38 additions & 0 deletions tests/fixtures/migrations/nullable-columns-with-foreign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->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');
}
}