Skip to content

Commit

Permalink
Toggle foreign key constraints for Migrations (laravel-shift#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Esayeas authored Sep 3, 2020
1 parent 6d2920a commit 86041d0
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MigrationGenerator implements Generator
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $files;

private $hasForeignKeyConstraints = false;

public function __construct($files)
{
$this->files = $files;
Expand Down Expand Up @@ -90,6 +92,10 @@ protected function populateStub(string $stub, Model $model)
$stub = str_replace('{{ table }}', $model->tableName(), $stub);
$stub = str_replace('{{ definition }}', $this->buildDefinition($model), $stub);

if ($this->hasForeignKeyConstraints) {
$stub = $this->disableForeignKeyConstraints($stub);
}

return $stub;
}

Expand All @@ -99,6 +105,10 @@ protected function populatePivotStub(string $stub, array $segments)
$stub = str_replace('{{ table }}', $this->getPivotTableName($segments), $stub);
$stub = str_replace('{{ definition }}', $this->buildPivotTableDefinition($segments), $stub);

if ($this->hasForeignKeyConstraints) {
$stub = $this->disableForeignKeyConstraints($stub);
}

return $stub;
}

Expand Down Expand Up @@ -149,6 +159,7 @@ protected function buildDefinition(Model $model)
$foreign_modifier = $column->isForeignKey();

if ($this->shouldAddForeignKeyConstraint($column)) {
$this->hasForeignKeyConstraints = true;
$foreign = $this->buildForeignKey(
$column->name(),
$foreign_modifier === 'foreign' ? null : $foreign_modifier,
Expand Down Expand Up @@ -232,6 +243,7 @@ protected function buildPivotTableDefinition(array $segments)
}

if (config('blueprint.use_constraints')) {
$this->hasForeignKeyConstraints = true;
$definition .= $this->buildForeignKey($foreign, $on, 'id').';'.PHP_EOL;
} elseif ($this->isLaravel7orNewer()) {
$definition .= self::INDENT.'$table->foreignId(\''.$foreign.'\');'.PHP_EOL;
Expand Down Expand Up @@ -283,6 +295,15 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
return self::INDENT.'$table->foreign'."('{$column_name}')->references('{$column}')->on('{$table}'){$on_delete_suffix}";
}

protected function disableForeignKeyConstraints($stub): string
{
$stub = str_replace('Schema::create(', 'Schema::disableForeignKeyConstraints();'.PHP_EOL.PHP_EOL.str_pad(' ', 8).'Schema::create(', $stub);

$stub = str_replace('});', '});'.PHP_EOL.PHP_EOL.str_pad(' ', 8).'Schema::enableForeignKeyConstraints();', $stub);

return $stub;
}

protected function getClassName(Model $model)
{
return 'Create'.Str::studly($model->tableName()).'Table';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class CreateJourneysTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('journeys', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/belongs-to-many-key-constraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class CreateJourneysTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('journeys', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class CreateDiaryJourneyTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('diary_journey', function (Blueprint $table) {
$table->unsignedBigInteger('diary_id');
$table->foreign('diary_id')->references('id')->on('diaries')->onDelete('cascade');
$table->unsignedBigInteger('journey_id');
$table->foreign('journey_id')->references('id')->on('journeys')->onDelete('cascade');
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class CreateDiaryJourneyTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('diary_journey', function (Blueprint $table) {
$table->foreignId('diary_id')->constrained()->cascadeOnDelete();
$table->foreignId('journey_id')->constrained()->cascadeOnDelete();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/foreign-key-on-delete-laravel6.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('site_id');
Expand All @@ -25,6 +27,8 @@ public function up()
$table->foreign('approver_id')->references('id')->on('users')->onDelete('no action');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/foreign-key-on-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('site_id')->constrained()->cascadeOnDelete();
Expand All @@ -21,6 +23,8 @@ public function up()
$table->foreignId('approver_id')->constrained('users')->onDelete('no action');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/foreign-key-shorthand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->foreignId('author_id')->constrained('users')->cascadeOnDelete();
$table->foreignId('ccid')->constrained('countries', 'code')->cascadeOnDelete();
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/model-key-constraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CreateOrdersTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('orders', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
Expand All @@ -23,6 +25,8 @@ public function up()
$table->json('meta')->default('[]');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/nullable-chaining-laravel6.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class CreateCartsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('carts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->unsignedBigInteger('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/nullable-chaining.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class CreateCartsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('post_id');
Expand All @@ -21,6 +23,8 @@ public function up()
$table->foreign('author_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/relationships-constraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->foreignId('author_id')->constrained('users')->cascadeOnDelete();
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/migrations/unconventional-foreign-key.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CreateStatesTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('states', function (Blueprint $table) {
$table->id();
$table->string('name');
Expand All @@ -25,6 +27,8 @@ public function up()
$table->foreign('c_code')->references('code')->on('countries')->onDelete('cascade');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down

0 comments on commit 86041d0

Please sign in to comment.