Skip to content

Commit 73909d1

Browse files
committed
Updating migration to fix sebastian-lenz#230 sebastian-lenz#254
1 parent 67e33b2 commit 73909d1

File tree

1 file changed

+66
-39
lines changed

1 file changed

+66
-39
lines changed

src/migrations/m190417_202153_migrateDataToTable.php

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use lenz\linkfield\fields\LinkField;
1313
use lenz\linkfield\records\LinkRecord;
1414
use verbb\supertable\fields\SuperTableField;
15+
use Yii;
1516

1617
/**
1718
* m190417_202153_migrateDataToTable migration.
@@ -119,7 +120,11 @@ private function updateMatrixField(Matrix $matrixField): void {
119120
*/
120121
private function updateLinkField(LinkField $field, string $table, string $handlePrefix = ''): void {
121122
$insertRows = [];
123+
$columnsToDrop = [];
122124
$columnName = ($field->columnPrefix ?: 'field_') . $handlePrefix . $field->handle;
125+
if (!$this->db->tableExists($table)) {
126+
return;
127+
}
123128
if ($field->columnSuffix) {
124129
$columnName .= '_' . $field->columnSuffix;
125130
}
@@ -132,55 +137,77 @@ private function updateLinkField(LinkField $field, string $table, string $handle
132137
}
133138
};
134139

135-
// Make sure the rows actually exist in the elements table.
136-
$rows = (new Query())
137-
->select(['t.elementId', 't.siteId', 't.'.$columnName])
138-
->from(['t' => $table])
139-
->innerJoin(['e' => Table::ELEMENTS], '[[t.elementId]] = [[e.id]]')
140-
->all();
140+
$yiiTable = Yii::$app->db->schema->getTableSchema($table);
141+
if (isset($yiiTable->columns[$columnName])) {
142+
// do something
141143

142-
foreach ($rows as $row) {
143-
$payload = Json::decode($row[$columnName]);
144-
if (!is_array($payload)) {
145-
continue;
146-
}
147144

148-
$type = $payload['type'] ?? null;
149-
$value = $payload['value'] ?? '';
150-
unset($payload['type']);
151-
unset($payload['value']);
145+
// Make sure the rows actually exist in the elements table.
146+
$rowQuery = (new Query())
147+
->select(['t.elementId', 't.siteId', 't.'.$columnName])
148+
->from(['t' => $table])
149+
->innerJoin(['e' => Table::ELEMENTS], '[[t.elementId]] = [[e.id]]');
150+
$rows = $rowQuery->all();
152151

153-
if ($value && is_numeric($value)) {
154-
$doesExist = (new Query())
155-
->select('id')
156-
->where(['id' => $value])
157-
->from('{{%elements}}')
158-
->exists();
152+
foreach ($rows as $row) {
159153

160-
if (!$doesExist) {
161-
$value = null;
154+
if (substr($row[$columnName], 0, 1) != "{") {
155+
continue;
156+
}
157+
158+
$payload = Json::decode((string)$row[$columnName], $associative=true, $depth=512, JSON_THROW_ON_ERROR);
159+
160+
if (!is_array($payload)) {
161+
continue;
162+
}
163+
164+
$type = $payload['type'] ?? null;
165+
$value = $payload['value'] ?? '';
166+
unset($payload['type']);
167+
unset($payload['value']);
168+
169+
if ($value && is_numeric($value)) {
170+
$doesExist = (new Query())
171+
->select('id')
172+
->where(['id' => $value])
173+
->from('{{%elements}}')
174+
->exists();
175+
176+
if (!$doesExist) {
177+
$value = null;
178+
}
162179
}
163-
}
164180

165-
$insertRows[] = [
166-
$row['elementId'], // elementId
167-
$row['siteId'], // siteId
168-
$field->id, // fieldId
169-
is_numeric($value) ? $value : null, // linkedId
170-
is_numeric($value) ? $row['siteId'] : null, // linkedSiteId
171-
$type, // type
172-
is_numeric($value) ? null : $value, // linkedUrl
173-
Json::encode($payload) // payload
174-
];
175-
176-
if (count($insertRows) > 100) {
177-
$writeRows($insertRows);
178-
$insertRows = [];
181+
$insertRows[] = [
182+
$row['elementId'], // elementId
183+
$row['siteId'], // siteId
184+
$field->id, // fieldId
185+
is_numeric($value) ? $value : null, // linkedId
186+
is_numeric($value) ? $row['siteId'] : null, // linkedSiteId
187+
$type, // type
188+
is_numeric($value) ? null : $value, // linkedUrl
189+
Json::encode($payload) // payload
190+
];
191+
192+
if (count($insertRows) > 100) {
193+
$writeRows($insertRows);
194+
$insertRows = [];
195+
}
196+
if (!in_array($columnName, $columnsToDrop)) {
197+
$columnsToDrop[] = $columnName;
198+
}
179199
}
180200
}
181201

182202
$writeRows($insertRows);
183-
$this->dropColumn($table, $columnName);
203+
204+
foreach ($columnsToDrop as $col) {
205+
$yiiTable = Yii::$app->db->schema->getTableSchema($table);
206+
if (isset($yiiTable->columns[$columnName])) {
207+
$this->dropColumn($table, $col);
208+
}
209+
}
210+
//
184211
}
185212

186213
/**

0 commit comments

Comments
 (0)