From 681e3c87514dcc3a8394248cc7cc9d2ae5a38333 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 2 Nov 2023 20:51:56 +0200 Subject: [PATCH] Add migration strategies to the configuration (#81) --- src/Bootloader/MigrationsBootloader.php | 25 +++++++++++++++++++ .../Bootloader/MigrationsBootloaderTest.php | 4 +++ 2 files changed, 29 insertions(+) diff --git a/src/Bootloader/MigrationsBootloader.php b/src/Bootloader/MigrationsBootloader.php index c5e021a..ac4059f 100644 --- a/src/Bootloader/MigrationsBootloader.php +++ b/src/Bootloader/MigrationsBootloader.php @@ -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; @@ -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( @@ -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); + } } diff --git a/tests/src/Bootloader/MigrationsBootloaderTest.php b/tests/src/Bootloader/MigrationsBootloaderTest.php index b7aa3c2..a3bbc1e 100644 --- a/tests/src/Bootloader/MigrationsBootloaderTest.php +++ b/tests/src/Bootloader/MigrationsBootloaderTest.php @@ -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 @@ -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);