* [FIX] Fix custom fields migration issue. Thanks to @VexedSyd for the feedback. Closes #1273

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2019-04-27 01:55:28 +02:00
parent 6e1f04ddc5
commit cf2becfcfe
4 changed files with 94 additions and 1 deletions

View File

@@ -44,7 +44,8 @@ final class UpgradeAppService extends Service implements UpgradeInterface
'300.18010101',
'300.18072901',
'300.18072902',
'310.19012201'
'310.19012201',
'310.19042701'
];
/**
@@ -126,6 +127,10 @@ final class UpgradeAppService extends Service implements UpgradeInterface
$this->dic->get(UpgradePlugin::class)
->upgrade_310_19012201();
return true;
case '310.19042701':
$this->dic->get(UpgradeCustomFieldDefinition::class)
->upgrade_310_19042701();
return true;
}
} catch (Exception $e) {
processException($e);

View File

@@ -206,6 +206,70 @@ final class UpgradeCustomFieldDefinition extends Service
);
}
/**
* upgrade_300_19042701
*
* @throws Exception
*/
public function upgrade_310_19042701()
{
if (!in_array('field', $this->db->getColumnsForTable('CustomFieldDefinition'))) {
return;
}
$this->eventDispatcher->notifyEvent('upgrade.customField.start',
new Event($this, EventMessage::factory()
->addDescription(__u('Custom fields update'))
->addDescription(__FUNCTION__))
);
try {
$customFieldTypeService = $this->dic->get(CustomFieldTypeService::class);
$customFieldType = [];
foreach ($customFieldTypeService->getAll() as $customFieldTypeData) {
$customFieldType[$customFieldTypeData->getName()] = $customFieldTypeData->getId();
}
$this->transactionAware(function () use ($customFieldType) {
$queryData = new QueryData();
$queryData->setQuery('SELECT id, field FROM CustomFieldDefinition WHERE field IS NOT NULL');
foreach ($this->db->doSelect($queryData)->getDataAsArray() as $item) {
/** @var CustomFieldDefDataOld $data */
$data = Util::unserialize(CustomFieldDefDataOld::class, $item->field, 'SP\DataModel\CustomFieldDefData');
$typeId = $customFieldType[$this->typeMapper((int)$data->getType())];
$queryData = new QueryData();
$queryData->setQuery('UPDATE CustomFieldDefinition SET typeId = ? WHERE id = ? LIMIT 1');
$queryData->setParams([$typeId, $item->id]);
$this->db->doQuery($queryData);
$this->eventDispatcher->notifyEvent('upgrade.customField.process',
new Event($this, EventMessage::factory()
->addDescription(__u('Field updated'))
->addDetail(__u('Field'), $data->getName()))
);
}
});
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
throw $e;
}
$this->eventDispatcher->notifyEvent('upgrade.customField.end',
new Event($this, EventMessage::factory()
->addDescription(__u('Custom fields update'))
->addDescription(__FUNCTION__))
);
}
protected function initialize()
{
$this->db = $this->dic->get(Database::class);

View File

@@ -392,4 +392,21 @@ final class Database implements DatabaseInterface
return $result;
}
/**
* @param $table
*
* @return array
*/
public function getColumnsForTable($table): array
{
$conn = $this->dbHandler->getConnection()->query("SELECT * FROM $table LIMIT 0");
$columns = [];
for ($i = 0; $i < $conn->columnCount(); $i++) {
$columns[] = $conn->getColumnMeta($i)['name'];
}
return $columns;
}
}

View File

@@ -109,4 +109,11 @@ interface DatabaseInterface
* @return bool
*/
public function rollbackTransaction();
/**
* @param $table
*
* @return array
*/
public function getColumnsForTable($table): array;
}