]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_08_01_130541_create_comments_table.php
1d69d1fa782ce47b80130cf35670418023755741
[bookstack] / database / migrations / 2017_08_01_130541_create_comments_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 CreateCommentsTable extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
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\Carbon::now()->toDateTimeString(),
41                     'updated_at' => \Carbon\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     /**
53      * Reverse the migrations.
54      *
55      * @return void
56      */
57     public function down()
58     {
59         Schema::dropIfExists('comments');
60         // Delete comment role permissions
61         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
62         $entity = 'Comment';
63         foreach ($ops as $op) {
64             $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
65             DB::table('role_permissions')->where('name', '=', $permName)->delete();
66         }
67     }
68 }