1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/analytics/ |
4
|
|
|
* @copyright Copyright (c) 2022, Dukt |
5
|
|
|
* @license https://dukt.net/analytics/docs/license |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\analytics\migrations; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\db\Migration; |
12
|
|
|
use craft\helpers\MigrationHelper; |
13
|
|
|
use dukt\analytics\models\Info; |
14
|
|
|
use dukt\analytics\Plugin; |
15
|
|
|
|
16
|
|
|
class Install extends Migration |
17
|
|
|
{ |
18
|
|
|
// Public Properties |
19
|
|
|
// ========================================================================= |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The database driver to use |
23
|
|
|
*/ |
24
|
|
|
public $driver; |
25
|
|
|
|
26
|
|
|
// Public Methods |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This method contains the logic to be executed when applying this migration. |
31
|
|
|
* This method differs from [[up()]] in that the DB logic implemented here will |
32
|
|
|
* be enclosed within a DB transaction. |
33
|
|
|
* Child classes may implement this method instead of [[up()]] if the DB logic |
34
|
|
|
* needs to be within a transaction. |
35
|
|
|
* |
36
|
|
|
* @return boolean return a false value to indicate the migration fails |
37
|
|
|
* and should not proceed further. All other return values mean the migration succeeds. |
38
|
|
|
*/ |
39
|
|
|
public function safeUp() |
40
|
|
|
{ |
41
|
|
|
$this->driver = Craft::$app->getConfig()->getDb()->driver; |
42
|
|
|
$this->createTables(); |
43
|
|
|
$this->createIndexes(); |
44
|
|
|
$this->addForeignKeys(); |
45
|
|
|
$this->insertDefaultData(); |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This method contains the logic to be executed when removing this migration. |
52
|
|
|
* This method differs from [[down()]] in that the DB logic implemented here will |
53
|
|
|
* be enclosed within a DB transaction. |
54
|
|
|
* Child classes may implement this method instead of [[down()]] if the DB logic |
55
|
|
|
* needs to be within a transaction. |
56
|
|
|
* |
57
|
|
|
* @return boolean return a false value to indicate the migration fails |
58
|
|
|
* and should not proceed further. All other return values mean the migration succeeds. |
59
|
|
|
*/ |
60
|
|
|
public function safeDown() |
61
|
|
|
{ |
62
|
|
|
$this->driver = Craft::$app->getConfig()->getDb()->driver; |
63
|
|
|
$this->removeForeignKeys(); |
64
|
|
|
$this->removeTables(); |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Protected Methods |
70
|
|
|
// ========================================================================= |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates the tables needed for the Records used by the plugin |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function createTables() |
78
|
|
|
{ |
79
|
|
|
$this->createTable( |
80
|
|
|
'{{%analytics_views}}', |
81
|
|
|
[ |
82
|
|
|
'id' => $this->primaryKey(), |
83
|
|
|
'name' => $this->string()->notNull(), |
84
|
|
|
'gaAccountId' => $this->string()->notNull(), |
85
|
|
|
'gaAccountName' => $this->string()->notNull(), |
86
|
|
|
'gaPropertyId' => $this->string()->notNull(), |
87
|
|
|
'gaPropertyName' => $this->string()->notNull(), |
88
|
|
|
'gaViewId' => $this->string()->notNull(), |
89
|
|
|
'gaViewName' => $this->string()->notNull(), |
90
|
|
|
'gaViewCurrency' => $this->string()->notNull(), |
91
|
|
|
|
92
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
93
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
94
|
|
|
'uid' => $this->uid() |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->createTable( |
99
|
|
|
'{{%analytics_site_views}}', |
100
|
|
|
[ |
101
|
|
|
'id' => $this->primaryKey(), |
102
|
|
|
'siteId' => $this->integer()->notNull(), |
103
|
|
|
'viewId' => $this->integer(), |
104
|
|
|
|
105
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
106
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
107
|
|
|
'uid' => $this->uid() |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->createTable( |
112
|
|
|
'{{%analytics_info}}', |
113
|
|
|
[ |
114
|
|
|
'id' => $this->primaryKey(), |
115
|
|
|
'forceConnect' => $this->boolean()->defaultValue(false)->notNull(), |
116
|
|
|
'token' => $this->text(), |
117
|
|
|
|
118
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
119
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
120
|
|
|
'uid' => $this->uid() |
121
|
|
|
] |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Creates the indexes needed for the Records used by the plugin |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
protected function createIndexes() |
131
|
|
|
{ |
132
|
|
|
$this->createIndex(null, '{{%analytics_site_views}}', 'siteId,viewId', true); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Creates the foreign keys needed for the Records used by the plugin |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function addForeignKeys() |
141
|
|
|
{ |
142
|
|
|
$this->addForeignKey($this->db->getForeignKeyName('{{%analytics_site_views}}', 'siteId'), '{{%analytics_site_views}}', 'siteId', '{{%sites}}', 'id', 'CASCADE', null); |
|
|
|
|
143
|
|
|
$this->addForeignKey($this->db->getForeignKeyName('{{%analytics_site_views}}', 'viewId'), '{{%analytics_site_views}}', 'viewId', '{{%analytics_views}}', 'id', 'CASCADE', null); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Populates the DB with the default data. |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
protected function insertDefaultData() |
152
|
|
|
{ |
153
|
|
|
// Populate the info table |
154
|
|
|
echo ' > populate the analytics_info table ...'; |
155
|
|
|
Plugin::getInstance()->saveInfo(new Info()); |
156
|
|
|
echo " done\n"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Removes the tables needed for the Records used by the plugin |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
protected function removeTables() |
165
|
|
|
{ |
166
|
|
|
$this->dropTable('{{%analytics_views}}'); |
167
|
|
|
$this->dropTable('{{%analytics_site_views}}'); |
168
|
|
|
$this->dropTable('{{%analytics_info}}'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Removes the foreign keys needed for the Records used by the plugin |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
protected function removeForeignKeys() |
177
|
|
|
{ |
178
|
|
|
if ($this->db->tableExists('{{%analytics_site_views}}')) { |
179
|
|
|
MigrationHelper::dropAllForeignKeysOnTable('{{%analytics_site_views}}'); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.