From cf2becfcfe34b7ee951f5bc3cf83f556894ca3a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 27 Apr 2019 01:55:28 +0200 Subject: [PATCH] * [FIX] Fix custom fields migration issue. Thanks to @VexedSyd for the feedback. Closes #1273 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/SP/Services/Upgrade/UpgradeAppService.php | 7 +- .../Upgrade/UpgradeCustomFieldDefinition.php | 64 +++++++++++++++++++ lib/SP/Storage/Database/Database.php | 17 +++++ lib/SP/Storage/Database/DatabaseInterface.php | 7 ++ 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/lib/SP/Services/Upgrade/UpgradeAppService.php b/lib/SP/Services/Upgrade/UpgradeAppService.php index dc2e28c3..534521e6 100644 --- a/lib/SP/Services/Upgrade/UpgradeAppService.php +++ b/lib/SP/Services/Upgrade/UpgradeAppService.php @@ -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); diff --git a/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php b/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php index fad92475..dbd2ac83 100644 --- a/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php +++ b/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php @@ -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); diff --git a/lib/SP/Storage/Database/Database.php b/lib/SP/Storage/Database/Database.php index 568b879d..b9861ff7 100644 --- a/lib/SP/Storage/Database/Database.php +++ b/lib/SP/Storage/Database/Database.php @@ -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; + } } \ No newline at end of file diff --git a/lib/SP/Storage/Database/DatabaseInterface.php b/lib/SP/Storage/Database/DatabaseInterface.php index 29d38801..5b40645c 100644 --- a/lib/SP/Storage/Database/DatabaseInterface.php +++ b/lib/SP/Storage/Database/DatabaseInterface.php @@ -109,4 +109,11 @@ interface DatabaseInterface * @return bool */ public function rollbackTransaction(); + + /** + * @param $table + * + * @return array + */ + public function getColumnsForTable($table): array; } \ No newline at end of file