Passed
Push — develop ( ba3995...484d74 )
by Andrew
07:17
created

m181213_233502_add_site_id::createIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 34
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace nystudio107\retour\migrations;
4
5
use Craft;
6
use craft\db\Migration;
7
8
/**
9
 * m181213_233502_add_site_id migration.
10
 */
11
class m181213_233502_add_site_id extends Migration
12
{
13
    /**
14
     * @inheritdoc
15
     */
16
    public function safeUp()
17
    {
18
        // Drop the locale columns
19
        if ($this->db->columnExists('{{%retour_redirects}}', 'locale')) {
20
            $this->dropColumn(
21
                '{{%retour_redirects}}',
22
                'enabled'
23
            );
24
        }
25
        if ($this->db->columnExists('{{%retour_static_redirects}}', 'locale')) {
26
            $this->dropColumn(
27
                '{{%retour_static_redirects}}',
28
                'enabled'
29
            );
30
        }
31
        // Add in the siteId columns
32
        if (!$this->db->columnExists('{{%retour_redirects}}', 'siteId')) {
33
            $this->addColumn(
34
                '{{%retour_redirects}}',
35
                'siteId',
36
                $this->integer()->null()->after('uid')->defaultValue(null)
37
            );
38
        }
39
        if (!$this->db->columnExists('{{%retour_static_redirects}}', 'siteId')) {
40
            $this->addColumn(
41
                '{{%retour_static_redirects}}',
42
                'siteId',
43
                $this->integer()->null()->after('uid')->defaultValue(null)
44
            );
45
        }
46
        if (!$this->db->columnExists('{{%retour_stats}}', 'siteId')) {
47
            $this->addColumn(
48
                '{{%retour_stats}}',
49
                'siteId',
50
                $this->integer()->null()->after('uid')->defaultValue(null)
51
            );
52
        }
53
        // Add foreign keys
54
        $this->addForeignKeys();
55
        // Create indexes
56
        $this->createIndexes();
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function safeDown()
63
    {
64
        echo "m181213_233502_add_site_id cannot be reverted.\n";
65
        return false;
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    protected function createIndexes()
72
    {
73
        $this->createIndex(
74
            $this->db->getIndexName(
75
                '{{%retour_redirects}}',
76
                'siteId',
77
                false
78
            ),
79
            '{{%retour_redirects}}',
80
            'siteId',
81
            false
82
        );
83
84
        $this->createIndex(
85
            $this->db->getIndexName(
86
                '{{%retour_static_redirects}}',
87
                'siteId',
88
                false
89
            ),
90
            '{{%retour_static_redirects}}',
91
            'siteId',
92
            false
93
        );
94
95
96
        $this->createIndex(
97
            $this->db->getIndexName(
98
                '{{%retour_stats}}',
99
                'siteId',
100
                false
101
            ),
102
            '{{%retour_stats}}',
103
            'siteId',
104
            false
105
        );
106
    }
107
108
    /**
109
     * @return void
110
     */
111
    protected function addForeignKeys()
112
    {
113
        $this->addForeignKey(
114
            $this->db->getForeignKeyName('{{%retour_redirects}}', 'siteId'),
115
            '{{%retour_redirects}}',
116
            'siteId',
117
            '{{%sites}}',
118
            'id',
119
            'CASCADE',
120
            'CASCADE'
121
        );
122
123
        $this->addForeignKey(
124
            $this->db->getForeignKeyName('{{%retour_static_redirects}}', 'siteId'),
125
            '{{%retour_static_redirects}}',
126
            'siteId',
127
            '{{%sites}}',
128
            'id',
129
            'CASCADE',
130
            'CASCADE'
131
        );
132
133
        $this->addForeignKey(
134
            $this->db->getForeignKeyName('{{%retour_stats}}', 'siteId'),
135
            '{{%retour_stats}}',
136
            'siteId',
137
            '{{%sites}}',
138
            'id',
139
            'CASCADE',
140
            'CASCADE'
141
        );
142
    }
143
}
144