]> BookStack Code Mirror - bookstack/blob - database/migrations/2016_10_09_142037_create_files_table.php
Made language configurable via .env file
[bookstack] / database / migrations / 2016_10_09_142037_create_files_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 CreateFilesTable extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
15     {
16         Schema::create('files', 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 = 'File';
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\Carbon::now()->toDateTimeString(),
44                 'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
45             ]);
46             DB::table('permission_role')->insert([
47                 'role_id' => $adminRoleId,
48                 'permission_id' => $permissionId
49             ]);
50         }
51
52     }
53
54     /**
55      * Reverse the migrations.
56      *
57      * @return void
58      */
59     public function down()
60     {
61         Schema::dropIfExists('files');
62
63         // Create & attach new entity permissions
64         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
65         $entity = 'File';
66         foreach ($ops as $op) {
67             $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
68             DB::table('role_permissions')->where('name', '=', $permName)->delete();
69         }
70     }
71 }