Skip to content
Snippets Groups Projects
Commit 944d59a6 authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[BUGFIX] Replace AbstractUpdate class

parent ee131407
No related branches found
No related tags found
1 merge request!23Feature upgrade to typo3 10
...@@ -28,7 +28,8 @@ namespace SGalinski\SgJobs\Updates; ...@@ -28,7 +28,8 @@ namespace SGalinski\SgJobs\Updates;
use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility; 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 * Class DepartmentUpdateWizard
...@@ -36,48 +37,76 @@ use TYPO3\CMS\Install\Updates\AbstractUpdate; ...@@ -36,48 +37,76 @@ use TYPO3\CMS\Install\Updates\AbstractUpdate;
* @package SGalinski\SgJobs\Updates * @package SGalinski\SgJobs\Updates
* @author Kevin Ditscheid <kevin.ditscheid@sgalinski.de> * @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
* @return bool Whether an update is required (TRUE) or not (FALSE)
*/ */
public function checkForUpdate(&$description): bool { protected function hasAreaField(): bool {
if ($this->isWizardDone()) { return $this->hasField('area');
return FALSE; }
}
if (!$this->checkIfTableExists('tx_sgjobs_domain_model_department')) {
return FALSE;
}
if ( /**
$this->hasAreaField() || * Check if the job database table has a field with the given name in it
$this->hasDeletedAreaField() *
) { * @param $fieldName
$description = 'Job areas can be migrated to the new department records'; * @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 TRUE;
} }
return FALSE; return FALSE;
} }
/** /**
* Performs the accordant updates. * Get the database query builder
* *
* @param array &$dbQueries Queries done in this update * @return Connection
* @param string &$customMessage Custom message
* @return bool Whether everything went smoothly or not
*/ */
public function performUpdate(array &$dbQueries, &$customMessage): bool { protected function getDatabaseConnection(): Connection {
$customMessage = ''; $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(); $connection = $this->getDatabaseConnection();
$queryBuilder = $connection->createQueryBuilder(); $queryBuilder = $connection->createQueryBuilder();
$field = $this->hasDeletedAreaField() ? 'zzz_deleted_area' : 'area'; $field = $this->hasDeletedAreaField() ? 'zzz_deleted_area' : 'area';
...@@ -87,8 +116,6 @@ class DepartmentUpdateWizard extends AbstractUpdate { ...@@ -87,8 +116,6 @@ class DepartmentUpdateWizard extends AbstractUpdate {
->execute() ->execute()
->fetchAll(); ->fetchAll();
$dbQueries[] = $queryBuilder->getSQL();
$newDepartments = []; $newDepartments = [];
if (\count($jobs) > 0) { if (\count($jobs) > 0) {
foreach ($jobs as $index => $job) { foreach ($jobs as $index => $job) {
...@@ -100,8 +127,6 @@ class DepartmentUpdateWizard extends AbstractUpdate { ...@@ -100,8 +127,6 @@ class DepartmentUpdateWizard extends AbstractUpdate {
'sys_language_uid' => $job['sys_language_uid'] 'sys_language_uid' => $job['sys_language_uid']
]; ];
} else { } 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]); unset($jobs[$index]);
} }
} }
...@@ -109,11 +134,11 @@ class DepartmentUpdateWizard extends AbstractUpdate { ...@@ -109,11 +134,11 @@ class DepartmentUpdateWizard extends AbstractUpdate {
/** @noinspection NotOptimalIfConditionsInspection */ /** @noinspection NotOptimalIfConditionsInspection */
if (\count($jobs) > 0) { if (\count($jobs) > 0) {
\array_walk( \array_walk(
$newDepartments, function ($department) use (&$dbQueries, $connection) { $newDepartments, function ($department) use (&$dbQueries, $connection) {
$queryBuilder = $connection->createQueryBuilder(); $queryBuilder = $connection->createQueryBuilder();
$queryBuilder->insert('tx_sgjobs_domain_model_department')->values($department)->execute(); $queryBuilder->insert('tx_sgjobs_domain_model_department')->values($department)->execute();
$dbQueries[] = $queryBuilder->getSQL(); $dbQueries[] = $queryBuilder->getSQL();
} }
); );
$queryBuilder->resetQueryParts(); $queryBuilder->resetQueryParts();
...@@ -143,7 +168,7 @@ class DepartmentUpdateWizard extends AbstractUpdate { ...@@ -143,7 +168,7 @@ class DepartmentUpdateWizard extends AbstractUpdate {
/** @noinspection NotOptimalIfConditionsInspection */ /** @noinspection NotOptimalIfConditionsInspection */
if (\count($jobs) > 0) { if (\count($jobs) > 0) {
\array_walk( \array_walk(
$jobs, function ($job) use (&$dbQueries, $connection) { $jobs, function ($job) use ($connection) {
$queryBuilder = $connection->createQueryBuilder(); $queryBuilder = $connection->createQueryBuilder();
$queryBuilder->update('tx_sgjobs_domain_model_job') $queryBuilder->update('tx_sgjobs_domain_model_job')
->set('department', (int) $job['department']) ->set('department', (int) $job['department'])
...@@ -153,56 +178,44 @@ class DepartmentUpdateWizard extends AbstractUpdate { ...@@ -153,56 +178,44 @@ class DepartmentUpdateWizard extends AbstractUpdate {
$queryBuilder->createNamedParameter($job['uid'], \PDO::PARAM_INT) $queryBuilder->createNamedParameter($job['uid'], \PDO::PARAM_INT)
) )
)->execute(); )->execute();
$dbQueries[] = $queryBuilder->getSQL();
} }
); );
} }
} }
} }
$this->markWizardAsDone();
return TRUE;
}
/** return TRUE;
* Check if the job table has a deleted area field
*
* @return bool
*/
protected function hasDeletedAreaField(): bool {
return $this->hasField('zzz_deleted_area');
} }
/** /**
* Check if the job table has an area field
*
* @return bool * @return bool
*/ */
protected function hasAreaField(): bool { public function updateNecessary(): bool {
return $this->hasField('area'); if (
} !GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_sgjobs_domain_model_department')
->getSchemaManager()
->tablesExist(['tx_sgjobs_domain_model_department'])
) {
return FALSE;
}
/** if (
* Check if the job database table has a field with the given name in it $this->hasAreaField() ||
* $this->hasDeletedAreaField()
* @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 TRUE;
} }
return FALSE; return FALSE;
} }
/** /**
* Get the database query builder * @return array|string[]
*
* @return Connection
*/ */
protected function getDatabaseConnection(): Connection { public function getPrerequisites(): array {
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); return [
return $connectionPool->getConnectionForTable('tx_sgjobs_domain_model_job'); DatabaseUpdatedPrerequisite::class
];
} }
} }
...@@ -98,7 +98,7 @@ call_user_func( ...@@ -98,7 +98,7 @@ call_user_func(
); );
// Register the upgrade wizard // 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; = \SGalinski\SgJobs\Updates\DepartmentUpdateWizard::class;
if (version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '>')) { if (version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '>')) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment