Install::createTables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
/**
3
 * @link      https://dukt.net/craft/social/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://dukt.net/craft/social/docs/license
6
 */
7
8
namespace dukt\social\migrations;
9
10
use Craft;
11
use craft\db\Migration;
12
13
/**
14
 * Class Install
15
 *
16
 * @author Dukt <[email protected]>
17
 * @since  2.0
18
 */
19
class Install extends Migration
20
{
21
    // Public Properties
22
    // =========================================================================
23
24
    /**
25
     * @var string The database driver to use
26
     */
27
    public $driver;
28
29
    // Public Methods
30
    // =========================================================================
31
32
    /**
33
     * This method contains the logic to be executed when applying this migration.
34
     * This method differs from [[up()]] in that the DB logic implemented here will
35
     * be enclosed within a DB transaction.
36
     * Child classes may implement this method instead of [[up()]] if the DB logic
37
     * needs to be within a transaction.
38
     *
39
     * @return boolean return a false value to indicate the migration fails
40
     * and should not proceed further. All other return values mean the migration succeeds.
41
     */
42
    public function safeUp()
43
    {
44
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
45
        $this->createTables();
46
        $this->createIndexes();
47
        $this->addForeignKeys();
48
        $this->insertDefaultData();
49
50
        return true;
51
    }
52
53
    /**
54
     * This method contains the logic to be executed when removing this migration.
55
     * This method differs from [[down()]] in that the DB logic implemented here will
56
     * be enclosed within a DB transaction.
57
     * Child classes may implement this method instead of [[down()]] if the DB logic
58
     * needs to be within a transaction.
59
     *
60
     * @return boolean return a false value to indicate the migration fails
61
     * and should not proceed further. All other return values mean the migration succeeds.
62
     */
63
    public function safeDown()
64
    {
65
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
66
        $this->removeIndexes();
67
        $this->removeTables();
68
69
        return true;
70
    }
71
72
    // Protected Methods
73
    // =========================================================================
74
75
    /**
76
     * Creates the tables needed for the Records used by the plugin
77
     *
78
     * @return void
79
     */
80
    protected function createTables()
81
    {
82
        $this->createTable(
83
            '{{%social_login_accounts}}',
84
            [
85
                'id' => $this->integer()->notNull(),
86
                'userId' => $this->integer()->notNull(),
87
                'providerHandle' => $this->string(255)->notNull(),
88
                'socialUid' => $this->string(255)->notNull(),
89
90
                'dateCreated' => $this->dateTime()->notNull(),
91
                'dateUpdated' => $this->dateTime()->notNull(),
92
                'uid' => $this->uid(),
93
                'PRIMARY KEY(id)',
94
            ]
95
        );
96
    }
97
98
    /**
99
     * Creates the indexes needed for the Records used by the plugin
100
     *
101
     * @return void
102
     */
103
    protected function createIndexes()
104
    {
105
    }
106
107
    /**
108
     * Creates the foreign keys needed for the Records used by the plugin
109
     *
110
     * @return void
111
     */
112
    protected function addForeignKeys()
113
    {
114
        $this->addForeignKey($this->db->getForeignKeyName(), '{{%social_login_accounts}}', 'id', '{{%elements}}', 'id', 'CASCADE', null);
115
        $this->addForeignKey($this->db->getForeignKeyName(), '{{%social_login_accounts}}', 'userId', '{{%users}}', 'id', 'CASCADE', null);
116
    }
117
118
    /**
119
     * Populates the DB with the default data.
120
     *
121
     * @return void
122
     */
123
    protected function insertDefaultData()
124
    {
125
    }
126
127
    /**
128
     * Removes the tables needed for the Records used by the plugin
129
     *
130
     * @return void
131
     */
132
    protected function removeTables()
133
    {
134
        $this->dropTable('{{%social_login_accounts}}');
135
    }
136
137
    /**
138
     * Removes the indexes needed for the Records used by the plugin
139
     *
140
     * @return void
141
     */
142
    protected function removeIndexes()
143
    {
144
    }
145
}
146