3 use Illuminate\Support\Facades\Schema;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Database\Migrations\Migration;
7 class CreateBookshelvesTable extends Migration
16 Schema::create('bookshelves', function (Blueprint $table) {
17 $table->increments('id');
18 $table->string('name', 200);
19 $table->string('slug', 200);
20 $table->text('description');
21 $table->integer('created_by')->nullable()->default(null);
22 $table->integer('updated_by')->nullable()->default(null);
23 $table->boolean('restricted')->default(false);
24 $table->integer('image_id')->nullable()->default(null);
27 $table->index('slug');
28 $table->index('created_by');
29 $table->index('updated_by');
30 $table->index('restricted');
33 // Copy existing role permissions from Books
34 $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
35 foreach ($ops as $op) {
36 $dbOpName = strtolower(str_replace(' ', '-', $op));
37 $roleIdsWithBookPermission = DB::table('role_permissions')
38 ->leftJoin('permission_role', 'role_permissions.id', '=', 'permission_role.permission_id')
39 ->leftJoin('roles', 'roles.id', '=', 'permission_role.role_id')
40 ->where('role_permissions.name', '=', 'book-' . $dbOpName)->get(['roles.id'])->pluck('id');
42 $permId = DB::table('role_permissions')->insertGetId([
43 'name' => 'bookshelf-' . $dbOpName,
44 'display_name' => $op . ' ' . 'BookShelves',
45 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
46 'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
49 $rowsToInsert = $roleIdsWithBookPermission->map(function($roleId) use ($permId) {
52 'permission_id' => $permId
56 // Assign view permission to all current roles
57 DB::table('permission_role')->insert($rowsToInsert);
62 * Reverse the migrations.
66 public function down()
68 // Drop created permissions
69 $ops = ['bookshelf-create-all','bookshelf-create-own','bookshelf-delete-all','bookshelf-delete-own','bookshelf-update-all','bookshelf-update-own','bookshelf-view-all','bookshelf-view-own'];
71 $permissionIds = DB::table('role_permissions')->whereIn('name', $ops)
72 ->get(['id'])->pluck('id')->toArray();
73 DB::table('permission_role')->whereIn('permission_id', $permissionIds)->delete();
74 DB::table('role_permissions')->whereIn('id', $permissionIds)->delete();
77 Schema::dropIfExists('bookshelves');