-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathBaseEntryTypeMergeMigration.php
More file actions
125 lines (111 loc) · 3.94 KB
/
BaseEntryTypeMergeMigration.php
File metadata and controls
125 lines (111 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace craft\migrations;
use craft\db\Migration;
use craft\db\Query;
use craft\db\Table;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\Json;
use craft\records\EntryType as EntryTypeRecord;
/**
* Base entry type merge migration class.
*
* This is extended by content migrations generated by the `entry-types/merge` command.
*
* @since 5.3.0
*/
class BaseEntryTypeMergeMigration extends Migration
{
public string $persistingEntryTypeUid;
public string $outgoingEntryTypeUid;
/** @var array<string,string> */
public array $layoutElementUidMap;
public function safeUp(): bool
{
/** @var EntryTypeRecord|null $persistingEntryTypeRecord */
$persistingEntryTypeRecord = EntryTypeRecord::findWithTrashed()
->where(['uid' => $this->persistingEntryTypeUid])
->one();
if (!$persistingEntryTypeRecord) {
echo "Couldn't find persisting entry type record ($this->persistingEntryTypeUid)";
return false;
}
/** @var EntryTypeRecord|null $outgoingEntryTypeRecord */
$outgoingEntryTypeRecord = EntryTypeRecord::findWithTrashed()
->where(['uid' => $this->outgoingEntryTypeUid])
->one();
if (!$outgoingEntryTypeRecord) {
echo "Couldn't find outgoing entry type record ($this->outgoingEntryTypeUid)";
return false;
}
$query = (new Query())
->select(['es.id', 'es.content'])
->from(['es' => Table::ELEMENTS_SITES])
->innerJoin(['e' => Table::ENTRIES], '[[e.id]] = [[es.elementId]]')
->where(['e.typeId' => $outgoingEntryTypeRecord->id])
->andWhere(['not', ['es.content' => null]]);
$total = (string)$query->count();
$totalLen = strlen($total);
$i = 0;
$rawTableName = $this->db->getSchema()->getRawTableName(Table::ELEMENTS_SITES);
foreach (Db::each($query) as $row) {
$i++;
echo sprintf(
' > [%s/%s] Updating %s#%s … ',
str_pad((string)$i, $totalLen, '0', STR_PAD_LEFT),
$total,
$rawTableName,
$row['id'],
);
$content = Json::decode($row['content']);
$changed = false;
foreach ($this->layoutElementUidMap as $oldUid => $newUid) {
if (array_key_exists($oldUid, $content)) {
$content[$newUid] = ArrayHelper::remove($content, $oldUid);
$changed = true;
}
}
if ($changed) {
Db::update(
Table::ELEMENTS_SITES,
['content' => $content],
['id' => $row['id']],
);
}
echo "✓\n";
}
echo ' > Restoring entries … ';
$elementsTable = Table::ELEMENTS;
$entriesTable = Table::ENTRIES;
if ($this->db->getIsMysql()) {
$this->db->createCommand(<<<SQL
UPDATE $elementsTable [[elements]]
INNER JOIN $entriesTable [[entries]] ON [[entries.id]] = [[elements.id]]
SET [[elements.dateDeleted]] = NULL
WHERE [[entries.typeId]] = $outgoingEntryTypeRecord->id
AND [[entries.deletedWithEntryType]] = 1
SQL)->execute();
} else {
$this->db->createCommand(<<<SQL
UPDATE $elementsTable [[elements]]
SET [[dateDeleted]] = NULL
FROM $entriesTable [[entries]]
WHERE [[entries.id]] = [[elements.id]]
AND [[entries.typeId]] = $outgoingEntryTypeRecord->id
AND [[entries.deletedWithEntryType]] = TRUE
SQL)->execute();
}
echo "✓\n";
echo ' > Reassigning entries … ';
Db::update(
Table::ENTRIES,
[
'typeId' => $persistingEntryTypeRecord->id,
'deletedWithEntryType' => false,
],
['typeId' => $outgoingEntryTypeRecord->id],
);
echo "✓\n";
return true;
}
}