From ad620de09accd6883b1a5d179907f37326e87894 Mon Sep 17 00:00:00 2001 From: Paul Clegg Date: Tue, 27 Jun 2017 13:47:55 +0100 Subject: [PATCH] Migrations now run in separate processes --- .../Console/Command/RunCommand.php | 84 +++++++++++++++++++ lib/PimcoreMigrations/Migration/Manager.php | 60 ++++++------- .../Model/AbstractMigration.php | 6 +- lib/PimcoreMigrations/Plugin.php | 4 +- 4 files changed, 118 insertions(+), 36 deletions(-) create mode 100644 lib/PimcoreMigrations/Console/Command/RunCommand.php diff --git a/lib/PimcoreMigrations/Console/Command/RunCommand.php b/lib/PimcoreMigrations/Console/Command/RunCommand.php new file mode 100644 index 0000000..c482de3 --- /dev/null +++ b/lib/PimcoreMigrations/Console/Command/RunCommand.php @@ -0,0 +1,84 @@ +setName('deployment:migrations:run') + ->addArgument('file', InputArgument::REQUIRED, 'The migration filename (no path)') + ->addArgument('mode', InputArgument::REQUIRED, 'The migration direction mode [up|down]') + ->setDescription('Run a single migration file (Migration Manager runs this internally)'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $factory = Factory::getInstance(); + $migrationFullPath = $factory->getMigrationsPath() . DIRECTORY_SEPARATOR . $input->getArgument('file'); + $mode = $input->getArgument('mode'); + + if ($mode !== MigrationInterface::UP && $mode !== MigrationInterface::DOWN) { + throw new \Exception('Invalid migration command'); + } + + $migration = Factory::getInstance()->createMigrationClassInstance($migrationFullPath); + $migration->init(); + + //up + if ($mode === MigrationInterface::UP) { + if (!$migration->hasBeenApplied()) { + if ($migration->getRebuildClassesBefore()) { + // rebuild the class definitions + Tool::rebuildClasses(); + } + $migration->run($mode); + } else { + $migration->setSkip(true); + } + } else { + // down + if ($migration->hasBeenApplied()) { + $migration->run($mode); + $migration->delete(); + } else { + $migration->setSkip(true); + } + } + + if ($migration->wasSuccessful()) { + $migration->save(); + return 0; + } else if ($migration->getSkip()) { + return 2; + } + + $this->output->writeln(sprintf('Migration "%s" failed (reason: "%s")', + $migration->getClassName(), + $migration->getError() + )); + + return 1; //error + } +} diff --git a/lib/PimcoreMigrations/Migration/Manager.php b/lib/PimcoreMigrations/Migration/Manager.php index 8d881ee..37edfd1 100644 --- a/lib/PimcoreMigrations/Migration/Manager.php +++ b/lib/PimcoreMigrations/Migration/Manager.php @@ -136,42 +136,23 @@ public function migrate() */ if($this->versionInRange($version)) { - if ($this->getMode() === MigrationInterface::UP) { - if (!$migration->hasBeenApplied()) { - $migration->run(MigrationInterface::UP); - $migration->save(); - } else { - $migration->setSkip(true); - } - } else { - // down - if ($migration->hasBeenApplied()) { - $migration->run(MigrationInterface::DOWN); - $migration->delete(); - } else { - $migration->setSkip(true); - } - } + $this->output->write('Starting Migration ' . $migration->getVersion()); + $result = $this->runInSeparateProcess($this->getConsoleCommandPath($migration->getFilename())); } else { $this->output->writeln(sprintf('Migration version "%d" not in range', $migration->getVersion())); continue; } - if ($migration->wasSuccessful()) { - $this->output->writeln(sprintf('Migration "%s" was successful version now "%d"', - $migration->getClassName(), - $migration->getVersion() - )); - } else if ($migration->getSkip()) { - $this->output->writeln(sprintf('Migration "%s" was skipped', $migration->getClassName())); + if ($result[0] == 0) { + $this->output->writeln(' - Success!'); + // success! + } else if ($result[0] == 2) { + $this->output->writeln(' - Skipped!'); + // skipped! } else { - - $this->output->writeln(sprintf('Migration "%s" failed (reason: "%s")', - $migration->getClassName(), - $migration->getError() - )); - - throw new \Exception('Error Migrating ' . $this->getMode() . '!'); + $this->output->writeln(' - Migration Failed :-('); + $this->output->writeln('' . implode('\n', $result[1]) . ''); + throw new \Exception('Migration Failure, exit code ' . $result); } } @@ -210,6 +191,7 @@ protected function loadMigrations() if (Tool::isValidMigrationFilename($basename)) { $migration = Factory::getInstance()->createMigrationClassInstance($filePath); + $migration->init(); $version = $this->determineMigrationVersion($migration); $migration->setVersion($version); @@ -242,6 +224,24 @@ protected function sortMigrations() } } + protected function getConsoleCommandPath($migrationFile) + { + $cmdPath = PIMCORE_PATH . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'console.php'; + $env = \Pimcore\Config::getEnvironment(); + if ($env) { + $cmdPath .= '--environment=' . $env; + } + + return $cmdPath . ' deployment:migrations:run "'.$migrationFile.'" ' . $this->getMode(); + } + + protected function runInSeparateProcess($command) + { + $ll = exec($command, $output, $code); + + return [$code,$output]; + } + } diff --git a/lib/PimcoreMigrations/Model/AbstractMigration.php b/lib/PimcoreMigrations/Model/AbstractMigration.php index 23a2413..ca5727a 100644 --- a/lib/PimcoreMigrations/Model/AbstractMigration.php +++ b/lib/PimcoreMigrations/Model/AbstractMigration.php @@ -155,7 +155,7 @@ public function setFilename($filename) } - protected function init() + public function init() { } @@ -252,10 +252,6 @@ public function run($action) try { - if ($this->getRebuildClassesBefore()) { - \PimcoreMigrations\Tool::rebuildClasses(); - } - $this->$action(); if ($this->getRebuildClassesAfter()) { diff --git a/lib/PimcoreMigrations/Plugin.php b/lib/PimcoreMigrations/Plugin.php index 887f445..e256042 100644 --- a/lib/PimcoreMigrations/Plugin.php +++ b/lib/PimcoreMigrations/Plugin.php @@ -15,6 +15,7 @@ use Pimcore\API\Plugin as PluginLib; use PimcoreMigrations\Console\Command\DownCommand; +use PimcoreMigrations\Console\Command\RunCommand; use PimcoreMigrations\Console\Command\StatusCommand; use PimcoreMigrations\Console\Command\UpCommand; @@ -76,7 +77,8 @@ public function getConsoleCommands() return [ new StatusCommand(), new UpCommand(), - new DownCommand() + new DownCommand(), + new RunCommand() ]; }