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

Support large tables in addSitesUpgradeWizard #63

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions Classes/Update/AddSites/AbstractSiteUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace In2code\Lux\Update\AddSites;

use In2code\Lux\Domain\Service\SiteService;
use In2code\Lux\Utility\DatabaseUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;

abstract class AbstractSiteUpgrade
Expand All @@ -25,6 +29,36 @@ public function __construct()
$this->siteService = GeneralUtility::makeInstance(SiteService::class);
}

public function run(): void
{
$connection = $this->getConnectionPool()->getConnectionForTable(static::TABLE_NAME);

$queryBuilder = $this->getPreparedQueryBuilder();
$result = $queryBuilder
->select('page')
->groupBy('page')
->where(
$queryBuilder->expr()->eq('site', $queryBuilder->createNamedParameter(''))
)
->executeQuery();
while ($record = $result->fetchAssociative()) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
if ($siteIdentifier !== '') {
$connection->update(
static::TABLE_NAME,
['site' => $siteIdentifier],
['page' => (int)$record['page']]
);
} else {
$connection->update(
static::TABLE_NAME,
['deleted' => 1],
['page' => (int)$record['page']]
);
}
}
}

protected function getSiteIdentifierFromPage(int $pageIdentifier): string
{
if (array_key_exists($pageIdentifier, $this->mapping)) {
Expand All @@ -38,4 +72,17 @@ protected function getSiteIdentifierFromPage(int $pageIdentifier): string
$this->mapping[$pageIdentifier] = $siteIdentifier;
return $siteIdentifier;
}

protected function getPreparedQueryBuilder(): QueryBuilder
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable(static::TABLE_NAME);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$queryBuilder->from(static::TABLE_NAME);
return $queryBuilder;
}

protected function getConnectionPool(): ConnectionPool
{
return GeneralUtility::makeInstance(ConnectionPool::class);
}
}
14 changes: 1 addition & 13 deletions Classes/Update/AddSites/AddSitesForDownloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,5 @@

class AddSitesForDownloads extends AbstractSiteUpgrade
{
public function run(): void
{
$connection = DatabaseUtility::getConnectionForTable(Download::TABLE_NAME);
$records = $connection
->executeQuery('select * from ' . Download::TABLE_NAME . ' where deleted=0')
->fetchAllAssociative();
foreach ($records as $record) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
$connection->executeQuery(
'update ' . Download::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid']
);
}
}
protected const TABLE_NAME = Download::TABLE_NAME;
}
14 changes: 1 addition & 13 deletions Classes/Update/AddSites/AddSitesForLinkclick.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,5 @@

class AddSitesForLinkclick extends AbstractSiteUpgrade
{
public function run(): void
{
$connection = DatabaseUtility::getConnectionForTable(Linkclick::TABLE_NAME);
$records = $connection
->executeQuery('select * from ' . Linkclick::TABLE_NAME . ' where deleted=0')
->fetchAllAssociative();
foreach ($records as $record) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
$connection->executeQuery(
'update ' . Linkclick::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid']
);
}
}
protected const TABLE_NAME = Linkclick::TABLE_NAME;
}
14 changes: 1 addition & 13 deletions Classes/Update/AddSites/AddSitesForPagevisits.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,5 @@

class AddSitesForPagevisits extends AbstractSiteUpgrade
{
public function run(): void
{
$connection = DatabaseUtility::getConnectionForTable(Pagevisit::TABLE_NAME);
$records = $connection
->executeQuery('select * from ' . Pagevisit::TABLE_NAME . ' where deleted=0')
->fetchAllAssociative();
foreach ($records as $record) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
$connection->executeQuery(
'update ' . Pagevisit::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid']
);
}
}
protected const TABLE_NAME = Pagevisit::TABLE_NAME;
}
9 changes: 6 additions & 3 deletions Classes/Utility/DatabaseUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class DatabaseUtility
Expand Down Expand Up @@ -107,12 +108,14 @@ public static function isFieldInTableFilled(string $fieldName, string $tableName
public static function isAnyFieldFilledInTable(string $fieldName, string $tableName): bool
{
$queryBuilder = self::getQueryBuilderForTable($tableName);
return $queryBuilder
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$result = $queryBuilder
->select($fieldName)
->from($tableName)
->where($fieldName . ' != \'\'')
->where($fieldName . ' = \'\'')
->setMaxResults(1)
->executeQuery()
->fetchOne() !== false;
->fetchOne();
return $result === false;
}
}