4 use Illuminate\Database\Migrations\Migration;
5 use Illuminate\Database\Schema\Blueprint;
6 use Illuminate\Support\Facades\DB;
7 use Illuminate\Support\Facades\Schema;
9 return new class extends Migration
14 public function up(): void
17 // Convert the existing entity tables to InnoDB.
18 // Wrapped in try-catch just in the event a different database system is used
19 // which does not support InnoDB but does support all required features
20 // like foreign key references.
22 $prefix = DB::getTablePrefix();
23 DB::statement("ALTER TABLE {$prefix}pages ENGINE = InnoDB;");
24 DB::statement("ALTER TABLE {$prefix}chapters ENGINE = InnoDB;");
25 DB::statement("ALTER TABLE {$prefix}books ENGINE = InnoDB;");
26 } catch (Exception $exception) {
29 // Here we have table drops before the creations due to upgrade issues
30 // people were having due to the bookshelves_books table creation failing.
31 if (Schema::hasTable('bookshelves_books')) {
32 Schema::drop('bookshelves_books');
35 if (Schema::hasTable('bookshelves')) {
36 Schema::drop('bookshelves');
39 Schema::create('bookshelves', function (Blueprint $table) {
40 $table->increments('id');
41 $table->string('name', 180);
42 $table->string('slug', 180);
43 $table->text('description');
44 $table->integer('created_by')->nullable()->default(null);
45 $table->integer('updated_by')->nullable()->default(null);
46 $table->boolean('restricted')->default(false);
47 $table->integer('image_id')->nullable()->default(null);
50 $table->index('slug');
51 $table->index('created_by');
52 $table->index('updated_by');
53 $table->index('restricted');
56 Schema::create('bookshelves_books', function (Blueprint $table) {
57 $table->integer('bookshelf_id')->unsigned();
58 $table->integer('book_id')->unsigned();
59 $table->integer('order')->unsigned();
61 $table->primary(['bookshelf_id', 'book_id']);
63 $table->foreign('bookshelf_id')->references('id')->on('bookshelves')
64 ->onUpdate('cascade')->onDelete('cascade');
65 $table->foreign('book_id')->references('id')->on('books')
66 ->onUpdate('cascade')->onDelete('cascade');
69 // Delete old bookshelf permissions
70 // Needed to to issues upon upgrade.
71 DB::table('role_permissions')->where('name', 'like', 'bookshelf-%')->delete();
73 // Copy existing role permissions from Books
74 $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
75 foreach ($ops as $op) {
76 $dbOpName = strtolower(str_replace(' ', '-', $op));
77 $roleIdsWithBookPermission = DB::table('role_permissions')
78 ->leftJoin('permission_role', 'role_permissions.id', '=', 'permission_role.permission_id')
79 ->leftJoin('roles', 'roles.id', '=', 'permission_role.role_id')
80 ->where('role_permissions.name', '=', 'book-' . $dbOpName)->get(['roles.id'])->pluck('id');
82 $permId = DB::table('role_permissions')->insertGetId([
83 'name' => 'bookshelf-' . $dbOpName,
84 'display_name' => $op . ' ' . 'BookShelves',
85 'created_at' => Carbon::now()->toDateTimeString(),
86 'updated_at' => Carbon::now()->toDateTimeString(),
89 $rowsToInsert = $roleIdsWithBookPermission->filter(function ($roleId) {
90 return !is_null($roleId);
91 })->map(function ($roleId) use ($permId) {
94 'permission_id' => $permId,
98 // Assign view permission to all current roles
99 DB::table('permission_role')->insert($rowsToInsert);
104 * Reverse the migrations.
106 public function down(): void
108 // Drop created permissions
109 $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'];
111 $permissionIds = DB::table('role_permissions')->whereIn('name', $ops)
112 ->get(['id'])->pluck('id')->toArray();
113 DB::table('permission_role')->whereIn('permission_id', $permissionIds)->delete();
114 DB::table('role_permissions')->whereIn('id', $permissionIds)->delete();
116 // Drop shelves table
117 Schema::dropIfExists('bookshelves_books');
118 Schema::dropIfExists('bookshelves');
120 // Drop related polymorphic items
121 DB::table('activities')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
122 DB::table('views')->where('viewable_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
123 DB::table('entity_permissions')->where('restrictable_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
124 DB::table('tags')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
125 DB::table('search_terms')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
126 DB::table('comments')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();