diff --git a/src/Services/UpgradeService.php b/src/Services/UpgradeService.php index 8f9f98c5..91f4586e 100644 --- a/src/Services/UpgradeService.php +++ b/src/Services/UpgradeService.php @@ -10,7 +10,7 @@ final class UpgradeService private $config; private $upgrades; private $databaseConnection; - private $executedUpgradeNames = []; + private $executedUpgradeNumbers = []; public function __construct( Config $config, @@ -20,7 +20,7 @@ final class UpgradeService $this->config = $config; $this->databaseConnection = $databaseConnection; $this->upgrades = $upgradeRepository->getUpgrades(); - $this->loadExecutedUpgradeNames(); + $this->loadExecutedUpgradeNumbers(); } public function runUpgradesVerbose() @@ -48,32 +48,37 @@ final class UpgradeService 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) { $upgrade->run($this->databaseConnection); - $this->executedUpgradeNames[] = get_class($upgrade); - $this->saveExecutedUpgradeNames(); + $number = $this->getUpgradeNumber($upgrade); + $this->executedUpgradeNumbers[] = $number; + $this->databaseConnection->getPDO()->insertInto('executedUpgrades')->values(['number' => $number])->execute(); } - private function loadExecutedUpgradeNames() + private function loadExecutedUpgradeNumbers() { - $infoFilePath = $this->getExecutedUpgradeNamesFilePath(); - if (!file_exists($infoFilePath)) - return; - $this->executedUpgradeNames = explode("\n", file_get_contents($infoFilePath)); + $this->executedUpgradeNumbers = []; + try + { + 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(); - file_put_contents($infoFilePath, implode("\n", $this->executedUpgradeNames)); - } - - private function getExecutedUpgradeNamesFilePath() - { - return $this->config->getDataDirectory() . DIRECTORY_SEPARATOR . 'executed_upgrades.txt'; + $className = get_class($upgrade); + preg_match('/(\d+)/', $className, $matches); + return intval($matches[1]); } } diff --git a/src/Upgrades/Upgrade00.php b/src/Upgrades/Upgrade00.php new file mode 100644 index 00000000..3b06be63 --- /dev/null +++ b/src/Upgrades/Upgrade00.php @@ -0,0 +1,26 @@ +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); + } + } +} diff --git a/src/di.php b/src/di.php index 80853591..7e1fa66a 100644 --- a/src/di.php +++ b/src/di.php @@ -16,6 +16,7 @@ return [ 'upgrades' => DI\factory(function (DI\container $container) { return [ + $container->get(\Szurubooru\Upgrades\Upgrade00::class), $container->get(\Szurubooru\Upgrades\Upgrade01::class), $container->get(\Szurubooru\Upgrades\Upgrade02::class), $container->get(\Szurubooru\Upgrades\Upgrade03::class),