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