Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Commit

Permalink
Migrations now run in separate processes
Browse files Browse the repository at this point in the history
  • Loading branch information
cleggypdc committed Jun 27, 2017
1 parent ebcc1e3 commit ad620de
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 36 deletions.
84 changes: 84 additions & 0 deletions lib/PimcoreMigrations/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* RunCommand
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md
* file distributed with this source code.
*
* @copyright Copyright (c) 2014-2016 Gather Digital Ltd (https://www.gatherdigital.co.uk)
* @license https://www.gatherdigital.co.uk/license GNU General Public License version 3 (GPLv3)
*/

namespace PimcoreMigrations\Console\Command;

use PimcoreMigrations\Factory;
use PimcoreMigrations\Migration\MigrationInterface;
use Pimcore\Console\AbstractCommand;
use PimcoreMigrations\Model\AbstractMigration;
use PimcoreMigrations\Tool;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RunCommand extends AbstractCommand
{
protected function configure()
{
$this
->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
}
}
60 changes: 30 additions & 30 deletions lib/PimcoreMigrations/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(' - <info>Success!</info>');
// success!
} else if ($result[0] == 2) {
$this->output->writeln(' - <comment>Skipped!</comment>');
// 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(' - <error>Migration Failed :-(</error>');
$this->output->writeln('<comment>' . implode('\n', $result[1]) . '</comment>');
throw new \Exception('Migration Failure, exit code ' . $result);
}

}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
}



}
6 changes: 1 addition & 5 deletions lib/PimcoreMigrations/Model/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function setFilename($filename)
}


protected function init()
public function init()
{

}
Expand Down Expand Up @@ -252,10 +252,6 @@ public function run($action)

try {

if ($this->getRebuildClassesBefore()) {
\PimcoreMigrations\Tool::rebuildClasses();
}

$this->$action();

if ($this->getRebuildClassesAfter()) {
Expand Down
4 changes: 3 additions & 1 deletion lib/PimcoreMigrations/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -76,7 +77,8 @@ public function getConsoleCommands()
return [
new StatusCommand(),
new UpCommand(),
new DownCommand()
new DownCommand(),
new RunCommand()
];
}

Expand Down

0 comments on commit ad620de

Please sign in to comment.