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

simple plan impl #69

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions src/Bridges/SymfonyConsole/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/Configurations/DefaultConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ public function setDummyDataDiffGenerator(IDiffGenerator $generator = NULL)
$this->dummyDataDiffGenerator = $generator;
}


/**
* @return string
*/
public function getPlanFile()
{
return $this->dir . '/plan.tsv';
}

}
65 changes: 65 additions & 0 deletions src/Engine/Plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

namespace Nextras\Migrations\Engine;

use Nextras\Migrations\Entities\Group;
use Nextras\Migrations\PlanException;


class Plan
{

/** @var NULL|string */
private $planFile;


/**
* @param NULL|string $planFile
*/
public function __construct($planFile)
{
$this->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++;
}
}

}
3 changes: 3 additions & 0 deletions src/Engine/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions src/IConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public function getGroups();
*/
public function getExtensionHandlers();


/**
* @return NULL|string
*/
public function getPlanFile();

}
8 changes: 8 additions & 0 deletions src/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class LogicException extends \LogicException implements Exception
}


/**
* Execution plan violation.
*/
class PlanException extends LogicException
{
}


/**
* Error during runtime.
*/
Expand Down