]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_08_01_130541_create_comments_table.php
Migrations: Updated with type hints instead of php doc
[bookstack] / database / migrations / 2017_08_01_130541_create_comments_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         Schema::create('comments', function (Blueprint $table) {
17             $table->increments('id')->unsigned();
18             $table->integer('entity_id')->unsigned();
19             $table->string('entity_type');
20             $table->longText('text')->nullable();
21             $table->longText('html')->nullable();
22             $table->integer('parent_id')->unsigned()->nullable();
23             $table->integer('local_id')->unsigned()->nullable();
24             $table->integer('created_by')->unsigned();
25             $table->integer('updated_by')->unsigned()->nullable();
26             $table->timestamps();
27
28             $table->index(['entity_id', 'entity_type']);
29             $table->index(['local_id']);
30
31             // Assign new comment permissions to admin role
32             $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id;
33             // Create & attach new entity permissions
34             $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
35             $entity = 'Comment';
36             foreach ($ops as $op) {
37                 $permissionId = DB::table('role_permissions')->insertGetId([
38                     'name'         => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
39                     'display_name' => $op . ' ' . $entity . 's',
40                     'created_at'   => Carbon::now()->toDateTimeString(),
41                     'updated_at'   => Carbon::now()->toDateTimeString(),
42                 ]);
43                 DB::table('permission_role')->insert([
44                     'role_id'       => $adminRoleId,
45                     'permission_id' => $permissionId,
46                 ]);
47             }
48         });
49     }
50
51     /**
52      * Reverse the migrations.
53      */
54     public function down(): void
55     {
56         Schema::dropIfExists('comments');
57         // Delete comment role permissions
58         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
59         $entity = 'Comment';
60         foreach ($ops as $op) {
61             $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
62             DB::table('role_permissions')->where('name', '=', $permName)->delete();
63         }
64     }
65 };