1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\migrations; |
10
|
|
|
|
11
|
|
|
use craft\db\Migration; |
12
|
|
|
use craft\db\Query; |
13
|
|
|
use flipbox\patron\events\handlers\BeforeInsertProviderInstance; |
14
|
|
|
use flipbox\patron\records\Provider; |
15
|
|
|
use flipbox\patron\records\ProviderEnvironment; |
16
|
|
|
use flipbox\patron\records\ProviderInstance; |
17
|
|
|
use yii\base\Event; |
18
|
|
|
|
19
|
|
|
class m181019_220655_provider_instances extends Migration |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function safeUp() |
25
|
|
|
{ |
26
|
|
|
// No insert events please... |
27
|
|
|
Event::off( |
28
|
|
|
ProviderInstance::class, |
29
|
|
|
ProviderInstance::EVENT_BEFORE_INSERT, |
30
|
|
|
[ |
31
|
|
|
BeforeInsertProviderInstance::class, |
32
|
|
|
'handle' |
33
|
|
|
] |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$this->createTable( |
37
|
|
|
ProviderInstance::tableName(), |
38
|
|
|
[ |
39
|
|
|
'id' => $this->primaryKey(), |
40
|
|
|
'providerId' => $this->integer()->notNull(), |
41
|
|
|
'clientId' => $this->char(ProviderInstance::CLIENT_ID_LENGTH)->notNull(), |
42
|
|
|
'clientSecret' => $this->char(ProviderInstance::CLIENT_SECRET_LENGTH), |
43
|
|
|
'settings' => $this->text(), |
44
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
45
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
46
|
|
|
'uid' => $this->uid(), |
47
|
|
|
] |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$this->addForeignKey( |
51
|
|
|
$this->db->getForeignKeyName( |
52
|
|
|
ProviderInstance::tableName(), |
53
|
|
|
'providerId' |
54
|
|
|
), |
55
|
|
|
ProviderInstance::tableName(), |
56
|
|
|
'providerId', |
57
|
|
|
Provider::tableName(), |
58
|
|
|
'id', |
59
|
|
|
'CASCADE' |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$this->addColumn( |
63
|
|
|
ProviderEnvironment::tableName(), |
64
|
|
|
'instanceId', |
65
|
|
|
$this->integer() |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// Migrates all the existing providers to new 'instance' types |
69
|
|
|
$this->migrateToInstances(); |
70
|
|
|
|
71
|
|
|
// Make the provider handle unique |
72
|
|
|
$this->createIndex( |
73
|
|
|
$this->db->getIndexName( |
74
|
|
|
Provider::tableName(), |
75
|
|
|
'handle', |
76
|
|
|
true, |
77
|
|
|
false |
78
|
|
|
), |
79
|
|
|
Provider::tableName(), |
80
|
|
|
'handle', |
81
|
|
|
true |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$this->dropColumn( |
85
|
|
|
Provider::tableName(), |
86
|
|
|
'settings' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->dropColumn( |
90
|
|
|
Provider::tableName(), |
91
|
|
|
'clientId' |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->dropColumn( |
95
|
|
|
Provider::tableName(), |
96
|
|
|
'clientSecret' |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
// We should be able to enforce notNull now |
100
|
|
|
$this->alterColumn( |
101
|
|
|
ProviderEnvironment::tableName(), |
102
|
|
|
'instanceId', |
103
|
|
|
$this->integer()->notNull() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
// Apply foreign key to settings |
107
|
|
|
$this->addForeignKey( |
108
|
|
|
$this->db->getForeignKeyName( |
109
|
|
|
ProviderEnvironment::tableName(), |
110
|
|
|
'instanceId' |
111
|
|
|
), |
112
|
|
|
ProviderEnvironment::tableName(), |
113
|
|
|
'instanceId', |
114
|
|
|
ProviderInstance::tableName(), |
115
|
|
|
'id', |
116
|
|
|
'CASCADE' |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
// Remove existing foreign key |
120
|
|
|
$this->dropForeignKey( |
121
|
|
|
$this->db->getForeignKeyName( |
122
|
|
|
ProviderEnvironment::tableName(), |
123
|
|
|
'providerId' |
124
|
|
|
), |
125
|
|
|
ProviderEnvironment::tableName() |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->dropColumn( |
129
|
|
|
ProviderEnvironment::tableName(), |
130
|
|
|
'providerId' |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$this->dropPrimaryKey( |
134
|
|
|
$this->db->getPrimaryKeyName( |
135
|
|
|
ProviderEnvironment::tableName(), |
136
|
|
|
[ |
137
|
|
|
'environment' |
138
|
|
|
] |
139
|
|
|
), |
140
|
|
|
ProviderEnvironment::tableName() |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$this->addPrimaryKey( |
144
|
|
|
$this->db->getPrimaryKeyName( |
145
|
|
|
ProviderEnvironment::tableName(), |
146
|
|
|
[ |
147
|
|
|
'instanceId', |
148
|
|
|
'environment' |
149
|
|
|
] |
150
|
|
|
), |
151
|
|
|
ProviderEnvironment::tableName(), |
152
|
|
|
[ |
153
|
|
|
'instanceId', |
154
|
|
|
'environment' |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
*/ |
162
|
|
|
protected function migrateToInstances() |
163
|
|
|
{ |
164
|
|
|
$query = (new Query) |
165
|
|
|
->select(['id', 'settings', 'clientId', 'clientSecret']) |
166
|
|
|
->from(Provider::tableName()); |
167
|
|
|
|
168
|
|
|
$rows = $query->all(); |
169
|
|
|
|
170
|
|
|
foreach ($rows as $row) { |
171
|
|
|
$instance = new ProviderInstance(); |
172
|
|
|
$instance->setProviderId($row['id']); |
173
|
|
|
$instance->settings = $row['settings']; |
174
|
|
|
$instance->clientId = $row['clientId']; |
175
|
|
|
$instance->clientSecret = $row['clientSecret']; |
176
|
|
|
|
177
|
|
|
if (!$instance->save()) { |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** @var ProviderEnvironment[] $environments */ |
182
|
|
|
$environments = ProviderEnvironment::findAll([ |
183
|
|
|
'providerId' => $row['id'] |
184
|
|
|
]); |
185
|
|
|
|
186
|
|
|
foreach ($environments as $environment) { |
187
|
|
|
$environment->instanceId = $instance->getId(); |
188
|
|
|
$environment->save(true, ['instanceId']); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
|
|
public function safeDown() |
197
|
|
|
{ |
198
|
|
|
$this->dropTableIfExists(ProviderInstance::tableName()); |
199
|
|
|
|
200
|
|
|
return true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|