laravel-roles/database/migrations/create_blax_role_tables.php...

91 lines
3.3 KiB
Plaintext
Raw Normal View History

2025-06-15 09:56:28 +00:00
<?php
namespace Blax\Roles\Migrations;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
2025-06-15 16:29:50 +00:00
// Permission
2025-06-16 05:37:35 +00:00
Schema::create(config('roles.table_names.permissions'), function (Blueprint $table) {
2025-06-15 09:56:28 +00:00
$table->id();
2025-06-16 06:31:14 +00:00
$table->string('slug')->unique();
2025-06-15 09:56:28 +00:00
$table->string('description')->nullable();
$table->timestamps();
});
2025-06-16 07:49:36 +00:00
// PermissionMember
Schema::create(config('roles.table_names.permission_members'), function (Blueprint $table) {
$table->id();
$table->foreignId('permission_id')->constrained('permissions')->onDelete('cascade');
$table->morphs('member');
$table->json('context')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});
2025-06-15 16:29:50 +00:00
// PermissionUsage
2025-06-16 05:37:35 +00:00
Schema::create(config('roles.table_names.permission_usage'), function (Blueprint $table) {
2025-06-15 09:56:28 +00:00
$table->id();
2025-06-15 16:29:50 +00:00
$table->foreignId('permission_id')->constrained('permissions')->onDelete('cascade');
2025-06-16 07:49:36 +00:00
$table->float('usage', 8)->default(1);
2025-06-15 16:29:50 +00:00
$table->morphs('user');
$table->json('context')->nullable();
$table->timestamps();
});
2025-06-16 07:49:36 +00:00
2025-06-15 16:29:50 +00:00
// Role
2025-06-16 05:37:35 +00:00
Schema::create(config('roles.table_names.roles'), function (Blueprint $table) {
2025-06-15 16:29:50 +00:00
$table->id();
$table->foreignId('parent_id')
->nullable()
->constrained('roles')
->onDelete('set null');
2025-06-16 06:31:14 +00:00
$table->string('name')->nullable();
$table->string('slug')->unique();
2025-06-15 09:56:28 +00:00
$table->string('description')->nullable();
$table->timestamps();
});
2025-06-15 16:29:50 +00:00
// RoleMember
2025-06-16 06:01:11 +00:00
Schema::create(config('roles.table_names.role_member'), function (Blueprint $table) {
2025-06-15 16:29:50 +00:00
$table->id();
$table->foreignId('role_id')->constrained('roles')->onDelete('cascade');
$table->morphs('member');
$table->json('context')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});
// RolePermission
2025-06-16 05:37:35 +00:00
Schema::create(config('roles.table_names.role_permission'), function (Blueprint $table) {
2025-06-15 16:29:50 +00:00
$table->id();
$table->foreignId('role_id')->constrained('roles')->onDelete('cascade');
$table->foreignId('permission_id')->constrained('permissions')->onDelete('cascade');
$table->json('context')->nullable();
2025-06-15 09:56:28 +00:00
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
2025-06-16 05:37:35 +00:00
Schema::dropIfExists(config('roles.table_names.role_permission'));
Schema::dropIfExists(config('roles.table_names.role_members'));
Schema::dropIfExists(config('roles.table_names.roles'));
Schema::dropIfExists(config('roles.table_names.permission_usage'));
2025-06-16 07:49:36 +00:00
Schema::dropIfExists(config('roles.table_names.permission_members'));
2025-06-16 05:37:35 +00:00
Schema::dropIfExists(config('roles.table_names.permissions'));
2025-06-15 09:56:28 +00:00
}
};