| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Schema; | 
					
						
							|  |  |  | use Illuminate\Database\Schema\Blueprint; | 
					
						
							|  |  |  | use Illuminate\Database\Migrations\Migration; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CreateBookshelvesTable extends Migration | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Run the migrations. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function up() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-09-24 22:58:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Convert the existing entity tables to InnoDB.
 | 
					
						
							|  |  |  |         // Wrapped in try-catch just in the event a different database system is used
 | 
					
						
							|  |  |  |         // which does not support InnoDB but does support all required features
 | 
					
						
							|  |  |  |         // like foreign key references.
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $prefix = DB::getTablePrefix(); | 
					
						
							|  |  |  |             DB::statement("ALTER TABLE {$prefix}pages ENGINE = InnoDB;"); | 
					
						
							|  |  |  |             DB::statement("ALTER TABLE {$prefix}chapters ENGINE = InnoDB;"); | 
					
						
							|  |  |  |             DB::statement("ALTER TABLE {$prefix}books ENGINE = InnoDB;"); | 
					
						
							|  |  |  |         } catch (Exception $exception) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Here we have table drops before the creations due to upgrade issues
 | 
					
						
							|  |  |  |         // people were having due to the bookshelves_books table creation failing.
 | 
					
						
							|  |  |  |         if (Schema::hasTable('bookshelves_books')) { | 
					
						
							|  |  |  |             Schema::drop('bookshelves_books'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (Schema::hasTable('bookshelves')) { | 
					
						
							|  |  |  |             Schema::drop('bookshelves'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  |         Schema::create('bookshelves', function (Blueprint $table) { | 
					
						
							|  |  |  |             $table->increments('id'); | 
					
						
							|  |  |  |             $table->string('name', 200); | 
					
						
							|  |  |  |             $table->string('slug', 200); | 
					
						
							|  |  |  |             $table->text('description'); | 
					
						
							|  |  |  |             $table->integer('created_by')->nullable()->default(null); | 
					
						
							|  |  |  |             $table->integer('updated_by')->nullable()->default(null); | 
					
						
							|  |  |  |             $table->boolean('restricted')->default(false); | 
					
						
							|  |  |  |             $table->integer('image_id')->nullable()->default(null); | 
					
						
							|  |  |  |             $table->timestamps(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $table->index('slug'); | 
					
						
							|  |  |  |             $table->index('created_by'); | 
					
						
							|  |  |  |             $table->index('updated_by'); | 
					
						
							|  |  |  |             $table->index('restricted'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         Schema::create('bookshelves_books', function (Blueprint $table) { | 
					
						
							|  |  |  |             $table->integer('bookshelf_id')->unsigned(); | 
					
						
							|  |  |  |             $table->integer('book_id')->unsigned(); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |             $table->integer('order')->unsigned(); | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 22:58:40 +08:00
										 |  |  |             $table->primary(['bookshelf_id', 'book_id']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |             $table->foreign('bookshelf_id')->references('id')->on('bookshelves') | 
					
						
							|  |  |  |                 ->onUpdate('cascade')->onDelete('cascade'); | 
					
						
							|  |  |  |             $table->foreign('book_id')->references('id')->on('books') | 
					
						
							|  |  |  |                 ->onUpdate('cascade')->onDelete('cascade'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 23:30:08 +08:00
										 |  |  |         // Delete old bookshelf permissions
 | 
					
						
							|  |  |  |         // Needed to to issues upon upgrade.
 | 
					
						
							|  |  |  |         DB::table('role_permissions')->where('name', 'like', 'bookshelf-%')->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-04 19:45:45 +08:00
										 |  |  |         // Copy existing role permissions from Books
 | 
					
						
							| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  |         $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own']; | 
					
						
							|  |  |  |         foreach ($ops as $op) { | 
					
						
							| 
									
										
										
										
											2018-08-04 19:45:45 +08:00
										 |  |  |             $dbOpName = strtolower(str_replace(' ', '-', $op)); | 
					
						
							|  |  |  |             $roleIdsWithBookPermission = DB::table('role_permissions') | 
					
						
							|  |  |  |                 ->leftJoin('permission_role', 'role_permissions.id', '=', 'permission_role.permission_id') | 
					
						
							|  |  |  |                 ->leftJoin('roles', 'roles.id', '=', 'permission_role.role_id') | 
					
						
							|  |  |  |                 ->where('role_permissions.name', '=', 'book-' . $dbOpName)->get(['roles.id'])->pluck('id'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $permId = DB::table('role_permissions')->insertGetId([ | 
					
						
							|  |  |  |                 'name' => 'bookshelf-' . $dbOpName, | 
					
						
							| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  |                 'display_name' => $op . ' ' . 'BookShelves', | 
					
						
							|  |  |  |                 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), | 
					
						
							|  |  |  |                 'updated_at' => \Carbon\Carbon::now()->toDateTimeString() | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2018-08-04 19:45:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 23:30:08 +08:00
										 |  |  |             $rowsToInsert = $roleIdsWithBookPermission->filter(function($roleId) { | 
					
						
							|  |  |  |                 return !is_null($roleId); | 
					
						
							|  |  |  |             })->map(function($roleId) use ($permId) { | 
					
						
							| 
									
										
										
										
											2018-08-04 19:45:45 +08:00
										 |  |  |                 return [ | 
					
						
							|  |  |  |                     'role_id' => $roleId, | 
					
						
							| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  |                     'permission_id' => $permId | 
					
						
							| 
									
										
										
										
											2018-08-04 19:45:45 +08:00
										 |  |  |                 ]; | 
					
						
							|  |  |  |             })->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Assign view permission to all current roles
 | 
					
						
							|  |  |  |             DB::table('permission_role')->insert($rowsToInsert); | 
					
						
							| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Reverse the migrations. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function down() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-04 19:45:45 +08:00
										 |  |  |         // Drop created permissions
 | 
					
						
							|  |  |  |         $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']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $permissionIds = DB::table('role_permissions')->whereIn('name', $ops) | 
					
						
							|  |  |  |             ->get(['id'])->pluck('id')->toArray(); | 
					
						
							|  |  |  |         DB::table('permission_role')->whereIn('permission_id', $permissionIds)->delete(); | 
					
						
							|  |  |  |         DB::table('role_permissions')->whereIn('id', $permissionIds)->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Drop shelves table
 | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         Schema::dropIfExists('bookshelves_books'); | 
					
						
							| 
									
										
										
										
											2018-09-17 02:34:09 +08:00
										 |  |  |         Schema::dropIfExists('bookshelves'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Drop related polymorphic items
 | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |         DB::table('activities')->where('entity_type', '=', 'BookStack\Entities\Bookshelf')->delete(); | 
					
						
							|  |  |  |         DB::table('views')->where('viewable_type', '=', 'BookStack\Entities\Bookshelf')->delete(); | 
					
						
							|  |  |  |         DB::table('entity_permissions')->where('restrictable_type', '=', 'BookStack\Entities\Bookshelf')->delete(); | 
					
						
							|  |  |  |         DB::table('tags')->where('entity_type', '=', 'BookStack\Entities\Bookshelf')->delete(); | 
					
						
							|  |  |  |         DB::table('search_terms')->where('entity_type', '=', 'BookStack\Entities\Bookshelf')->delete(); | 
					
						
							|  |  |  |         DB::table('comments')->where('entity_type', '=', 'BookStack\Entities\Bookshelf')->delete(); | 
					
						
							| 
									
										
										
										
											2018-06-24 20:38:19 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |