diff --git a/CHANGELOG.md b/CHANGELOG.md index d9fc46b36104d0dcf2933ebd7bd7141ee3eac0f7..72fc82e925bb46ff821b4e3750c679040b7789c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +1.4.2 (28/05/2025) +------------------ + +- Ajout d'une commande CLI pour lancer des tests de migration (... bddadmin:test-migration <Nom test> <utile|before|after>) + + 1.4.1 (23/05/2025) ------------------ diff --git a/config/module.config.php b/config/module.config.php index 03c1b502a6812bf0267b28129b7bba36d60aec35..7a2df605cc907827f3b9e9e1eab7c71ffe4f5b98 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -56,7 +56,7 @@ return [ 'factories' => [ Bdd::class => BddFactory::class, Command\InstallCommand::class => Command\CommandFactory::class, - Command\InstallDemoCommand::class => Command\CommandFactory::class, + Command\InstallDemoCommand::class => Command\CommandFactory::class, Command\UpdateCommand::class => Command\CommandFactory::class, Command\UpdateDdlCommand::class => Command\CommandFactory::class, Command\UpdateDataCommand::class => Command\CommandFactory::class, @@ -66,6 +66,7 @@ return [ Command\CopyFromCommand::class => Command\CommandFactory::class, Command\LoadCommand::class => Command\CommandFactory::class, Command\SaveCommand::class => Command\CommandFactory::class, + Command\TestMigrationCommand::class => Command\CommandFactory::class, ], ], @@ -73,7 +74,7 @@ return [ 'laminas-cli' => [ 'commands' => [ 'bddadmin:install' => Command\InstallCommand::class, - 'bddadmin:install-demo' => Command\InstallDemoCommand::class, + 'bddadmin:install-demo' => Command\InstallDemoCommand::class, 'bddadmin:update' => Command\UpdateCommand::class, 'bddadmin:clear' => Command\ClearCommand::class, 'bddadmin:update-ddl' => Command\UpdateDdlCommand::class, @@ -83,6 +84,7 @@ return [ 'bddadmin:copy-from' => Command\CopyFromCommand::class, 'bddadmin:load' => Command\LoadCommand::class, 'bddadmin:save' => Command\SaveCommand::class, + 'bddadmin:test-migration' => Command\TestMigrationCommand::class, ], ], ]; \ No newline at end of file diff --git a/src/Command/TestMigrationCommand.php b/src/Command/TestMigrationCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..c2bc79757b5834eab61590e4d5f4b43f42c1b2a2 --- /dev/null +++ b/src/Command/TestMigrationCommand.php @@ -0,0 +1,46 @@ +<?php + +namespace Unicaen\BddAdmin\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; +use Unicaen\BddAdmin\BddAwareTrait; +use Unicaen\BddAdmin\Data\DataManager; +use Unicaen\BddAdmin\Ddl\Ddl; + +/** + * Description of TestMigrationCommand + * + * @author Laurent Lécluse <laurent.lecluse at unicaen.fr> + */ +class TestMigrationCommand extends Command +{ + use BddAwareTrait; + + protected function configure(): void + { + $this->setDescription('Test de script de migration args: <Nom partiel du script> <utile|before|after>') + ->addArgument('class', InputArgument::REQUIRED, 'Nom du script de migration (classe)') + ->addArgument('context', InputArgument::REQUIRED, 'Contexte de migration (utile, before ou after)'); + } + + + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $bdd = $this->getBdd()->setLogger($io); + + $io->title('Test de script de migration'); + + $class = $input->getArgument('class'); + $context = $input->getArgument('context'); + + $bdd->migration()->test($class, $context); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Migration/MigrationManager.php b/src/Migration/MigrationManager.php index 7e76ba6bd099353eb45691c074248fa63824318a..088c4219ec24cf06ec5ba30260f93ccd25656396 100644 --- a/src/Migration/MigrationManager.php +++ b/src/Migration/MigrationManager.php @@ -208,4 +208,48 @@ class MigrationManager } } } + + + + public function test(string $class, string $context): void + { + if ($context != self::ACTION_BEFORE && $context != self::ACTION_AFTER && $context != 'utile') { + throw new \Exception('Le contexte de migration ' . $context . ' est non conforme, seuls after, before et utile sont possibles'); + } + + $scripts = $this->getScripts(); + $script = null; + foreach ($scripts as $object) { + if (str_contains($object::class, $class)) { + if ($script !== null) { + throw new \Exception("Plusieurs scripts de migration correspondent à \"$class\", merci de préciser"); + } + $script = $object; + } + } + + if (!$script){ + throw new \Exception('Aucun script de migration ne correspond à "'.$class.'"'); + } + + $object->logBegin($object::class.' -> '.$context); + + if ('utile' === $context) { + if ($script->utile()){ + $object->logMsg("Utile renvoie OUI"); + }else{ + $object->logMsg("Utile renvoie NON"); + } + }else{ + try { + $script->$context(); + }catch(\Throwable $e){ + $object->logError($e); + } + } + + $object->logEnd('Fin du test'); + + $script->$context(); + } } \ No newline at end of file