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', 200);
41 $table->string('slug', 200);
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 // Copy existing role permissions from Books
69 $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
70 foreach ($ops as $op) {
71 $dbOpName = strtolower(str_replace(' ', '-', $op));
72 $roleIdsWithBookPermission = DB::table('role_permissions')
73 ->leftJoin('permission_role', 'role_permissions.id', '=', 'permission_role.permission_id')
74 ->leftJoin('roles', 'roles.id', '=', 'permission_role.role_id')
75 ->where('role_permissions.name', '=', 'book-' . $dbOpName)->get(['roles.id'])->pluck('id');
77 $permId = DB::table('role_permissions')->insertGetId([
78 'name' => 'bookshelf-' . $dbOpName,
79 'display_name' => $op . ' ' . 'BookShelves',
80 'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
81 'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
84 $rowsToInsert = $roleIdsWithBookPermission->map(function($roleId) use ($permId) {
87 'permission_id' => $permId
91 // Assign view permission to all current roles
92 DB::table('permission_role')->insert($rowsToInsert);
97 * Reverse the migrations.
101 public function down()
103 // Drop created permissions
104 $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'];
106 $permissionIds = DB::table('role_permissions')->whereIn('name', $ops)
107 ->get(['id'])->pluck('id')->toArray();
108 DB::table('permission_role')->whereIn('permission_id', $permissionIds)->delete();
109 DB::table('role_permissions')->whereIn('id', $permissionIds)->delete();
111 // Drop shelves table
112 Schema::dropIfExists('bookshelves_books');
113 Schema::dropIfExists('bookshelves');
115 // Drop related polymorphic items
116 DB::table('activities')->where('entity_type', '=', 'BookStack\Bookshelf')->delete();
117 DB::table('views')->where('viewable_type', '=', 'BookStack\Bookshelf')->delete();
118 DB::table('entity_permissions')->where('restrictable_type', '=', 'BookStack\Bookshelf')->delete();
119 DB::table('tags')->where('entity_type', '=', 'BookStack\Bookshelf')->delete();
120 DB::table('search_terms')->where('entity_type', '=', 'BookStack\Bookshelf')->delete();
121 DB::table('comments')->where('entity_type', '=', 'BookStack\Bookshelf')->delete();