Install::createTables()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 29
nc 2
nop 0
dl 0
loc 39
rs 9.456
c 0
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\migrations;
13
14
use Craft;
15
use craft\config\DbConfig;
16
use craft\db\Migration;
17
use nystudio107\seomatic\fields\Seomatic_Meta as Seomatic_MetaField;
18
use nystudio107\seomatic\Seomatic;
19
20
/**
21
 * @author    nystudio107
22
 * @package   SEOmatic
23
 * @since     3.0.0
24
 */
25
class Install extends Migration
26
{
27
    // Public Properties
28
    // =========================================================================
29
30
    /**
31
     * @var string The database driver to use
32
     */
33
    public $driver;
34
35
    // Public Methods
36
    // =========================================================================
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function safeUp()
42
    {
43
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
44
        if ($this->createTables()) {
45
            $this->createIndexes();
46
            $this->addForeignKeys();
47
            // Refresh the db schema caches
48
            Craft::$app->db->schema->refresh();
49
            $this->insertDefaultData();
50
            $this->migrateData();
51
        }
52
53
        return true;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function safeDown()
60
    {
61
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
62
        $this->removeTables();
63
64
        return true;
65
    }
66
67
    // Protected Methods
68
    // =========================================================================
69
70
    /**
71
     * @return bool
72
     */
73
    protected function createTables()
74
    {
75
        $tablesCreated = false;
76
77
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%seomatic_metabundles}}');
78
        if ($tableSchema === null) {
79
            $tablesCreated = true;
80
            // seomatic_metabundles table
81
            $this->createTable(
82
                '{{%seomatic_metabundles}}',
83
                [
84
                    'id' => $this->primaryKey(),
85
                    'dateCreated' => $this->dateTime()->notNull(),
86
                    'dateUpdated' => $this->dateTime()->notNull(),
87
                    'uid' => $this->uid(),
88
89
                    'bundleVersion' => $this->string()->notNull()->defaultValue(''),
90
                    'sourceBundleType' => $this->string()->notNull()->defaultValue(''),
91
                    'sourceId' => $this->integer()->null(),
92
                    'sourceName' => $this->string()->notNull()->defaultValue(''),
93
                    'sourceHandle' => $this->string()->notNull()->defaultValue(''),
94
                    'sourceType' => $this->string(64)->notNull()->defaultValue(''),
95
                    'typeId' => $this->integer()->null(),
96
                    'sourceTemplate' => $this->string(500)->defaultValue(''),
97
                    'sourceSiteId' => $this->integer()->null(),
98
                    'sourceAltSiteSettings' => $this->text(),
99
                    'sourceDateUpdated' => $this->dateTime()->notNull(),
100
                    'metaGlobalVars' => $this->text(),
101
                    'metaSiteVars' => $this->text(),
102
                    'metaSitemapVars' => $this->text(),
103
                    'metaContainers' => $this->text(),
104
                    'redirectsContainer' => $this->text(),
105
                    'frontendTemplatesContainer' => $this->text(),
106
                    'metaBundleSettings' => $this->text(),
107
                ]
108
            );
109
        }
110
111
        return $tablesCreated;
112
    }
113
114
    /**
115
     * @return void
116
     */
117
    protected function createIndexes()
118
    {
119
        // seomatic_metabundles table
120
        $this->createIndex(
121
            $this->db->getIndexName(),
122
            '{{%seomatic_metabundles}}',
123
            'sourceBundleType',
124
            false
125
        );
126
        $this->createIndex(
127
            $this->db->getIndexName(),
128
            '{{%seomatic_metabundles}}',
129
            'sourceId',
130
            false
131
        );
132
        $this->createIndex(
133
            $this->db->getIndexName(),
134
            '{{%seomatic_metabundles}}',
135
            'sourceSiteId',
136
            false
137
        );
138
        $this->createIndex(
139
            $this->db->getIndexName(),
140
            '{{%seomatic_metabundles}}',
141
            'sourceHandle',
142
            false
143
        );
144
        // Additional commands depending on the db driver
145
        switch ($this->driver) {
146
            case DbConfig::DRIVER_MYSQL:
0 ignored issues
show
Deprecated Code introduced by
The constant craft\config\DbConfig::DRIVER_MYSQL has been deprecated: in 3.4.0. Use [[Connection::DRIVER_MYSQL]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

146
            case /** @scrutinizer ignore-deprecated */ DbConfig::DRIVER_MYSQL:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
147
                break;
148
            case DbConfig::DRIVER_PGSQL:
0 ignored issues
show
Deprecated Code introduced by
The constant craft\config\DbConfig::DRIVER_PGSQL has been deprecated: in 3.4.0. Use [[Connection::DRIVER_PGSQL]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
            case /** @scrutinizer ignore-deprecated */ DbConfig::DRIVER_PGSQL:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
149
                break;
150
        }
151
    }
152
153
    /**
154
     * @return void
155
     */
156
    protected function addForeignKeys()
157
    {
158
        $this->addForeignKey(
159
            $this->db->getForeignKeyName(),
160
            '{{%seomatic_metabundles}}',
161
            'sourceSiteId',
162
            '{{%sites}}',
163
            'id',
164
            'CASCADE',
165
            'CASCADE'
166
        );
167
    }
168
169
    /**
170
     * @return void
171
     */
172
    protected function insertDefaultData()
173
    {
174
        // Insert our default data
175
        Seomatic::$plugin->metaBundles->createGlobalMetaBundles();
176
        Seomatic::$plugin->metaBundles->createContentMetaBundles();
177
    }
178
179
    /**
180
     * @return void
181
     */
182
    protected function migrateData()
183
    {
184
        // Migrate the old Seomatic_Meta field
185
        $this->update('{{%fields}}', [
186
            'type' => Seomatic_MetaField::class,
187
        ], ['type' => 'Seomatic_Meta']);
188
    }
189
190
    /**
191
     * @return void
192
     */
193
    protected function removeTables()
194
    {
195
        // seomatic_metabundles table
196
        $this->dropTableIfExists('{{%seomatic_metabundles}}');
197
        // seomatic_frontendtemplates table (deprecated)
198
        $this->dropTableIfExists('{{%seomatic_frontendtemplates}}');
199
    }
200
}
201