Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating migration to fix https://github.com/sebastian-lenz/craft-li… #1

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 66 additions & 39 deletions src/migrations/m190417_202153_migrateDataToTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use lenz\linkfield\fields\LinkField;
use lenz\linkfield\records\LinkRecord;
use verbb\supertable\fields\SuperTableField;
use Yii;

/**
* m190417_202153_migrateDataToTable migration.
Expand Down Expand Up @@ -119,7 +120,11 @@ private function updateMatrixField(Matrix $matrixField): void {
*/
private function updateLinkField(LinkField $field, string $table, string $handlePrefix = ''): void {
$insertRows = [];
$columnsToDrop = [];
$columnName = ($field->columnPrefix ?: 'field_') . $handlePrefix . $field->handle;
if (!$this->db->tableExists($table)) {
return;
}
if ($field->columnSuffix) {
$columnName .= '_' . $field->columnSuffix;
}
Expand All @@ -132,55 +137,77 @@ private function updateLinkField(LinkField $field, string $table, string $handle
}
};

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

foreach ($rows as $row) {
$payload = Json::decode($row[$columnName]);
if (!is_array($payload)) {
continue;
}

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

if ($value && is_numeric($value)) {
$doesExist = (new Query())
->select('id')
->where(['id' => $value])
->from('{{%elements}}')
->exists();
foreach ($rows as $row) {

if (!$doesExist) {
$value = null;
if (substr($row[$columnName], 0, 1) != "{") {
continue;
}

$payload = Json::decode((string)$row[$columnName], $associative=true, $depth=512, JSON_THROW_ON_ERROR);

if (!is_array($payload)) {
continue;
}

$type = $payload['type'] ?? null;
$value = $payload['value'] ?? '';
unset($payload['type']);
unset($payload['value']);

if ($value && is_numeric($value)) {
$doesExist = (new Query())
->select('id')
->where(['id' => $value])
->from('{{%elements}}')
->exists();

if (!$doesExist) {
$value = null;
}
}
}

$insertRows[] = [
$row['elementId'], // elementId
$row['siteId'], // siteId
$field->id, // fieldId
is_numeric($value) ? $value : null, // linkedId
is_numeric($value) ? $row['siteId'] : null, // linkedSiteId
$type, // type
is_numeric($value) ? null : $value, // linkedUrl
Json::encode($payload) // payload
];

if (count($insertRows) > 100) {
$writeRows($insertRows);
$insertRows = [];
$insertRows[] = [
$row['elementId'], // elementId
$row['siteId'], // siteId
$field->id, // fieldId
is_numeric($value) ? $value : null, // linkedId
is_numeric($value) ? $row['siteId'] : null, // linkedSiteId
$type, // type
is_numeric($value) ? null : $value, // linkedUrl
Json::encode($payload) // payload
];

if (count($insertRows) > 100) {
$writeRows($insertRows);
$insertRows = [];
}
if (!in_array($columnName, $columnsToDrop)) {
$columnsToDrop[] = $columnName;
}
}
}

$writeRows($insertRows);
$this->dropColumn($table, $columnName);

foreach ($columnsToDrop as $col) {
$yiiTable = Yii::$app->db->schema->getTableSchema($table);
if (isset($yiiTable->columns[$columnName])) {
$this->dropColumn($table, $col);
}
}
//
}

/**
Expand Down