Skip to content

Commit

Permalink
Add migration strategies to the configuration (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Nov 2, 2023
1 parent 454c779 commit 681e3c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Bootloader/MigrationsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Cycle\Migrations\FileRepository;
use Cycle\Migrations\Migrator;
use Cycle\Migrations\RepositoryInterface;
use Cycle\Schema\Generator\Migrations\NameBasedOnChangesGenerator;
use Cycle\Schema\Generator\Migrations\NameGeneratorInterface;
use Cycle\Schema\Generator\Migrations\Strategy\GeneratorStrategyInterface;
use Cycle\Schema\Generator\Migrations\Strategy\SingleFileStrategy;
use Psr\Container\ContainerInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\DirectoriesInterface;
use Spiral\Boot\EnvironmentInterface;
Expand All @@ -24,6 +29,8 @@ final class MigrationsBootloader extends Bootloader
protected const SINGLETONS = [
Migrator::class => Migrator::class,
RepositoryInterface::class => FileRepository::class,
NameGeneratorInterface::class => [self::class, 'initNameGenerator'],
GeneratorStrategyInterface::class => [self::class, 'initGeneratorStrategy'],
];

public function init(
Expand All @@ -39,9 +46,27 @@ public function init(
MigrationConfig::CONFIG,
[
'directory' => $dirs->get('migrations'),
'strategy' => SingleFileStrategy::class,
'nameGenerator' => NameBasedOnChangesGenerator::class,
'table' => 'migrations',
'safe' => $env->get('SAFE_MIGRATIONS', false),
]
);
}

private function initGeneratorStrategy(
MigrationConfig $config,
ContainerInterface $container
): GeneratorStrategyInterface {
$strategy = $config->toArray()['strategy'] ?? SingleFileStrategy::class;

return $container->get($strategy);
}

private function initNameGenerator(MigrationConfig $config, ContainerInterface $container): NameGeneratorInterface
{
$nameGenerator = $config->toArray()['nameGenerator'] ?? NameBasedOnChangesGenerator::class;

return $container->get($nameGenerator);
}
}
4 changes: 4 additions & 0 deletions tests/src/Bootloader/MigrationsBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Cycle\Migrations\FileRepository;
use Cycle\Migrations\Migrator;
use Cycle\Migrations\RepositoryInterface;
use Cycle\Schema\Generator\Migrations\NameBasedOnChangesGenerator;
use Cycle\Schema\Generator\Migrations\Strategy\SingleFileStrategy;
use Spiral\Tests\BaseTest;

final class MigrationsBootloaderTest extends BaseTest
Expand All @@ -28,6 +30,8 @@ public function testGetsDefaultConfig(): void
$this->assertDirectoryAliasDefined('migrations');
$this->assertSame([
'directory' => $this->getDirectoryByAlias('migrations'),
'strategy' => SingleFileStrategy::class,
'nameGenerator' => NameBasedOnChangesGenerator::class,
'table' => 'migrations',
'safe' => false,
], $config);
Expand Down

0 comments on commit 681e3c8

Please sign in to comment.