]> BookStack Code Mirror - bookstack/blob - database/migrations/2016_10_09_142037_create_attachments_table.php
Migrations: Updated with type hints instead of php doc
[bookstack] / database / migrations / 2016_10_09_142037_create_attachments_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('attachments', function (Blueprint $table) {
17             $table->increments('id');
18             $table->string('name');
19             $table->string('path');
20             $table->string('extension', 20);
21             $table->integer('uploaded_to');
22
23             $table->boolean('external');
24             $table->integer('order');
25
26             $table->integer('created_by');
27             $table->integer('updated_by');
28
29             $table->index('uploaded_to');
30             $table->timestamps();
31         });
32
33         // Get roles with permissions we need to change
34         $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id;
35
36         // Create & attach new entity permissions
37         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
38         $entity = 'Attachment';
39         foreach ($ops as $op) {
40             $permissionId = DB::table('role_permissions')->insertGetId([
41                 'name'         => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
42                 'display_name' => $op . ' ' . $entity . 's',
43                 'created_at'   => Carbon::now()->toDateTimeString(),
44                 'updated_at'   => Carbon::now()->toDateTimeString(),
45             ]);
46             DB::table('permission_role')->insert([
47                 'role_id'       => $adminRoleId,
48                 'permission_id' => $permissionId,
49             ]);
50         }
51     }
52
53     /**
54      * Reverse the migrations.
55      */
56     public function down(): void
57     {
58         Schema::dropIfExists('attachments');
59
60         // Create & attach new entity permissions
61         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
62         $entity = 'Attachment';
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 };