Install::safeDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organization\migrations;
10
11
use craft\db\Migration;
12
use craft\records\Element as ElementRecord;
13
use craft\records\FieldLayout as FieldLayoutRecord;
14
use craft\records\Site as SiteRecord;
15
use craft\records\User as UserRecord;
16
use flipbox\organization\models\Settings;
17
use flipbox\organization\records\Organization as OrganizationRecord;
18
use flipbox\organization\records\OrganizationType as OrganizationTypeOrganizationRecord;
19
use flipbox\organization\records\Type as OrganizationTypeRecord;
20
use flipbox\organization\records\TypeSettings as OrganizationTypeSettingsRecord;
21
use flipbox\organization\records\User as OrganizationUserRecord;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class Install extends Migration
28
{
29
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function safeUp()
35
    {
36
        $this->createTables();
37
        $this->createIndexes();
38
        $this->addForeignKeys();
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function safeDown()
45
    {
46
47
        // Delete tables
48
        $this->dropTableIfExists(OrganizationUserRecord::tableName());
49
        $this->dropTableIfExists(OrganizationTypeOrganizationRecord::tableName());
50
        $this->dropTableIfExists(OrganizationTypeSettingsRecord::tableName());
51
        $this->dropTableIfExists(OrganizationTypeRecord::tableName());
52
        $this->dropTableIfExists(OrganizationRecord::tableName());
53
54
        return true;
55
    }
56
57
    /**
58
     * Creates the tables.
59
     *
60
     * @return void
61
     */
62
    protected function createTables()
63
    {
64
65
        /** @var array of statuses $defaultStatuses */
66
        $defaultStatuses = Settings::defaultStatuses();
67
68
        $this->createTable(OrganizationRecord::tableName(), [
69
            'id' => $this->primaryKey(),
70
            'ownerId' => $this->integer(),
71
            'status' => $this->enum('status', array_keys($defaultStatuses)),
72
            'dateJoined' => $this->dateTime()->notNull(),
73
            'dateCreated' => $this->dateTime()->notNull(),
74
            'dateUpdated' => $this->dateTime()->notNull(),
75
            'uid' => $this->uid()
76
        ]);
77
78
        $this->createTable(OrganizationTypeRecord::tableName(), [
79
            'id' => $this->primaryKey(),
80
            'handle' => $this->string()->notNull(),
81
            'name' => $this->string()->notNull(),
82
            'dateCreated' => $this->dateTime()->notNull(),
83
            'dateUpdated' => $this->dateTime()->notNull(),
84
            'uid' => $this->uid()
85
        ]);
86
87
        $this->createTable(OrganizationTypeSettingsRecord::tableName(), [
88
            'id' => $this->primaryKey(),
89
            'typeId' => $this->integer()->notNull(),
90
            'siteId' => $this->integer()->notNull(),
91
            'hasUrls' => $this->boolean()->defaultValue(true)->notNull(),
92
            'uriFormat' => $this->text(),
93
            'template' => $this->string(500),
94
            'fieldLayoutId' => $this->integer(),
95
            'dateCreated' => $this->dateTime()->notNull(),
96
            'dateUpdated' => $this->dateTime()->notNull(),
97
            'uid' => $this->uid()
98
        ]);
99
100
        $this->createTable(OrganizationTypeOrganizationRecord::tableName(), [
101
            'id' => $this->primaryKey(),
102
            'typeId' => $this->integer()->notNull(),
103
            'organizationId' => $this->integer()->notNull(),
104
            'primary' => $this->boolean()->defaultValue(false)->notNull(),
105
            'dateCreated' => $this->dateTime()->notNull(),
106
            'dateUpdated' => $this->dateTime()->notNull(),
107
            'uid' => $this->uid()
108
        ]);
109
110
        $this->createTable(OrganizationUserRecord::tableName(), [
111
            'id' => $this->primaryKey(),
112
            'userId' => $this->integer()->notNull(),
113
            'organizationId' => $this->integer()->notNull(),
114
            'siteId' => $this->integer(),
115
            'sortOrder' => $this->smallInteger()->unsigned(),
116
            'dateCreated' => $this->dateTime()->notNull(),
117
            'dateUpdated' => $this->dateTime()->notNull(),
118
            'uid' => $this->uid()
119
        ]);
120
    }
121
122
    /**
123
     * Creates the indexes.
124
     *
125
     * @return void
126
     */
127
    protected function createIndexes()
128
    {
129
130
        $this->createIndex(
131
            $this->db->getIndexName(OrganizationRecord::tableName(), 'ownerId', false, true),
132
            OrganizationRecord::tableName(),
133
            'ownerId',
134
            false
135
        );
136
137
        $this->createIndex(
138
            $this->db->getIndexName(OrganizationTypeRecord::tableName(), 'handle', true),
139
            OrganizationTypeRecord::tableName(),
140
            'handle',
141
            true
142
        );
143
144
        $this->createIndex(
145
            $this->db->getIndexName(OrganizationTypeSettingsRecord::tableName(), 'typeId', false, true),
146
            OrganizationTypeSettingsRecord::tableName(),
147
            'typeId',
148
            false
149
        );
150
        $this->createIndex(
151
            $this->db->getIndexName(OrganizationTypeSettingsRecord::tableName(), 'fieldLayoutId', false, true),
152
            OrganizationTypeSettingsRecord::tableName(),
153
            'fieldLayoutId',
154
            false
155
        );
156
        $this->createIndex(
157
            $this->db->getIndexName(OrganizationTypeSettingsRecord::tableName(), 'typeId,siteId', true),
158
            OrganizationTypeSettingsRecord::tableName(),
159
            'typeId,siteId',
160
            true
161
        );
162
        $this->createIndex(
163
            $this->db->getIndexName(OrganizationTypeSettingsRecord::tableName(), 'siteId', false, true),
164
            OrganizationTypeSettingsRecord::tableName(),
165
            'siteId',
166
            false
167
        );
168
169
        $this->createIndex(
170
            $this->db->getIndexName(OrganizationTypeOrganizationRecord::tableName(), 'typeId', false, true),
171
            OrganizationTypeOrganizationRecord::tableName(),
172
            'typeId',
173
            false
174
        );
175
        $this->createIndex(
176
            $this->db->getIndexName(OrganizationTypeOrganizationRecord::tableName(), 'organizationId', false, true),
177
            OrganizationTypeOrganizationRecord::tableName(),
178
            'organizationId',
179
            false
180
        );
181
        $this->createIndex(
182
            $this->db->getIndexName(OrganizationTypeOrganizationRecord::tableName(), 'typeId,organizationId', true),
183
            OrganizationTypeOrganizationRecord::tableName(),
184
            'typeId,organizationId',
185
            true
186
        );
187
        $this->createIndex(
188
            $this->db->getIndexName(OrganizationTypeOrganizationRecord::tableName(), 'primary', false),
189
            OrganizationTypeOrganizationRecord::tableName(),
190
            'primary',
191
            false
192
        );
193
194
        $this->createIndex(
195
            $this->db->getIndexName(OrganizationUserRecord::tableName(), 'userId', false, true),
196
            OrganizationUserRecord::tableName(),
197
            'userId',
198
            false
199
        );
200
        $this->createIndex(
201
            $this->db->getIndexName(OrganizationUserRecord::tableName(), 'siteId', false, true),
202
            OrganizationUserRecord::tableName(),
203
            'siteId',
204
            false
205
        );
206
        $this->createIndex(
207
            $this->db->getIndexName(OrganizationUserRecord::tableName(), 'organizationId', false, true),
208
            OrganizationUserRecord::tableName(),
209
            'organizationId',
210
            false
211
        );
212
        $this->createIndex(
213
            $this->db->getIndexName(OrganizationUserRecord::tableName(), 'userId,organizationId,siteId', true),
214
            OrganizationUserRecord::tableName(),
215
            'userId,organizationId,siteId',
216
            true
217
        );
218
    }
219
220
    /**
221
     * Adds the foreign keys.
222
     *
223
     * @return void
224
     */
225
    protected function addForeignKeys()
226
    {
227
228
        $this->addForeignKey(
229
            $this->db->getForeignKeyName(OrganizationRecord::tableName(), 'id'),
230
            OrganizationRecord::tableName(),
231
            'id',
232
            ElementRecord::tableName(),
233
            'id',
234
            'CASCADE',
235
            null
236
        );
237
        $this->addForeignKey(
238
            $this->db->getForeignKeyName(OrganizationRecord::tableName(), 'ownerId'),
239
            OrganizationRecord::tableName(),
240
            'ownerId',
241
            UserRecord::tableName(),
242
            'id',
243
            'CASCADE',
244
            null
245
        );
246
247
        $this->addForeignKey(
248
            $this->db->getForeignKeyName(OrganizationTypeSettingsRecord::tableName(), 'typeId'),
249
            OrganizationTypeSettingsRecord::tableName(),
250
            'typeId',
251
            OrganizationTypeRecord::tableName(),
252
            'id',
253
            'CASCADE',
254
            'CASCADE'
255
        );
256
        $this->addForeignKey(
257
            $this->db->getForeignKeyName(OrganizationTypeSettingsRecord::tableName(), 'siteId'),
258
            OrganizationTypeSettingsRecord::tableName(),
259
            'siteId',
260
            SiteRecord::tableName(),
261
            'id',
262
            'CASCADE',
263
            'CASCADE'
264
        );
265
        $this->addForeignKey(
266
            $this->db->getForeignKeyName(OrganizationTypeSettingsRecord::tableName(), 'fieldLayoutId'),
267
            OrganizationTypeSettingsRecord::tableName(),
268
            'fieldLayoutId',
269
            FieldLayoutRecord::tableName(),
270
            'id',
271
            'SET NULL',
272
            null
273
        );
274
275
        $this->addForeignKey(
276
            $this->db->getForeignKeyName(OrganizationTypeOrganizationRecord::tableName(), 'typeId'),
277
            OrganizationTypeOrganizationRecord::tableName(),
278
            'typeId',
279
            OrganizationTypeRecord::tableName(),
280
            'id',
281
            'CASCADE',
282
            'CASCADE'
283
        );
284
        $this->addForeignKey(
285
            $this->db->getForeignKeyName(OrganizationTypeOrganizationRecord::tableName(), 'organizationId'),
286
            OrganizationTypeOrganizationRecord::tableName(),
287
            'organizationId',
288
            OrganizationRecord::tableName(),
289
            'id',
290
            'CASCADE',
291
            'CASCADE'
292
        );
293
294
        $this->addForeignKey(
295
            $this->db->getForeignKeyName(OrganizationUserRecord::tableName(), 'userId'),
296
            OrganizationUserRecord::tableName(),
297
            'userId',
298
            UserRecord::tableName(),
299
            'id',
300
            'CASCADE',
301
            'CASCADE'
302
        );
303
        $this->addForeignKey(
304
            $this->db->getForeignKeyName(OrganizationUserRecord::tableName(), 'siteId'),
305
            OrganizationUserRecord::tableName(),
306
            'siteId',
307
            SiteRecord::tableName(),
308
            'id',
309
            'CASCADE',
310
            'CASCADE'
311
        );
312
        $this->addForeignKey(
313
            $this->db->getForeignKeyName(OrganizationUserRecord::tableName(), 'organizationId'),
314
            OrganizationUserRecord::tableName(),
315
            'organizationId',
316
            OrganizationRecord::tableName(),
317
            'id',
318
            'CASCADE',
319
            'CASCADE'
320
        );
321
    }
322
}
323