1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace mmikkel\cacheflag\migrations; |
5
|
|
|
|
6
|
|
|
use Craft; |
7
|
|
|
use craft\db\Migration; |
8
|
|
|
use craft\db\Query; |
9
|
|
|
use craft\elements\Asset; |
10
|
|
|
use craft\elements\Category; |
11
|
|
|
use craft\elements\Entry; |
12
|
|
|
use craft\elements\GlobalSet; |
13
|
|
|
use craft\elements\MatrixBlock; |
14
|
|
|
use craft\elements\Tag; |
15
|
|
|
use craft\elements\User; |
16
|
|
|
use mmikkel\cacheflag\records\Flags; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class m200721_201623_migrate_craft2_flags |
20
|
|
|
* @package mmikkel\cacheflag\migrations |
21
|
|
|
* @since 1.2.0 |
22
|
|
|
*/ |
23
|
|
|
class m200721_201623_migrate_craft2_flags extends Migration |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function safeUp() |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
// Don't make the same config changes twice |
33
|
|
|
$schemaVersion = Craft::$app->projectConfig |
34
|
|
|
->get('plugins.cache-flag.schemaVersion', true); |
35
|
|
|
|
36
|
|
|
if (\version_compare($schemaVersion, '1.0.1', '>=')) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!!Flags::find()->count()) { |
41
|
|
|
// There's already content in the Craft 3 table, so don't attempt to migrate anything |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$craft2TableSchema = Craft::$app->db->schema->getTableSchema('{{%templatecaches_flags}}'); |
46
|
|
|
if ($craft2TableSchema === null) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Get all migratable flags |
51
|
|
|
$baseQuery = (new Query()) |
52
|
|
|
->select(['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'assetSourceId', 'globalSetId', 'elementType', 'flags.dateCreated', 'flags.dateUpdated', 'flags.uid']) |
53
|
|
|
->from('{{%templatecaches_flags}} AS flags'); |
54
|
|
|
|
55
|
|
|
$rowsToInsert = []; |
56
|
|
|
|
57
|
|
|
$rowsToInsert += (clone $baseQuery) |
58
|
|
|
->innerJoin('{{%sections}} AS sections', 'sections.id=sectionId') |
59
|
|
|
->all(); |
60
|
|
|
|
61
|
|
|
$rowsToInsert += (clone $baseQuery) |
62
|
|
|
->innerJoin('{{%categorygroups}} AS categorygroups', 'categorygroups.id=categoryGroupId') |
63
|
|
|
->all(); |
64
|
|
|
|
65
|
|
|
$rowsToInsert += (clone $baseQuery) |
66
|
|
|
->innerJoin('{{%taggroups}} AS taggroups', 'taggroups.id=tagGroupId') |
67
|
|
|
->all(); |
68
|
|
|
|
69
|
|
|
$rowsToInsert += (clone $baseQuery) |
70
|
|
|
->innerJoin('{{%usergroups}} AS usergroups', 'usergroups.id=userGroupId') |
71
|
|
|
->all(); |
72
|
|
|
|
73
|
|
|
$volumeRows = (clone $baseQuery) |
74
|
|
|
->innerJoin('{{%volumes}} AS volumes', 'volumes.id=assetSourceId') |
75
|
|
|
->all(); |
76
|
|
|
|
77
|
|
|
foreach ($volumeRows as $volumeRow) { |
78
|
|
|
$volumeRow['volumeId'] = $volumeRow['assetSourceId']; |
79
|
|
|
unset($volumeRow['assetSourceId']); |
80
|
|
|
$rowsToInsert[] = $volumeRow; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$rowsToInsert += (clone $baseQuery) |
84
|
|
|
->innerJoin('{{%globalsets}} AS globalsets', 'globalsets.id=globalSetId') |
85
|
|
|
->all(); |
86
|
|
|
|
87
|
|
|
$elementTypeRows = (clone $baseQuery) |
88
|
|
|
->where('elementType IS NOT NULL') |
89
|
|
|
->all(); |
90
|
|
|
foreach ($elementTypeRows as $elementTypeRow) { |
91
|
|
|
$elementType = $elementTypeRow['elementType']; |
92
|
|
|
switch ($elementType) { |
93
|
|
|
case 'Entry': |
94
|
|
|
$elementType = Entry::class; |
95
|
|
|
break; |
96
|
|
|
case 'Category': |
97
|
|
|
$elementType = Category::class; |
98
|
|
|
break; |
99
|
|
|
case 'Tag': |
100
|
|
|
$elementType = Tag::class; |
101
|
|
|
break; |
102
|
|
|
case 'User': |
103
|
|
|
$elementType = User::class; |
104
|
|
|
break; |
105
|
|
|
case 'Asset': |
106
|
|
|
$elementType = Asset::class; |
107
|
|
|
break; |
108
|
|
|
case 'GlobalSet': |
109
|
|
|
$elementType = GlobalSet::class; |
110
|
|
|
break; |
111
|
|
|
case 'MatrixBlock': |
112
|
|
|
$elementType = MatrixBlock::class; |
113
|
|
|
break; |
114
|
|
|
default: |
115
|
|
|
$elementType = null; |
116
|
|
|
} |
117
|
|
|
if (!$elementType) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
$elementTypeRow['elementType'] = $elementType; |
121
|
|
|
$rowsToInsert[] = $elementTypeRow; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!empty($rowsToInsert)) { |
125
|
|
|
$this |
126
|
|
|
->batchInsert( |
127
|
|
|
'{{%cacheflag_flags}}', |
128
|
|
|
['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'volumeId', 'globalSetId', 'elementType', 'dateCreated', 'dateUpdated', 'uid'], |
129
|
|
|
$rowsToInsert, |
130
|
|
|
false |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Delete the Craft 2 table |
135
|
|
|
$this->dropTableIfExists('{{%templatecaches_flags}}'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function safeDown() |
142
|
|
|
{ |
143
|
|
|
echo "m200721_201623_migrate_craft2_flags cannot be reverted.\n"; |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|