Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

m140506_102106_rbac_init   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 154
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthManager() 0 8 2
A isOracle() 0 3 1
A isMSSQL() 0 3 3
A buildFkClause() 0 11 3
A down() 0 13 2
A up() 0 86 3
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
use yii\base\InvalidConfigException;
9
use yii\rbac\DbManager;
10
11
/**
12
 * Initializes RBAC tables.
13
 *
14
 * @author Alexander Kochetov <[email protected]>
15
 * @since 2.0
16
 */
17
class m140506_102106_rbac_init extends \yii\db\Migration
18
{
19
    /**
20
     * @throws yii\base\InvalidConfigException
21
     * @return DbManager
22
     */
23
    protected function getAuthManager()
24
    {
25
        $authManager = Yii::$app->getAuthManager();
26
        if (!$authManager instanceof DbManager) {
27
            throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
28
        }
29
30
        return $authManager;
31
    }
32
33
    /**
34
     * @return bool
35
     */
36
    protected function isMSSQL()
37
    {
38
        return $this->db->driverName === 'mssql' || $this->db->driverName === 'sqlsrv' || $this->db->driverName === 'dblib';
39
    }
40
41
    protected function isOracle()
42
    {
43
        return $this->db->driverName === 'oci';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function up()
50
    {
51
        $authManager = $this->getAuthManager();
52
        $this->db = $authManager->db;
53
        $schema = $this->db->getSchema()->defaultSchema;
54
55
        $tableOptions = null;
56
        if ($this->db->driverName === 'mysql') {
57
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
58
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
59
        }
60
61
        $this->createTable($authManager->ruleTable, [
62
            'name' => $this->string(64)->notNull(),
63
            'data' => $this->binary(),
64
            'created_at' => $this->integer(),
65
            'updated_at' => $this->integer(),
66
            'PRIMARY KEY ([[name]])',
67
        ], $tableOptions);
68
69
        $this->createTable($authManager->itemTable, [
70
            'name' => $this->string(64)->notNull(),
71
            'type' => $this->smallInteger()->notNull(),
72
            'description' => $this->text(),
73
            'rule_name' => $this->string(64),
74
            'data' => $this->binary(),
75
            'created_at' => $this->integer(),
76
            'updated_at' => $this->integer(),
77
            'PRIMARY KEY ([[name]])',
78
            'FOREIGN KEY ([[rule_name]]) REFERENCES ' . $authManager->ruleTable . ' ([[name]])' .
79
                $this->buildFkClause('ON DELETE SET NULL', 'ON UPDATE CASCADE'),
80
        ], $tableOptions);
81
        $this->createIndex('idx-auth_item-type', $authManager->itemTable, 'type');
82
83
        $this->createTable($authManager->itemChildTable, [
84
            'parent' => $this->string(64)->notNull(),
85
            'child' => $this->string(64)->notNull(),
86
            'PRIMARY KEY ([[parent]], [[child]])',
87
            'FOREIGN KEY ([[parent]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' .
88
                $this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
89
            'FOREIGN KEY ([[child]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' .
90
                $this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
91
        ], $tableOptions);
92
93
        $this->createTable($authManager->assignmentTable, [
94
            'item_name' => $this->string(64)->notNull(),
95
            'user_id' => $this->string(64)->notNull(),
96
            'created_at' => $this->integer(),
97
            'PRIMARY KEY ([[item_name]], [[user_id]])',
98
            'FOREIGN KEY ([[item_name]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' .
99
                $this->buildFkClause('ON DELETE CASCADE', 'ON UPDATE CASCADE'),
100
        ], $tableOptions);
101
102
        if ($this->isMSSQL()) {
103
            $this->execute("CREATE TRIGGER {$schema}.trigger_auth_item_child
104
            ON {$schema}.{$authManager->itemTable}
105
            INSTEAD OF DELETE, UPDATE
106
            AS
107
            DECLARE @old_name VARCHAR (64) = (SELECT name FROM deleted)
108
            DECLARE @new_name VARCHAR (64) = (SELECT name FROM inserted)
109
            BEGIN
110
            IF COLUMNS_UPDATED() > 0
111
                BEGIN
112
                    IF @old_name <> @new_name
113
                    BEGIN
114
                        ALTER TABLE {$authManager->itemChildTable} NOCHECK CONSTRAINT FK__auth_item__child;
115
                        UPDATE {$authManager->itemChildTable} SET child = @new_name WHERE child = @old_name;
116
                    END
117
                UPDATE {$authManager->itemTable}
118
                SET name = (SELECT name FROM inserted),
119
                type = (SELECT type FROM inserted),
120
                description = (SELECT description FROM inserted),
121
                rule_name = (SELECT rule_name FROM inserted),
122
                data = (SELECT data FROM inserted),
123
                created_at = (SELECT created_at FROM inserted),
124
                updated_at = (SELECT updated_at FROM inserted)
125
                WHERE name IN (SELECT name FROM deleted)
126
                IF @old_name <> @new_name
127
                    BEGIN
128
                        ALTER TABLE {$authManager->itemChildTable} CHECK CONSTRAINT FK__auth_item__child;
129
                    END
130
                END
131
                ELSE
132
                    BEGIN
133
                        DELETE FROM {$schema}.{$authManager->itemChildTable} WHERE parent IN (SELECT name FROM deleted) OR child IN (SELECT name FROM deleted);
134
                        DELETE FROM {$schema}.{$authManager->itemTable} WHERE name IN (SELECT name FROM deleted);
135
                    END
136
            END;");
137
        }
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function down()
144
    {
145
        $authManager = $this->getAuthManager();
146
        $this->db = $authManager->db;
147
148
        if ($this->isMSSQL()) {
149
            $this->execute('DROP TRIGGER {$schema}.trigger_auth_item_child;');
150
        }
151
152
        $this->dropTable($authManager->assignmentTable);
153
        $this->dropTable($authManager->itemChildTable);
154
        $this->dropTable($authManager->itemTable);
155
        $this->dropTable($authManager->ruleTable);
156
    }
157
158
    protected function buildFkClause($delete = '', $update = '')
159
    {
160
        if ($this->isMSSQL()) {
161
            return '';
162
        }
163
164
        if ($this->isOracle()) {
165
            return ' ' . $delete;
166
        }
167
168
        return implode(' ', ['', $delete, $update]);
169
    }
170
}
171