From c1f6374f377d7b6d2aeefa464837226279a521a3 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 28 Jul 2019 16:26:24 +0200 Subject: Migrations: Changed defaults, bug fixes and return object for chaining --- .../2018_10_01_000000_create_users_tables.php | 13 ++++++------ db/migrations/ChangesReferences.php | 23 +++++++++++++++------- db/migrations/Reference.php | 12 ++++++++--- 3 files changed, 31 insertions(+), 17 deletions(-) (limited to 'db') diff --git a/db/migrations/2018_10_01_000000_create_users_tables.php b/db/migrations/2018_10_01_000000_create_users_tables.php index 52b3658f..5b88511b 100644 --- a/db/migrations/2018_10_01_000000_create_users_tables.php +++ b/db/migrations/2018_10_01_000000_create_users_tables.php @@ -36,7 +36,7 @@ class CreateUsersTables extends Migration }); $this->schema->create('users_personal_data', function (Blueprint $table) { - $this->referencesUser($table); + $this->referencesUser($table, true); $table->string('first_name', 64)->nullable(); $table->string('last_name', 64)->nullable(); @@ -47,7 +47,7 @@ class CreateUsersTables extends Migration }); $this->schema->create('users_contact', function (Blueprint $table) { - $this->referencesUser($table); + $this->referencesUser($table, true); $table->string('dect', 5)->nullable(); $table->string('mobile', 40)->nullable(); @@ -55,7 +55,7 @@ class CreateUsersTables extends Migration }); $this->schema->create('users_settings', function (Blueprint $table) { - $this->referencesUser($table); + $this->referencesUser($table, true); $table->string('language', 64); $table->tinyInteger('theme'); @@ -64,7 +64,7 @@ class CreateUsersTables extends Migration }); $this->schema->create('users_state', function (Blueprint $table) { - $this->referencesUser($table); + $this->referencesUser($table, true); $table->boolean('arrived')->default(false); $table->dateTime('arrival_date')->nullable(); @@ -75,7 +75,7 @@ class CreateUsersTables extends Migration }); $this->schema->create('password_resets', function (Blueprint $table) { - $this->referencesUser($table); + $this->referencesUser($table, true); $table->text('token'); @@ -157,8 +157,7 @@ class CreateUsersTables extends Migration 'User', 'UID', 'users', - 'id', - 'unsignedInteger' + 'id' ); } diff --git a/db/migrations/ChangesReferences.php b/db/migrations/ChangesReferences.php index 6bdd187d..84156c7b 100644 --- a/db/migrations/ChangesReferences.php +++ b/db/migrations/ChangesReferences.php @@ -3,6 +3,7 @@ namespace Engelsystem\Migrations; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder as SchemaBuilder; use stdClass; trait ChangesReferences @@ -14,24 +15,30 @@ trait ChangesReferences * @param string $targetColumn * @param string $type */ - protected function changeReferences($fromTable, $fromColumn, $targetTable, $targetColumn, $type) + protected function changeReferences($fromTable, $fromColumn, $targetTable, $targetColumn, $type = 'unsignedInteger') { $references = $this->getReferencingTables($fromTable, $fromColumn); foreach ($references as $reference) { /** @var stdClass $reference */ - $this->schema->table($reference->table, function (Blueprint $table) use ($reference) { + /** @var SchemaBuilder $schema */ + $schema = $this->schema; + + $schema->table($reference->table, function (Blueprint $table) use ($reference) { $table->dropForeign($reference->constraint); }); - $this->schema->table($reference->table, + $schema->table( + $reference->table, function (Blueprint $table) use ($reference, $targetTable, $targetColumn, $type) { $table->{$type}($reference->column)->change(); $table->foreign($reference->column) ->references($targetColumn)->on($targetTable) + ->onUpdate('cascade') ->onDelete('cascade'); - }); + } + ); } } @@ -42,8 +49,10 @@ trait ChangesReferences */ protected function getReferencingTables($table, $column): array { - return $this->schema - ->getConnection() + /** @var SchemaBuilder $schema */ + $schema = $this->schema; + + return $schema->getConnection() ->select( ' SELECT @@ -56,7 +65,7 @@ trait ChangesReferences AND REFERENCED_COLUMN_NAME = ? ', [ - $this->schema->getConnection()->getDatabaseName(), + $schema->getConnection()->getDatabaseName(), $table, $column, ] diff --git a/db/migrations/Reference.php b/db/migrations/Reference.php index 4c35b59b..cc4e4fc9 100644 --- a/db/migrations/Reference.php +++ b/db/migrations/Reference.php @@ -3,16 +3,18 @@ namespace Engelsystem\Migrations; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\ColumnDefinition; trait Reference { /** * @param Blueprint $table * @param bool $setPrimary + * @return ColumnDefinition */ - protected function referencesUser(Blueprint $table, $setPrimary = true) + protected function referencesUser(Blueprint $table, $setPrimary = false) { - $this->references($table, 'users', 'user_id', $setPrimary); + return $this->references($table, 'users', 'user_id', $setPrimary); } /** @@ -20,10 +22,11 @@ trait Reference * @param string $targetTable * @param string $fromColumn * @param bool $setPrimary + * @return ColumnDefinition */ protected function references(Blueprint $table, $targetTable, $fromColumn, $setPrimary = false) { - $table->unsignedInteger($fromColumn); + $definition = $table->unsignedInteger($fromColumn); if ($setPrimary) { $table->primary($fromColumn); @@ -31,6 +34,9 @@ trait Reference $table->foreign($fromColumn) ->references('id')->on($targetTable) + ->onUpdate('cascade') ->onDelete('cascade'); + + return $definition; } } -- cgit v1.2.3-70-g09d2