Skip to content

Commit

Permalink
Created update command form 3.2 to 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Jun 1, 2017
1 parent 3b29e59 commit d9ee6de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/commands/UpgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UpgradeCommand extends Command
*
* @var string
*/
protected $description = 'Creates a migration to upgrade laratrust from version 3.1 to 3.2.';
protected $description = 'Creates a migration to upgrade laratrust from version 3.2 to 3.3.';

/**
* Suffix of the migration name.
Expand Down
69 changes: 26 additions & 43 deletions src/views/generators/upgrade-migration.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,47 @@ class LaratrustUpgradeTables extends Migration
*/
public function up()
{
// Create table for storing groups
Schema::create('{{ $laratrust['groups_table'] }}', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});

Schema::table('{{ $laratrust['role_user_table'] }}', function (Blueprint $table) {
// Drop user foreign key and primary with role_id
$table->dropForeign(['{{ $laratrust['user_foreign_key'] }}']);
// Drop role foreign key and primary key
$table->dropForeign(['{{ $laratrust['role_foreign_key'] }}']);
$table->dropPrimary(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['role_foreign_key'] }}']);

$table->string('user_type');
});
$table->dropPrimary(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['role_foreign_key'] }}', 'user_type']);

DB::table('{{ $laratrust['role_user_table'] }}')->update(['user_type' => '{{ get_class($user) }}']);
// Add {{ $laratrust['group_foreign_key'] }} column
$table->integer('{{ $laratrust['group_foreign_key'] }}')->unsigned()->nullable();

Schema::table('{{ $laratrust['role_user_table'] }}', function (Blueprint $table) {
// Create foreign keys
$table->foreign('{{ $laratrust['role_foreign_key'] }}')->references('id')->on('{{ $laratrust['roles_table'] }}')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['role_foreign_key'] }}', 'user_type']);
});

$table->foreign('{{ $laratrust['group_foreign_key'] }}')->references('id')->on('{{ $laratrust['groups_table'] }}')
->onUpdate('cascade')->onDelete('cascade');

// Create a unique key
$table->unique(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['role_foreign_key'] }}', 'user_type', '{{ $laratrust['group_foreign_key'] }}']);
});

Schema::table('{{ $laratrust['permission_user_table'] }}', function (Blueprint $table) {
// Drop user foreign key and primary with permission_id
$table->dropForeign(['{{ $laratrust['user_foreign_key'] }}']);
// Drop permission foreign key and primary key
$table->dropForeign(['{{ $laratrust['permission_foreign_key'] }}']);
$table->dropPrimary(['{{ $laratrust['permission_foreign_key'] }}', '{{ $laratrust['user_foreign_key'] }}']);

$table->string('user_type');
});
$table->dropPrimary(['{{ $laratrust['permission_foreign_key'] }}', '{{ $laratrust['user_foreign_key'] }}', 'user_type']);

DB::table('{{ $laratrust['permission_user_table'] }}')->update(['user_type' => '{{ get_class($user) }}']);
// Add {{ $laratrust['group_foreign_key'] }} column
$table->integer('{{ $laratrust['group_foreign_key'] }}')->unsigned()->nullable();

Schema::table('{{ $laratrust['permission_user_table'] }}', function (Blueprint $table) {
$table->foreign('{{ $laratrust['permission_foreign_key'] }}')->references('id')->on('{{ $laratrust['permissions_table'] }}')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['{{ $laratrust['permission_foreign_key'] }}', '{{ $laratrust['user_foreign_key'] }}', 'user_type']);
$table->foreign('{{ $laratrust['group_foreign_key'] }}')->references('id')->on('{{ $laratrust['groups_table'] }}')
->onUpdate('cascade')->onDelete('cascade');

$table->unique(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['permission_foreign_key'] }}', 'user_type', '{{ $laratrust['group_foreign_key'] }}']);
});
}

Expand All @@ -57,28 +63,5 @@ public function up()
*/
public function down()
{
Schema::table('{{ $laratrust['role_user_table'] }}', function (Blueprint $table) {
$table->dropForeign(['{{ $laratrust['role_foreign_key'] }}']);
$table->dropPrimary(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['role_foreign_key'] }}', 'user_type']);
$table->dropColumn('user_type');

$table->foreign('{{ $laratrust['user_foreign_key'] }}')->references('{{ $user->getKeyName() }}')->on('{{ $user->getTable() }}')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('{{ $laratrust['role_foreign_key'] }}')->references('id')->on('{{ $laratrust['roles_table'] }}')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['{{ $laratrust['user_foreign_key'] }}', '{{ $laratrust['role_foreign_key'] }}']);
});

Schema::table('{{ $laratrust['permission_user_table'] }}', function (Blueprint $table) {
$table->dropForeign(['{{ $laratrust['permission_foreign_key'] }}']);
$table->dropPrimary(['{{ $laratrust['permission_foreign_key'] }}', '{{ $laratrust['user_foreign_key'] }}', 'user_type']);
$table->dropColumn('user_type');

$table->foreign('{{ $laratrust['user_foreign_key'] }}')->references('{{ $user->getKeyName() }}')->on('{{ $user->getTable() }}')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('{{ $laratrust['permission_foreign_key'] }}')->references('id')->on('{{ $laratrust['permissions_table'] }}')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['{{ $laratrust['permission_foreign_key'] }}', '{{ $laratrust['user_foreign_key'] }}']);
});
}
}

0 comments on commit d9ee6de

Please sign in to comment.