Skip to content

Commit

Permalink
add checkChecksum and checkMissingPreviousExecuted to group
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejkrajcik-blogic committed Apr 25, 2024
1 parent c1a201d commit bf91999
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
27 changes: 26 additions & 1 deletion src/Configurations/DefaultConfiguration.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,32 @@ class DefaultConfiguration implements IConfiguration
/** @var ?IDiffGenerator */
protected $dummyDataDiffGenerator;

/** @var bool */
protected $checkChecksum;

/** @var bool */
protected $checkMissingPreviousExecuted;

/**
* @param array<string, mixed> $phpParams
*/
public function __construct(string $dir, IDriver $driver, bool $withDummyData = true, array $phpParams = [])
public function __construct(
string $dir,
IDriver $driver,
bool $withDummyData = true,
array $phpParams = [],
bool $checkChecksum,
bool $checkDependMigration,
bool $checkMissingPreviousExecuted,
)
{
$this->dir = $dir;
$this->driver = $driver;
$this->withDummyData = $withDummyData;
$this->phpParams = $phpParams;
$this->checkChecksum = $checkChecksum;
$this->checkDependMigration = $checkDependMigration;
$this->checkMissingPreviousExecuted = $checkMissingPreviousExecuted;
}


Expand All @@ -66,19 +82,28 @@ public function getGroups(): array
if ($this->groups === null) {
$structures = new Group();
$structures->enabled = true;
$structures->checkChecksum = $this->checkChecksum;
$structures->checkDependMigration = $this->checkDependMigration;
$structures->checkMissingPreviousExecuted = $this->checkMissingPreviousExecuted;
$structures->name = 'structures';
$structures->directory = $this->dir . '/structures';
$structures->dependencies = [];
$structures->generator = $this->structureDiffGenerator;

$basicData = new Group();
$basicData->enabled = true;
$basicData->checkChecksum = $this->checkChecksum;
$basicData->checkDependMigration = $this->checkDependMigration;
$basicData->checkMissingPreviousExecuted = $this->checkMissingPreviousExecuted;
$basicData->name = 'basic-data';
$basicData->directory = $this->dir . '/basic-data';
$basicData->dependencies = ['structures'];

$dummyData = new Group();
$dummyData->enabled = $this->withDummyData;
$dummyData->checkChecksum = $this->checkChecksum;
$basicData->checkDependMigration = $this->checkDependMigration;
$dummyData->checkMissingPreviousExecuted = $this->checkMissingPreviousExecuted;
$dummyData->name = 'dummy-data';
$dummyData->directory = $this->dir . '/dummy-data';
$dummyData->dependencies = ['structures', 'basic-data'];
Expand Down
18 changes: 14 additions & 4 deletions src/Controllers/BaseController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,26 @@ public function __construct(IDriver $driver)
abstract public function run(): void;


/**
* @param list<string> $dependencies
*/
public function addGroup(string $name, string $dir, array $dependencies = []): self
/**
* @param list<string> $dependencies
*/
public function addGroup(
string $name,
string $dir,
array $dependencies = [],
bool $checkChecksum = true,
bool $checkMissingPreviousExecuted = true,
bool $checkDependMigration = true,
): self
{
$group = new Group;
$group->name = $name;
$group->directory = $dir;
$group->dependencies = $dependencies;
$group->enabled = false;
$group->checkChecksum = $checkChecksum;
$group->checkMissingPreviousExecuted = $checkMissingPreviousExecuted;
$group->checkDependMigration = $checkDependMigration;

$this->groups[$name] = $group;
return $this;
Expand Down
9 changes: 6 additions & 3 deletions src/Engine/OrderResolver.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class OrderResolver
{

/**
* @param list<Migration> $migrations
* @param list<Group> $groups
Expand Down Expand Up @@ -58,15 +59,15 @@ public function resolve(array $migrations, array $groups, array $files, string $

if (isset($files[$groupName][$filename])) {
$file = $files[$groupName][$filename];
if ($migration->checksum !== $file->checksum) {
if ($group->checkChecksum && $migration->checksum !== $file->checksum) {
throw new LogicException(sprintf(
'Previously executed migration "%s/%s" has been changed. File checksum is "%s", but executed migration had checksum "%s".',
$groupName, $filename, $file->checksum, $migration->checksum
));
}
unset($files[$groupName][$filename]);

} elseif ($group->enabled) {
} elseif ($group->checkMissingPreviousExecuted && $group->enabled) {
throw new LogicException(sprintf(
'Previously executed migration "%s/%s" is missing.',
$groupName, $filename
Expand All @@ -92,7 +93,9 @@ public function resolve(array $migrations, array $groups, array $files, string $
continue;
}

if ($this->isGroupDependentOn($groups, $file->group, $group) || $this->isGroupDependentOn($groups, $group, $file->group)) {
if ($group->checkDependMigration && $this->isGroupDependentOn($groups, $file->group, $group)
|| $this->isGroupDependentOn($groups, $group, $file->group)
) {
throw new LogicException(sprintf(
'New migration "%s/%s" must follow after the latest executed migration "%s/%s".',
$file->group->name, $file->name, $group->name, $lastMigrations[$group->name]
Expand Down
9 changes: 9 additions & 0 deletions src/Entities/Group.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class Group
/** @var bool */
public $enabled;

/** @var bool */
public $checkChecksum = true;

/** @var bool */
public $checkDependMigration = true;

/** @var bool */
public $checkMissingPreviousExecuted = true;

/** @var string absolute path do directory */
public $directory;

Expand Down

0 comments on commit bf91999

Please sign in to comment.