3 use Illuminate\Support\Facades\Schema;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Database\Migrations\Migration;
7 class CreateBookshelvesTable extends Migration
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) {}
28 // Here we have table drops before the creations due to upgrade issues
29 // people were having due to the bookshelves_books table creation failing.
30 if (Schema::hasTable('bookshelves_books')) {
31 Schema::drop('bookshelves_books');
34 if (Schema::hasTable('bookshelves')) {
35 Schema::drop('bookshelves');
38 Schema::create('bookshelves', function (Blueprint $table) {
39 $table->increments('id');
40 $table->string('name', 180);
41 $table->string('slug', 180);
42 $table->text('description');
43 $table->integer('created_by')->nullable()->default(null);
44 $table->integer('updated_by')->nullable()->default(null);
45 $table->boolean('restricted')->default(false);
46 $table->integer('image_id')->nullable()->default(null);
49 $table->index('slug');
50 $table->index('created_by');
51 $table->index('updated_by');
52 $table->index('restricted');
55 Schema::create('bookshelves_books', function (Blueprint $table) {
56 $table->integer('bookshelf_id')->unsigned();
57 $table->integer('book_id')->unsigned();
58 $table->integer('order')->unsigned();
60 $table->primary(['bookshelf_id', 'book_id']);
62 $table->foreign('bookshelf_id')->references('id')->on('bookshelves')
63 ->onUpdate('cascade')->onDelete('cascade');
64 $table->foreign('book_id')->references('id')->on('books')
65 ->onUpdate('cascade')->onDelete('cascade');
68 // Delete old bookshelf permissions
69 // Needed to to issues upon upgrade.
70 DB::table('role_permissions')->where('name', 'like', 'bookshelf-%')->delete();
72 // Copy existing role permissions from Books
73 $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
74 foreach ($ops as $op) {
75 $dbOpName = strtolower(str_replace(' ', '-', $op));
76 $roleIdsWithBookPermission = DB::table('role_permissions')
77 ->leftJoin('permission_role', 'role_permissions.id', '=', 'permission_role.permission_id')
78 ->leftJoin('roles', 'roles.id', '=', 'permission_role.role_id')
79 ->where('role_permissions.name', '=', 'book-' . $dbOpName)->get(['roles.id'])->pluck('id');
81 $permId = DB::table('role_permissions')->insertGetId([
82 'name' => 'bookshelf-' . $dbOpName,
83 'display_name' => $op . ' ' . 'BookShelves',
84 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
85 'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
88 $rowsToInsert = $roleIdsWithBookPermission->filter(function($roleId) {
89 return !is_null($roleId);
90 })->map(function($roleId) use ($permId) {
93 'permission_id' => $permId
97 // Assign view permission to all current roles
98 DB::table('permission_role')->insert($rowsToInsert);
103 * Reverse the migrations.
107 public function down()
109 // Drop created permissions
110 $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'];
112 $permissionIds = DB::table('role_permissions')->whereIn('name', $ops)
113 ->get(['id'])->pluck('id')->toArray();
114 DB::table('permission_role')->whereIn('permission_id', $permissionIds)->delete();
115 DB::table('role_permissions')->whereIn('id', $permissionIds)->delete();
117 // Drop shelves table
118 Schema::dropIfExists('bookshelves_books');
119 Schema::dropIfExists('bookshelves');
121 // Drop related polymorphic items
122 DB::table('activities')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
123 DB::table('views')->where('viewable_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
124 DB::table('entity_permissions')->where('restrictable_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
125 DB::table('tags')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
126 DB::table('search_terms')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();
127 DB::table('comments')->where('entity_type', '=', 'BookStack\Entities\Models\Bookshelf')->delete();