From 3240c8ae5b0f7bb86329582388bec2e05fe0a9e3 Mon Sep 17 00:00:00 2001 From: nitinsingh_oxidesales Date: Mon, 17 Feb 2020 16:52:30 +0100 Subject: [PATCH 1/6] Implemented Module Migration Feature --- README.md | 13 ++++++++ src/Command/MigrateCommand.php | 14 +++++++- src/Core/Migration/MigrationHandler.php | 44 +++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6e42f49..30fb90c 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,19 @@ class AddDemoCulumnToOxUserMigration extends oxMigrationQuery *Note: It is better to use generator for migration queries creation* +## Run Module Migration +*Note: Currently this if experimental feature* + +We have now experimental feature to run migration files from module itself. Now we can put module related migration file inside 'migration' directory of module. + +Module + -- migration + -- XXX.PHP + +You can pass '--skip-module-migration' option to skip to run module migration files. +``` + migration:run --skip-module-migration +``` # Related Projects * https://github.com/OXIDprojects/oxid-module-internals * https://github.com/OXIDprojects/oxid_modules_config diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index f82b3e5..7589541 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\NullOutput; use OxidEsales\Eshop\Core\Registry; use OxidProfessionalServices\OxidConsole\Core\Exception\ConsoleException; @@ -37,7 +38,12 @@ public function configure() ->setName('migration:run') ->setAliases(['migrate']) ->setDescription('Run database migration scripts') - ->addArgument('timestamp', InputArgument::OPTIONAL, "Migration to use for execution"); + ->addArgument('timestamp', InputArgument::OPTIONAL, "Migration to use for execution") + ->addOption('skip-module-migration', null, InputOption::VALUE_NONE, "Skip migration files from modules") + ->setHelp(<<<'EOF' +Command %command.name% will skip migration files from modules. +EOF + ); } /** @@ -52,6 +58,7 @@ public function execute(InputInterface $input, OutputInterface $output) exit(1); } + $output->writeln('NOTE: Running module migration feature is currently experimental feature and you can skip this operation by passing \'--skip-module-migration\' option.'); $output->writeln('Running migration scripts'); $debugOutput = $input->getOption('verbose') @@ -59,6 +66,11 @@ public function execute(InputInterface $input, OutputInterface $output) : new NullOutput(); /** @var MigrationHandler $oMigrationHandler */ + if ($input->getOption('skip-module-migration')) { + $output->writeln('Skipping Module Migration...'); + MigrationHandler::$skipModuleMigration = true; + } + $oMigrationHandler = Registry::get(MigrationHandler::class); $oMigrationHandler->run($timestamp, $debugOutput); diff --git a/src/Core/Migration/MigrationHandler.php b/src/Core/Migration/MigrationHandler.php index feae701..914febc 100644 --- a/src/Core/Migration/MigrationHandler.php +++ b/src/Core/Migration/MigrationHandler.php @@ -15,9 +15,12 @@ use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use RegexIterator; +use AppendIterator; use Symfony\Component\Console\Output\OutputInterface; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidProfessionalServices\OxidConsole\Core\Exception\MigrationException; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Module\ModuleList; /** * Migration handler for migration queries @@ -55,6 +58,11 @@ class MigrationHandler */ protected $queries = array(); + /** + * @var bool skip module migration + */ + public static $skipModuleMigration = false; + /** * Constructor. * @@ -229,9 +237,41 @@ protected function buildMigrationQueries() } $oDirectory = new RecursiveDirectoryIterator($this->migrationQueriesDir); - $oFlattened = new RecursiveIteratorIterator($oDirectory); + $oIterators = new AppendIterator(); + $oIterators->append(new RecursiveIteratorIterator($oDirectory)); + + // Include module migration files + if (!self::$skipModuleMigration) { + $oConfig = Registry::getConfig(); + + if (!class_exists(ModuleList::class)) { + print "ERROR: Oxid ModuleList class can not be loaded, + please try to run vendor/bin/oe-eshop-unified_namespace_generator"; + } else { + try { + // TODO: We need to use shop internal service(Shop 6.2 ref -> /Internal/Framework/Module/Configuration/Dao/ShopConfigurationDaoInterface.php) to get active modules path + $moduleList = oxNew(ModuleList::class); + $modulesDir = $oConfig->getModulesDir(); + $activeModules = $moduleList->getActiveModuleInfo(); + + if (is_array($activeModules) and count($activeModules) > 0) { + foreach ($activeModules as $activeModule) { + $migrationQueryDir = $modulesDir . $activeModule . DIRECTORY_SEPARATOR . 'migration' . DIRECTORY_SEPARATOR; + if (!is_dir($migrationQueryDir)) { + continue; + } + $oDirectory = new RecursiveDirectoryIterator($migrationQueryDir); + $oIterators->append(new RecursiveIteratorIterator($oDirectory)); + } + } + } catch (Throwable $exception) { + print "Shop is not able to list modules\n"; + print $exception->getMessage(); + } + } + } - $aFiles = new RegexIterator($oFlattened, AbstractQuery::REGEXP_FILE); + $aFiles = new RegexIterator($oIterators, AbstractQuery::REGEXP_FILE); foreach ($aFiles as $sFilePath) { include_once $sFilePath; From c5908dd9bd14f612ee45f8b25fa51c61878b9ee0 Mon Sep 17 00:00:00 2001 From: nitinsingh_oxidesales Date: Mon, 17 Feb 2020 17:01:52 +0100 Subject: [PATCH 2/6] Implemented Module Migration Feature --- src/Command/MigrateCommand.php | 3 ++- src/Core/Migration/MigrationHandler.php | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index 7589541..6ecd81c 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -58,7 +58,8 @@ public function execute(InputInterface $input, OutputInterface $output) exit(1); } - $output->writeln('NOTE: Running module migration feature is currently experimental feature and you can skip this operation by passing \'--skip-module-migration\' option.'); + $output->writeln('NOTE: Running module migration feature is currently experimental feature. + You can skip this operation by passing \'--skip-module-migration\' option.'); $output->writeln('Running migration scripts'); $debugOutput = $input->getOption('verbose') diff --git a/src/Core/Migration/MigrationHandler.php b/src/Core/Migration/MigrationHandler.php index 914febc..b7e1aa2 100644 --- a/src/Core/Migration/MigrationHandler.php +++ b/src/Core/Migration/MigrationHandler.php @@ -249,14 +249,15 @@ protected function buildMigrationQueries() please try to run vendor/bin/oe-eshop-unified_namespace_generator"; } else { try { - // TODO: We need to use shop internal service(Shop 6.2 ref -> /Internal/Framework/Module/Configuration/Dao/ShopConfigurationDaoInterface.php) to get active modules path + // TODO: We need to use shop internal service to get active modules path $moduleList = oxNew(ModuleList::class); $modulesDir = $oConfig->getModulesDir(); $activeModules = $moduleList->getActiveModuleInfo(); if (is_array($activeModules) and count($activeModules) > 0) { foreach ($activeModules as $activeModule) { - $migrationQueryDir = $modulesDir . $activeModule . DIRECTORY_SEPARATOR . 'migration' . DIRECTORY_SEPARATOR; + $migrationQueryDir = $modulesDir . $activeModule . DIRECTORY_SEPARATOR . + 'migration' . DIRECTORY_SEPARATOR; if (!is_dir($migrationQueryDir)) { continue; } From b85ebebbf4d0e17bfc077a72d5ebc4adf1caea65 Mon Sep 17 00:00:00 2001 From: nitinsingh_oxidesales Date: Tue, 18 Feb 2020 11:44:25 +0100 Subject: [PATCH 3/6] mplemented Module Migration Feature and updated review points --- README.md | 4 ++-- src/Command/MigrateCommand.php | 4 ++-- src/Core/Migration/MigrationHandler.php | 11 +++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 30fb90c..eae89f0 100644 --- a/README.md +++ b/README.md @@ -212,9 +212,9 @@ class AddDemoCulumnToOxUserMigration extends oxMigrationQuery ## Run Module Migration -*Note: Currently this if experimental feature* +*Note: Currently this is experimental feature* -We have now experimental feature to run migration files from module itself. Now we can put module related migration file inside 'migration' directory of module. +We have now an experimental feature to run migration files from modules itself. Now we can put module-related migration files inside the 'migration' directory of the module. Module -- migration diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index 6ecd81c..cd65baf 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -58,8 +58,8 @@ public function execute(InputInterface $input, OutputInterface $output) exit(1); } - $output->writeln('NOTE: Running module migration feature is currently experimental feature. - You can skip this operation by passing \'--skip-module-migration\' option.'); + $output->writeln("NOTE: Running module migrations is currently an experimental feature."); + $output->writeln(" You can skip this by using '--skip-module-migration'."); $output->writeln('Running migration scripts'); $debugOutput = $input->getOption('verbose') diff --git a/src/Core/Migration/MigrationHandler.php b/src/Core/Migration/MigrationHandler.php index b7e1aa2..55c16ea 100644 --- a/src/Core/Migration/MigrationHandler.php +++ b/src/Core/Migration/MigrationHandler.php @@ -245,11 +245,11 @@ protected function buildMigrationQueries() $oConfig = Registry::getConfig(); if (!class_exists(ModuleList::class)) { - print "ERROR: Oxid ModuleList class can not be loaded, - please try to run vendor/bin/oe-eshop-unified_namespace_generator"; + throw new MigrationException('ERROR: Oxid ModuleList class can not be loaded, + please try to run vendor/bin/oe-eshop-unified_namespace_generator'); } else { try { - // TODO: We need to use shop internal service to get active modules path + // TODO: We need to use shop internal services to get active modules path $moduleList = oxNew(ModuleList::class); $modulesDir = $oConfig->getModulesDir(); $activeModules = $moduleList->getActiveModuleInfo(); @@ -265,9 +265,8 @@ protected function buildMigrationQueries() $oIterators->append(new RecursiveIteratorIterator($oDirectory)); } } - } catch (Throwable $exception) { - print "Shop is not able to list modules\n"; - print $exception->getMessage(); + } catch (MigrationException $exception) { + throw $exception; } } } From 9b7ac6dceb38dd1e7938d7ad46f029960bff99b9 Mon Sep 17 00:00:00 2001 From: nitinsingh_oxidesales Date: Tue, 18 Feb 2020 12:13:49 +0100 Subject: [PATCH 4/6] mplemented Module Migration Feature and updated review points --- README.md | 2 +- src/Core/Migration/MigrationHandler.php | 34 +++++++++++-------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index eae89f0..bef1b86 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ class AddDemoCulumnToOxUserMigration extends oxMigrationQuery ## Run Module Migration -*Note: Currently this is experimental feature* +*Note: Currently this is an experimental feature* We have now an experimental feature to run migration files from modules itself. Now we can put module-related migration files inside the 'migration' directory of the module. diff --git a/src/Core/Migration/MigrationHandler.php b/src/Core/Migration/MigrationHandler.php index 55c16ea..fd9b3d3 100644 --- a/src/Core/Migration/MigrationHandler.php +++ b/src/Core/Migration/MigrationHandler.php @@ -245,28 +245,24 @@ protected function buildMigrationQueries() $oConfig = Registry::getConfig(); if (!class_exists(ModuleList::class)) { - throw new MigrationException('ERROR: Oxid ModuleList class can not be loaded, - please try to run vendor/bin/oe-eshop-unified_namespace_generator'); + throw new MigrationException('ERROR: Oxid ModuleList class can not be loaded, ' + . 'please try to run vendor/bin/oe-eshop-unified_namespace_generator'); } else { - try { - // TODO: We need to use shop internal services to get active modules path - $moduleList = oxNew(ModuleList::class); - $modulesDir = $oConfig->getModulesDir(); - $activeModules = $moduleList->getActiveModuleInfo(); - - if (is_array($activeModules) and count($activeModules) > 0) { - foreach ($activeModules as $activeModule) { - $migrationQueryDir = $modulesDir . $activeModule . DIRECTORY_SEPARATOR . - 'migration' . DIRECTORY_SEPARATOR; - if (!is_dir($migrationQueryDir)) { - continue; - } - $oDirectory = new RecursiveDirectoryIterator($migrationQueryDir); - $oIterators->append(new RecursiveIteratorIterator($oDirectory)); + // TODO: We need to use shop internal services to get active modules path + $moduleList = oxNew(ModuleList::class); + $modulesDir = $oConfig->getModulesDir(); + $activeModules = $moduleList->getActiveModuleInfo(); + + if (is_array($activeModules) and count($activeModules) > 0) { + foreach ($activeModules as $activeModule) { + $migrationQueryDir = $modulesDir . $activeModule . DIRECTORY_SEPARATOR . + 'migration' . DIRECTORY_SEPARATOR; + if (!is_dir($migrationQueryDir)) { + continue; } + $oDirectory = new RecursiveDirectoryIterator($migrationQueryDir); + $oIterators->append(new RecursiveIteratorIterator($oDirectory)); } - } catch (MigrationException $exception) { - throw $exception; } } } From 4d279615251951f00282e0ee3c6f2a526fc56104 Mon Sep 17 00:00:00 2001 From: nitinsingh_oxidesales Date: Tue, 18 Feb 2020 12:45:06 +0100 Subject: [PATCH 5/6] mplemented Module Migration Feature and updated review points --- src/Command/MigrateCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index cd65baf..55ba69d 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -58,8 +58,6 @@ public function execute(InputInterface $input, OutputInterface $output) exit(1); } - $output->writeln("NOTE: Running module migrations is currently an experimental feature."); - $output->writeln(" You can skip this by using '--skip-module-migration'."); $output->writeln('Running migration scripts'); $debugOutput = $input->getOption('verbose') @@ -72,6 +70,9 @@ public function execute(InputInterface $input, OutputInterface $output) MigrationHandler::$skipModuleMigration = true; } + $output->writeln("NOTE: Running module migrations is currently an experimental feature."); + $output->writeln(" You can skip this by using '--skip-module-migration'."); + $oMigrationHandler = Registry::get(MigrationHandler::class); $oMigrationHandler->run($timestamp, $debugOutput); From 057fa2ce89b16222ebadcaba90645fa3394fb9fc Mon Sep 17 00:00:00 2001 From: nitinsingh_oxidesales Date: Tue, 18 Feb 2020 13:10:33 +0100 Subject: [PATCH 6/6] mplemented Module Migration Feature and updated review points --- src/Command/MigrateCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index 55ba69d..5178f48 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -58,21 +58,20 @@ public function execute(InputInterface $input, OutputInterface $output) exit(1); } + $output->writeln("NOTE: Running module migrations is currently an experimental feature."); + $output->writeln(" You can skip this by using '--skip-module-migration'."); $output->writeln('Running migration scripts'); $debugOutput = $input->getOption('verbose') ? $output : new NullOutput(); - /** @var MigrationHandler $oMigrationHandler */ if ($input->getOption('skip-module-migration')) { $output->writeln('Skipping Module Migration...'); MigrationHandler::$skipModuleMigration = true; } - $output->writeln("NOTE: Running module migrations is currently an experimental feature."); - $output->writeln(" You can skip this by using '--skip-module-migration'."); - + /** @var MigrationHandler $oMigrationHandler */ $oMigrationHandler = Registry::get(MigrationHandler::class); $oMigrationHandler->run($timestamp, $debugOutput);