2015_12_29_015055_setup_access_tables.php
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
/**
 * Class SetupAccessTables.
 */
class SetupAccessTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table(config('access.users_table'), function ($table) {
            $table->tinyInteger('status')->after('password')->default(1)->unsigned();
            $table->softDeletes();
        });
        Schema::create(config('access.roles_table'), function ($table) {
            $table->increments('id')->unsigned();
            $table->string('name');
            $table->boolean('all')->default(false);
            $table->smallInteger('sort')->default(0)->unsigned();
            $table->timestamps();
            /*
             * Add Foreign/Unique/Index
             */
            $table->unique('name');
        });
        Schema::create(config('access.role_user_table'), function ($table) {
            $table->increments('id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();
            /*
             * Add Foreign/Unique/Index
             */
            $table->foreign('user_id')
                ->references('id')
                ->on(config('access.users_table'))
                ->onDelete('cascade');
            $table->foreign('role_id')
                ->references('id')
                ->on(config('access.roles_table'))
                ->onDelete('cascade');
        });
        Schema::create(config('access.permissions_table'), function ($table) {
            $table->increments('id')->unsigned();
            $table->string('name');
            $table->string('display_name');
            $table->timestamps();
            /*
             * Add Foreign/Unique/Index
             */
            $table->unique('name');
        });
        Schema::create(config('access.permission_role_table'), function ($table) {
            $table->increments('id')->unsigned();
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
            /*
             * Add Foreign/Unique/Index
             */
            $table->foreign('permission_id')
                ->references('id')
                ->on(config('access.permissions_table'))
                ->onDelete('cascade');
            $table->foreign('role_id')
                ->references('id')
                ->on(config('access.roles_table'))
                ->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table(config('access.users_table'), function (Blueprint $table) {
            $table->dropColumn(['status', 'deleted_at']);
        });
        /*
         * Remove Foreign/Unique/Index
         */
        Schema::table(config('access.roles_table'), function (Blueprint $table) {
            $table->dropUnique(config('access.roles_table').'_name_unique');
        });
        Schema::table(config('access.role_user_table'), function (Blueprint $table) {
            $table->dropForeign(config('access.role_user_table').'_user_id_foreign');
            $table->dropForeign(config('access.role_user_table').'_role_id_foreign');
        });
        Schema::table(config('access.permissions_table'), function (Blueprint $table) {
            $table->dropUnique(config('access.permissions_table').'_name_unique');
        });
        Schema::table(config('access.permission_role_table'), function (Blueprint $table) {
            $table->dropForeign(config('access.permission_role_table').'_permission_id_foreign');
            $table->dropForeign(config('access.permission_role_table').'_role_id_foreign');
        });
        /*
         * Drop tables
         */
        Schema::dropIfExists(config('access.role_user_table'));
        Schema::dropIfExists(config('access.permission_role_table'));
        Schema::dropIfExists(config('access.roles_table'));
        Schema::dropIfExists(config('access.permissions_table'));
    }
}