]> BookStack Code Mirror - bookstack/blob - database/migrations/2018_08_04_115700_create_bookshelves_table.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / database / migrations / 2018_08_04_115700_create_bookshelves_table.php
1 <?php
2
3 use Carbon\Carbon;
4 use Illuminate\Database\Migrations\Migration;
5 use Illuminate\Database\Schema\Blueprint;
6 use Illuminate\Support\Facades\DB;
7 use Illuminate\Support\Facades\Schema;
8
9 return new class extends Migration
10 {
11     /**
12      * Run the migrations.
13      */
14     public function up(): void
15     {
16
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.
21         try {
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) {
27         }
28
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');
33         }
34
35         if (Schema::hasTable('bookshelves')) {
36             Schema::drop('bookshelves');
37         }
38
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);
48             $table->timestamps();
49
50             $table->index('slug');
51             $table->index('created_by');
52             $table->index('updated_by');
53             $table->index('restricted');
54         });
55
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();
60
61             $table->primary(['bookshelf_id', 'book_id']);
62
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');
67         });
68
69         // Delete old bookshelf permissions
70         // Needed to to issues upon upgrade.
71         DB::table('role_permissions')->where('name', 'like', 'bookshelf-%')->delete();
72
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');
81
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(),
87             ]);
88
89             $rowsToInsert = $roleIdsWithBookPermission->filter(function ($roleId) {
90                 return !is_null($roleId);
91             })->map(function ($roleId) use ($permId) {
92                 return [
93                     'role_id'       => $roleId,
94                     'permission_id' => $permId,
95                 ];
96             })->toArray();
97
98             // Assign view permission to all current roles
99             DB::table('permission_role')->insert($rowsToInsert);
100         }
101     }
102
103     /**
104      * Reverse the migrations.
105      */
106     public function down(): void
107     {
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'];
110
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();
115
116         // Drop shelves table
117         Schema::dropIfExists('bookshelves_books');
118         Schema::dropIfExists('bookshelves');
119
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();
127     }
128 };