]> BookStack Code Mirror - bookstack/blob - database/migrations/2023_01_24_104625_refactor_joint_permissions_storage.php
53994c52b57ef5f56f1d63a5ae7ce7c9590011f4
[bookstack] / database / migrations / 2023_01_24_104625_refactor_joint_permissions_storage.php
1 <?php
2
3 use BookStack\Permissions\JointPermissionBuilder;
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      * @return void
15      */
16     public function up()
17     {
18         // Truncate before schema changes to avoid performance issues
19         // since we'll need to rebuild anyway.
20         DB::table('joint_permissions')->truncate();
21
22         if (Schema::hasColumn('joint_permissions', 'owned_by')) {
23             Schema::table('joint_permissions', function (Blueprint $table) {
24                 $table->dropColumn(['has_permission', 'has_permission_own', 'owned_by']);
25
26                 $table->unsignedTinyInteger('status')->index();
27                 $table->unsignedInteger('owner_id')->nullable()->index();
28             });
29         }
30
31         // Rebuild permissions
32         app(JointPermissionBuilder::class)->rebuildForAll();
33     }
34
35     /**
36      * Reverse the migrations.
37      *
38      * @return void
39      */
40     public function down()
41     {
42         DB::table('joint_permissions')->truncate();
43
44         Schema::table('joint_permissions', function (Blueprint $table) {
45             $table->dropColumn(['status', 'owner_id']);
46
47             $table->boolean('has_permission')->index();
48             $table->boolean('has_permission_own')->index();
49             $table->unsignedInteger('owned_by')->index();
50         });
51     }
52 };