Skip to content

Commit

Permalink
Rework sql list
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Oct 18, 2024
1 parent deccebd commit c0e3ffa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 50 deletions.
36 changes: 5 additions & 31 deletions classes/Progress/Backlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,42 +106,16 @@ public function getFirstValue()
}

/**
* @param int|string $key
* @param mixed $content
* @param int|string $backlogKey
*/
public function updateItem($key, $content): void
public function removeAt($backlogKey): void
{
if (empty($content)) {
unset($this->backlog[$key]);
} else {
$this->backlog[$key] = $content;
}
unset($this->backlog[$backlogKey]);
}

public function getRemainingTotal(int $depth = 1): int
{
return $this->countElements($this->backlog, $depth);
}

/**
* @param mixed[] $array
*/
private function countElements(array $array, int $depth): int
public function getRemainingTotal(): int
{
if ($depth === 1) {
return count($array);
}

$total = 0;
foreach ($array as $value) {
if (is_array($value)) {
$total += $this->countElements($value, $depth - 1);
} else {
++$total;
}
}

return $total;
return count($this->backlog);
}

public function getInitialTotal(): int
Expand Down
4 changes: 2 additions & 2 deletions classes/Progress/CompletionCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function getBasePercentageOfTask(string $taskName): int
/**
* @return int<0, 100>
*/
public function computePercentage(Backlog $backlog, string $currentTaskClass, string $nextTaskClass, int $backlogDepth = 1): int
public function computePercentage(Backlog $backlog, string $currentTaskClass, string $nextTaskClass): int
{
$currentBaseProgress = $this->getBasePercentageOfTask($currentTaskClass);
$nextBaseProgress = $this->getBasePercentageOfTask($nextTaskClass);
Expand All @@ -108,6 +108,6 @@ public function computePercentage(Backlog $backlog, string $currentTaskClass, st
}

// Casting as integer is equivalent to using floor(), and we want to round down.
return (int) ($currentBaseProgress + (($nextBaseProgress - $currentBaseProgress) * ($backlog->getInitialTotal() - $backlog->getRemainingTotal($backlogDepth)) / $backlog->getInitialTotal()));
return (int) ($currentBaseProgress + (($nextBaseProgress - $currentBaseProgress) * ($backlog->getInitialTotal() - $backlog->getRemainingTotal()) / $backlog->getInitialTotal()));
}
}
20 changes: 5 additions & 15 deletions classes/Task/Update/UpdateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,13 @@ public function run(): int
$this->warmUp();
$originVersion = $this->container->getState()->getOriginVersion();
$sqlContentList = $this->getCoreUpgrader()->getSqlContentList($originVersion);

$totalQueries = 0;
foreach ($sqlContentList as $queries) {
$totalQueries += count($queries);
}

$backlog = new Backlog($sqlContentList, $totalQueries);
$backlog = new Backlog($sqlContentList, count($sqlContentList));
} else {
$this->getCoreUpgrader()->setupUpdateEnvironment();
$backlog = Backlog::fromContents($this->container->getFileConfigurationStorage()->load(UpgradeFileNames::SQL_TO_EXECUTE_LIST));
}

if ($backlog->getRemainingTotal(2) > 0) {
if ($backlog->getRemainingTotal() > 0) {
$this->logger->info($this->translator->trans('Update database in progress. %d queries left', [$backlog->getRemainingTotal(2)]));

Check failure on line 65 in classes/Task/Update/UpdateDatabase.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Progress\Backlog::getRemainingTotal() invoked with 1 parameter, 0 required.

Check failure on line 65 in classes/Task/Update/UpdateDatabase.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Progress\Backlog::getRemainingTotal() invoked with 1 parameter, 0 required.

Check failure on line 65 in classes/Task/Update/UpdateDatabase.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Method PrestaShop\Module\AutoUpgrade\Progress\Backlog::getRemainingTotal() invoked with 1 parameter, 0 required.

Check failure on line 65 in classes/Task/Update/UpdateDatabase.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.5.1)

Method PrestaShop\Module\AutoUpgrade\Progress\Backlog::getRemainingTotal() invoked with 1 parameter, 0 required.

Check failure on line 65 in classes/Task/Update/UpdateDatabase.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8)

Method PrestaShop\Module\AutoUpgrade\Progress\Backlog::getRemainingTotal() invoked with 1 parameter, 0 required.

$this->updateDatabase($backlog);
Expand Down Expand Up @@ -182,16 +176,12 @@ protected function checkVersionIsNewer(): void

protected function updateDatabase(Backlog $backlog): void
{
$upgradeFile = $backlog->getFirstKey();
$sqlContent = $backlog->getFirstValue();
$key = $backlog->getFirstKey();

$query = reset($sqlContent);
$queryIndex = key($sqlContent);

$this->getCoreUpgrader()->runQuery($upgradeFile, $query);
$this->getCoreUpgrader()->runQuery($sqlContent['version'], $sqlContent['query']);
$backlog->removeAt($key);

unset($sqlContent[$queryIndex]);
$backlog->updateItem($upgradeFile, $sqlContent);
$this->container->getFileConfigurationStorage()->save($backlog->dump(), UpgradeFileNames::SQL_TO_EXECUTE_LIST);
}
}
9 changes: 7 additions & 2 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function getSqlContentList(string $originVersion): array
*
* @param array<string, string> $sqlFiles
*
* @return array<string, string[]> of SQL requests per version
* @return array<array{'version':string,'query':string}> of SQL requests per version
*/
protected function applySqlParams(array $sqlFiles): array
{
Expand All @@ -319,7 +319,12 @@ protected function applySqlParams(array $sqlFiles): array
$sqlContent = file_get_contents($file) . "\n";
$sqlContent = str_replace($search, $replace, $sqlContent);
$sqlContent = array_filter(preg_split("/;\s*[\r\n]+/", $sqlContent));
$sqlRequests[$version] = $sqlContent;
foreach ($sqlContent as $query) {
$sqlRequests[] = [
'version' => $version,
'query' => $query,
];
}
}

return $sqlRequests;
Expand Down

0 comments on commit c0e3ffa

Please sign in to comment.