From aa767125e4f05903f73a89f7f4e4ea062b48ce8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikul=C3=A1=C5=A1=20D=C3=ADt=C4=9B?= Date: Sat, 22 Apr 2017 13:58:33 +0200 Subject: [PATCH] simple plan impl --- src/Bridges/SymfonyConsole/CreateCommand.php | 5 ++ src/Configurations/DefaultConfiguration.php | 9 +++ src/Engine/Plan.php | 65 ++++++++++++++++++++ src/Engine/Runner.php | 3 + src/IConfiguration.php | 6 ++ src/exceptions.php | 8 +++ 6 files changed, 96 insertions(+) create mode 100644 src/Engine/Plan.php diff --git a/src/Bridges/SymfonyConsole/CreateCommand.php b/src/Bridges/SymfonyConsole/CreateCommand.php index 85e5069..bdfd146 100644 --- a/src/Bridges/SymfonyConsole/CreateCommand.php +++ b/src/Bridges/SymfonyConsole/CreateCommand.php @@ -11,6 +11,7 @@ use Nette\Utils\Strings; use Nextras; +use Nextras\Migrations\Engine\Plan; use Nextras\Migrations\Entities\Group; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -68,6 +69,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $path = $this->getPath($group, $input->getArgument('label')); $content = $this->getFileContent($group, $this->getFileContentSource($input)); + $planFile = $this->config->getPlanFile(); + $plan = new Plan($planFile); + $plan->append($group, basename($path)); + $this->createFile($path, $content, $output); $output->writeln($path); diff --git a/src/Configurations/DefaultConfiguration.php b/src/Configurations/DefaultConfiguration.php index d8060fe..9dd5191 100644 --- a/src/Configurations/DefaultConfiguration.php +++ b/src/Configurations/DefaultConfiguration.php @@ -131,4 +131,13 @@ public function setDummyDataDiffGenerator(IDiffGenerator $generator = NULL) $this->dummyDataDiffGenerator = $generator; } + + /** + * @return string + */ + public function getPlanFile() + { + return $this->dir . '/plan.tsv'; + } + } diff --git a/src/Engine/Plan.php b/src/Engine/Plan.php new file mode 100644 index 0000000..066f4b1 --- /dev/null +++ b/src/Engine/Plan.php @@ -0,0 +1,65 @@ +planFile = $planFile; + } + + + private function getEntries(): array + { + if ($this->planFile === NULL || !file_exists($this->planFile)) { + return []; + } + + $raw = file_get_contents($this->planFile); + $lines = preg_split("~\r?\n\r?~", $raw, -1, PREG_SPLIT_NO_EMPTY); + return array_map(function(string $line) { + return explode("\t", $line); + }, $lines); + } + + + public function append(Group $group, string $name): void + { + $entries = $this->getEntries(); + $lastEntry = array_shift($entries); + + $ord = 0; + if ($lastEntry !== NULL) { + $ord = 1 + (int) $lastEntry[0]; + } + + file_put_contents($this->planFile, "$ord\t{$group->name}\t$name\n", FILE_APPEND); + } + + + public function validate(): void + { + $expectedOrd = 0; + foreach ($this->getEntries() as $entry) { + if ($expectedOrd !== (int) $entry[0]) { + $fmtEntry = $entry[1] . ': ' . $entry[2]; + throw new PlanException("Execution plan violation: expected ord $expectedOrd, got $entry[0] ($fmtEntry)."); + } + $expectedOrd++; + } + } + +} diff --git a/src/Engine/Runner.php b/src/Engine/Runner.php index 7e65a69..480f5cc 100644 --- a/src/Engine/Runner.php +++ b/src/Engine/Runner.php @@ -97,6 +97,9 @@ public function run($mode = self::MODE_CONTINUE, IConfiguration $config = NULL) } } + $plan = new Plan($config->getPlanFile()); + $plan->validate(); + if ($mode === self::MODE_INIT) { $this->printer->printSource($this->driver->getInitTableSource() . "\n"); $files = $this->finder->find($this->groups, array_keys($this->extensionsHandlers)); diff --git a/src/IConfiguration.php b/src/IConfiguration.php index cd7fac3..cae82d2 100644 --- a/src/IConfiguration.php +++ b/src/IConfiguration.php @@ -28,4 +28,10 @@ public function getGroups(); */ public function getExtensionHandlers(); + + /** + * @return NULL|string + */ + public function getPlanFile(); + } diff --git a/src/exceptions.php b/src/exceptions.php index 051aacd..44d0dd8 100644 --- a/src/exceptions.php +++ b/src/exceptions.php @@ -26,6 +26,14 @@ class LogicException extends \LogicException implements Exception } +/** + * Execution plan violation. + */ +class PlanException extends LogicException +{ +} + + /** * Error during runtime. */