|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Hyperf\Database\Schema\Blueprint; |
| 6 | +use Hypervel\Database\Migrations\Migration; |
| 7 | +use Hypervel\Support\Facades\Schema; |
| 8 | + |
| 9 | +use function Hypervel\Config\config; |
| 10 | + |
| 11 | +return new class extends Migration { |
| 12 | + /** |
| 13 | + * Get the migration connection name. |
| 14 | + */ |
| 15 | + public function getConnection(): string |
| 16 | + { |
| 17 | + return config('permission.storage.database.connection') |
| 18 | + ?: parent::getConnection(); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Run the migrations. |
| 23 | + */ |
| 24 | + public function up(): void |
| 25 | + { |
| 26 | + $schema = Schema::connection($this->getConnection()); |
| 27 | + |
| 28 | + $schema->create('roles', function (Blueprint $table) { |
| 29 | + $table->bigIncrements('id'); |
| 30 | + $table->string('name')->unique(); |
| 31 | + $table->string('guard_name'); |
| 32 | + $table->timestamps(); |
| 33 | + |
| 34 | + $table->index(['name', 'guard_name']); |
| 35 | + }); |
| 36 | + |
| 37 | + $schema->create('permissions', function (Blueprint $table) { |
| 38 | + $table->bigIncrements('id'); |
| 39 | + $table->string('name')->unique(); |
| 40 | + $table->string('guard_name'); |
| 41 | + $table->timestamps(); |
| 42 | + $table->index(['name', 'guard_name']); |
| 43 | + }); |
| 44 | + $schema->create('role_has_permissions', function (Blueprint $table) { |
| 45 | + $table->unsignedBigInteger('permission_id'); |
| 46 | + $table->unsignedBigInteger('role_id'); |
| 47 | + $table->boolean('is_forbidden'); |
| 48 | + $table->timestamps(); |
| 49 | + |
| 50 | + $table->primary(['permission_id', 'role_id']); |
| 51 | + $table->index('role_id'); |
| 52 | + $table->index('permission_id'); |
| 53 | + }); |
| 54 | + |
| 55 | + $schema->create('owner_has_permissions', function (Blueprint $table) { |
| 56 | + $table->unsignedBigInteger('permission_id'); |
| 57 | + $table->morphs('owner'); |
| 58 | + $table->boolean('is_forbidden'); |
| 59 | + $table->timestamps(); |
| 60 | + |
| 61 | + $table->primary(['permission_id', 'owner_id', 'owner_type']); |
| 62 | + $table->index('owner_id'); |
| 63 | + $table->index('permission_id'); |
| 64 | + }); |
| 65 | + |
| 66 | + $schema->create('owner_has_roles', function (Blueprint $table) { |
| 67 | + $table->unsignedBigInteger('role_id'); |
| 68 | + $table->morphs('owner'); |
| 69 | + $table->timestamps(); |
| 70 | + |
| 71 | + $table->primary(['role_id', 'owner_id', 'owner_type']); |
| 72 | + $table->index('owner_id'); |
| 73 | + $table->index('role_id'); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Reverse the migrations. |
| 79 | + */ |
| 80 | + public function down(): void |
| 81 | + { |
| 82 | + $schema = Schema::connection($this->getConnection()); |
| 83 | + $schema->dropIfExists('owner_has_roles'); |
| 84 | + $schema->dropIfExists('owner_has_permissions'); |
| 85 | + $schema->dropIfExists('role_has_permissions'); |
| 86 | + $schema->dropIfExists('permissions'); |
| 87 | + $schema->dropIfExists('roles'); |
| 88 | + } |
| 89 | +}; |
0 commit comments