Changed storage of executed upgrades to DB

This commit is contained in:
Marcin Kurczewski 2014-11-09 17:32:16 +01:00
parent fc71007063
commit 5ceecbd0ca
3 changed files with 50 additions and 18 deletions

View file

@ -10,7 +10,7 @@ final class UpgradeService
private $config; private $config;
private $upgrades; private $upgrades;
private $databaseConnection; private $databaseConnection;
private $executedUpgradeNames = []; private $executedUpgradeNumbers = [];
public function __construct( public function __construct(
Config $config, Config $config,
@ -20,7 +20,7 @@ final class UpgradeService
$this->config = $config; $this->config = $config;
$this->databaseConnection = $databaseConnection; $this->databaseConnection = $databaseConnection;
$this->upgrades = $upgradeRepository->getUpgrades(); $this->upgrades = $upgradeRepository->getUpgrades();
$this->loadExecutedUpgradeNames(); $this->loadExecutedUpgradeNumbers();
} }
public function runUpgradesVerbose() public function runUpgradesVerbose()
@ -48,32 +48,37 @@ final class UpgradeService
private function isUpgradeNeeded(IUpgrade $upgrade) private function isUpgradeNeeded(IUpgrade $upgrade)
{ {
return !in_array(get_class($upgrade), $this->executedUpgradeNames); return !in_array($this->getUpgradeNumber($upgrade), $this->executedUpgradeNumbers);
} }
private function runUpgrade(IUpgrade $upgrade) private function runUpgrade(IUpgrade $upgrade)
{ {
$upgrade->run($this->databaseConnection); $upgrade->run($this->databaseConnection);
$this->executedUpgradeNames[] = get_class($upgrade); $number = $this->getUpgradeNumber($upgrade);
$this->saveExecutedUpgradeNames(); $this->executedUpgradeNumbers[] = $number;
$this->databaseConnection->getPDO()->insertInto('executedUpgrades')->values(['number' => $number])->execute();
} }
private function loadExecutedUpgradeNames() private function loadExecutedUpgradeNumbers()
{ {
$infoFilePath = $this->getExecutedUpgradeNamesFilePath(); $this->executedUpgradeNumbers = [];
if (!file_exists($infoFilePath)) try
return; {
$this->executedUpgradeNames = explode("\n", file_get_contents($infoFilePath)); foreach ($this->databaseConnection->getPDO()->from('executedUpgrades') as $row)
{
$this->executedUpgradeNumbers[] = intval($row['number']);
}
}
catch (\Exception $e)
{
//most probably, no table found - need to execute all upgrades
}
} }
private function saveExecutedUpgradeNames() private function getUpgradeNumber(IUpgrade $upgrade)
{ {
$infoFilePath = $this->getExecutedUpgradeNamesFilePath(); $className = get_class($upgrade);
file_put_contents($infoFilePath, implode("\n", $this->executedUpgradeNames)); preg_match('/(\d+)/', $className, $matches);
} return intval($matches[1]);
private function getExecutedUpgradeNamesFilePath()
{
return $this->config->getDataDirectory() . DIRECTORY_SEPARATOR . 'executed_upgrades.txt';
} }
} }

View file

@ -0,0 +1,26 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\DatabaseConnection;
class Upgrade00 implements IUpgrade
{
public function run(DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->getPDO();
$pdo->exec('CREATE TABLE executedUpgrades (number INT NOT NULL)');
$oldFilePath = __DIR__ . '/../../data/executed_upgrades.txt';
if (file_exists($oldFilePath))
{
foreach (explode("\n", file_get_contents($oldFilePath)) as $className)
{
if (preg_match('/(\d+)/', $className, $matches))
{
$number = intval($matches[1]);
$pdo->insertInto('executedUpgrades')->values(['number' => $number])->execute();
}
}
unlink($oldFilePath);
}
}
}

View file

@ -16,6 +16,7 @@ return [
'upgrades' => DI\factory(function (DI\container $container) { 'upgrades' => DI\factory(function (DI\container $container) {
return [ return [
$container->get(\Szurubooru\Upgrades\Upgrade00::class),
$container->get(\Szurubooru\Upgrades\Upgrade01::class), $container->get(\Szurubooru\Upgrades\Upgrade01::class),
$container->get(\Szurubooru\Upgrades\Upgrade02::class), $container->get(\Szurubooru\Upgrades\Upgrade02::class),
$container->get(\Szurubooru\Upgrades\Upgrade03::class), $container->get(\Szurubooru\Upgrades\Upgrade03::class),