]> BookStack Code Mirror - bookstack/blob - database/migrations/2018_08_04_115700_create_bookshelves_table.php
Updated migration to convert MyISAM tables to InnoDB
[bookstack] / database / migrations / 2018_08_04_115700_create_bookshelves_table.php
1 <?php
2
3 use Illuminate\Support\Facades\Schema;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Database\Migrations\Migration;
6
7 class CreateBookshelvesTable extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
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         // 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');
32         }
33
34         if (Schema::hasTable('bookshelves')) {
35             Schema::drop('bookshelves');
36         }
37
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);
47             $table->timestamps();
48
49             $table->index('slug');
50             $table->index('created_by');
51             $table->index('updated_by');
52             $table->index('restricted');
53         });
54
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();
59
60             $table->primary(['bookshelf_id', 'book_id']);
61
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');
66         });
67
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');
76
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()
82             ]);
83
84             $rowsToInsert = $roleIdsWithBookPermission->map(function($roleId) use ($permId) {
85                 return [
86                     'role_id' => $roleId,
87                     'permission_id' => $permId
88                 ];
89             })->toArray();
90
91             // Assign view permission to all current roles
92             DB::table('permission_role')->insert($rowsToInsert);
93         }
94     }
95
96     /**
97      * Reverse the migrations.
98      *
99      * @return void
100      */
101     public function down()
102     {
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'];
105
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();
110
111         // Drop shelves table
112         Schema::dropIfExists('bookshelves_books');
113         Schema::dropIfExists('bookshelves');
114
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();
122     }
123 }