From 944d59a6f8c7ffee5c284991dd35479816d38a2e Mon Sep 17 00:00:00 2001 From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de> Date: Thu, 9 Jul 2020 12:51:11 +0200 Subject: [PATCH] [BUGFIX] Replace AbstractUpdate class --- Classes/Updates/DepartmentUpdateWizard.php | 151 +++++++++++---------- ext_localconf.php | 2 +- 2 files changed, 83 insertions(+), 70 deletions(-) diff --git a/Classes/Updates/DepartmentUpdateWizard.php b/Classes/Updates/DepartmentUpdateWizard.php index 5bb0e8b6..6515b6d0 100644 --- a/Classes/Updates/DepartmentUpdateWizard.php +++ b/Classes/Updates/DepartmentUpdateWizard.php @@ -28,7 +28,8 @@ namespace SGalinski\SgJobs\Updates; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Install\Updates\AbstractUpdate; +use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite; +use TYPO3\CMS\Install\Updates\UpgradeWizardInterface; /** * Class DepartmentUpdateWizard @@ -36,48 +37,76 @@ use TYPO3\CMS\Install\Updates\AbstractUpdate; * @package SGalinski\SgJobs\Updates * @author Kevin Ditscheid <kevin.ditscheid@sgalinski.de> */ -class DepartmentUpdateWizard extends AbstractUpdate { +class DepartmentUpdateWizard implements UpgradeWizardInterface { /** - * @var string + * Check if the job table has a deleted area field + * + * @return bool */ - protected $title = 'Migrate old fulltext job areas to department records'; + protected function hasDeletedAreaField(): bool { + return $this->hasField('zzz_deleted_area'); + } /** - * Checks whether updates are required. + * Check if the job table has an area field * - * @param string &$description The description for the update - * @return bool Whether an update is required (TRUE) or not (FALSE) + * @return bool */ - public function checkForUpdate(&$description): bool { - if ($this->isWizardDone()) { - return FALSE; - } - - if (!$this->checkIfTableExists('tx_sgjobs_domain_model_department')) { - return FALSE; - } + protected function hasAreaField(): bool { + return $this->hasField('area'); + } - if ( - $this->hasAreaField() || - $this->hasDeletedAreaField() - ) { - $description = 'Job areas can be migrated to the new department records'; + /** + * Check if the job database table has a field with the given name in it + * + * @param $fieldName + * @return bool + */ + protected function hasField($fieldName): bool { + $connection = $this->getDatabaseConnection(); + $tableColumns = $connection->getSchemaManager()->listTableColumns('tx_sgjobs_domain_model_job'); + if (\array_key_exists($fieldName, $tableColumns)) { return TRUE; } - return FALSE; } /** - * Performs the accordant updates. + * Get the database query builder * - * @param array &$dbQueries Queries done in this update - * @param string &$customMessage Custom message - * @return bool Whether everything went smoothly or not + * @return Connection */ - public function performUpdate(array &$dbQueries, &$customMessage): bool { - $customMessage = ''; + protected function getDatabaseConnection(): Connection { + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); + return $connectionPool->getConnectionForTable('tx_sgjobs_domain_model_job'); + } + + /** + * @return string + */ + public function getIdentifier(): string { + return 'tx_sgjobs_departmentupdatewizward'; + } + + /** + * @return string + */ + public function getTitle(): string { + return 'Migrate old fulltext job areas to department records'; + } + + /** + * @return string + */ + public function getDescription(): string { + return ''; + } + + /** + * @return bool + */ + public function executeUpdate(): bool { $connection = $this->getDatabaseConnection(); $queryBuilder = $connection->createQueryBuilder(); $field = $this->hasDeletedAreaField() ? 'zzz_deleted_area' : 'area'; @@ -87,8 +116,6 @@ class DepartmentUpdateWizard extends AbstractUpdate { ->execute() ->fetchAll(); - $dbQueries[] = $queryBuilder->getSQL(); - $newDepartments = []; if (\count($jobs) > 0) { foreach ($jobs as $index => $job) { @@ -100,8 +127,6 @@ class DepartmentUpdateWizard extends AbstractUpdate { 'sys_language_uid' => $job['sys_language_uid'] ]; } else { - $customMessage .= 'The Job with id ' . $job['uid'] . ' does not have an area set.' - . 'Please set a department manually later, because it is a required field now!' . \PHP_EOL; unset($jobs[$index]); } } @@ -109,11 +134,11 @@ class DepartmentUpdateWizard extends AbstractUpdate { /** @noinspection NotOptimalIfConditionsInspection */ if (\count($jobs) > 0) { \array_walk( - $newDepartments, function ($department) use (&$dbQueries, $connection) { - $queryBuilder = $connection->createQueryBuilder(); - $queryBuilder->insert('tx_sgjobs_domain_model_department')->values($department)->execute(); - $dbQueries[] = $queryBuilder->getSQL(); - } + $newDepartments, function ($department) use (&$dbQueries, $connection) { + $queryBuilder = $connection->createQueryBuilder(); + $queryBuilder->insert('tx_sgjobs_domain_model_department')->values($department)->execute(); + $dbQueries[] = $queryBuilder->getSQL(); + } ); $queryBuilder->resetQueryParts(); @@ -143,7 +168,7 @@ class DepartmentUpdateWizard extends AbstractUpdate { /** @noinspection NotOptimalIfConditionsInspection */ if (\count($jobs) > 0) { \array_walk( - $jobs, function ($job) use (&$dbQueries, $connection) { + $jobs, function ($job) use ($connection) { $queryBuilder = $connection->createQueryBuilder(); $queryBuilder->update('tx_sgjobs_domain_model_job') ->set('department', (int) $job['department']) @@ -153,56 +178,44 @@ class DepartmentUpdateWizard extends AbstractUpdate { $queryBuilder->createNamedParameter($job['uid'], \PDO::PARAM_INT) ) )->execute(); - $dbQueries[] = $queryBuilder->getSQL(); } ); } } } - $this->markWizardAsDone(); - return TRUE; - } - /** - * Check if the job table has a deleted area field - * - * @return bool - */ - protected function hasDeletedAreaField(): bool { - return $this->hasField('zzz_deleted_area'); + return TRUE; } /** - * Check if the job table has an area field - * * @return bool */ - protected function hasAreaField(): bool { - return $this->hasField('area'); - } + public function updateNecessary(): bool { + if ( + !GeneralUtility::makeInstance(ConnectionPool::class) + ->getConnectionForTable('tx_sgjobs_domain_model_department') + ->getSchemaManager() + ->tablesExist(['tx_sgjobs_domain_model_department']) + ) { + return FALSE; + } - /** - * Check if the job database table has a field with the given name in it - * - * @param $fieldName - * @return bool - */ - protected function hasField($fieldName): bool { - $connection = $this->getDatabaseConnection(); - $tableColumns = $connection->getSchemaManager()->listTableColumns('tx_sgjobs_domain_model_job'); - if (\array_key_exists($fieldName, $tableColumns)) { + if ( + $this->hasAreaField() || + $this->hasDeletedAreaField() + ) { return TRUE; } + return FALSE; } /** - * Get the database query builder - * - * @return Connection + * @return array|string[] */ - protected function getDatabaseConnection(): Connection { - $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); - return $connectionPool->getConnectionForTable('tx_sgjobs_domain_model_job'); + public function getPrerequisites(): array { + return [ + DatabaseUpdatedPrerequisite::class + ]; } } diff --git a/ext_localconf.php b/ext_localconf.php index b172f8aa..6ec77b8e 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -98,7 +98,7 @@ call_user_func( ); // Register the upgrade wizard - $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgJobs\Updates\DepartmentUpdateWizard::class] + $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['tx_sgjobs_departmentupdatewizward'] = \SGalinski\SgJobs\Updates\DepartmentUpdateWizard::class; if (version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '>')) { -- GitLab