Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented migration rollback. #1932

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions src/Propel/Generator/Command/Executor/RollbackExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

/**
* MIT License. This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Propel\Generator\Command\Executor;

use Exception;
use Propel\Generator\Manager\MigrationManager;
use Propel\Generator\Util\SqlParser;
use Propel\Runtime\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Service class for executing rollback.
*/
class RollbackExecutor
{
/**
* @var string
*/
protected const COMMAND_OPTION_FAKE = 'fake';

/**
* @var string
*/
protected const COMMAND_OPTION_FORCE = 'force';

/**
* @var string
*/
protected const COMMAND_OPTION_VERBOSE = 'verbose';

/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
protected InputInterface $input;

/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected OutputInterface $output;

/**
* @var \Propel\Generator\Manager\MigrationManager
*/
protected MigrationManager $migrationManager;

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Propel\Generator\Manager\MigrationManager $migrationManager
*/
public function __construct(
InputInterface $input,
OutputInterface $output,
MigrationManager $migrationManager
) {
$this->input = $input;
$this->output = $output;
$this->migrationManager = $migrationManager;
}

/**
* @param list<int> $previousTimestamps
*
* @return bool
*/
public function executeRollbackToPreviousVersion(array &$previousTimestamps): bool
{
$nextMigrationTimestamp = array_pop($previousTimestamps);
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
if (!$nextMigrationTimestamp) {
$this->output->writeln('No migration were ever executed on this database - nothing to reverse.');
dereuromark marked this conversation as resolved.
Show resolved Hide resolved

return false;
}

$this->output->writeln(sprintf(
'Executing migration %s down',
$this->migrationManager->getMigrationClassName($nextMigrationTimestamp),
));

$nbPreviousTimestamps = count($previousTimestamps);
$previousTimestamp = 0;
if ($nbPreviousTimestamps) {
$previousTimestamp = $previousTimestamps[array_key_last($previousTimestamps)];
}

$migration = $this->migrationManager->getMigrationObject($nextMigrationTimestamp);
if (!$this->input->getOption(static::COMMAND_OPTION_FAKE) && $migration->preDown($this->migrationManager) === false) {
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
if (!$this->input->getOption(static::COMMAND_OPTION_FORCE)) {
$this->output->writeln('<error>preDown() returned false. Aborting migration.</error>');

return false;
}

$this->output->writeln('<error>preDown() returned false. Continue migration.</error>');
}

foreach ($migration->getDownSQL() as $datasource => $sql) {
$this->executeRollbackForDatasource($datasource, $sql);

$this->migrationManager->removeMigrationTimestamp($datasource, $nextMigrationTimestamp);

if ($this->input->getOption(static::COMMAND_OPTION_VERBOSE)) {
$this->output->writeln(sprintf(
'Downgraded migration date to %d for datasource "%s"',
$previousTimestamp,
$datasource,
));
}
}

if (!$this->input->getOption(static::COMMAND_OPTION_FAKE)) {
$migration->postDown($this->migrationManager);
}

if ($nbPreviousTimestamps) {
$this->output->writeln(sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
} else {
$this->output->writeln('Reverse migration complete. No more migration available for reverse');
}

return true;
}

/**
* @param string $datasource
* @param string $sql
*
* @throws \Propel\Runtime\Exception\RuntimeException
*
* @return void
*/
protected function executeRollbackForDatasource(string $datasource, string $sql): void
{
$connection = $this->migrationManager->getConnection($datasource);

if ($this->input->getOption(static::COMMAND_OPTION_VERBOSE)) {
$this->output->writeln(sprintf(
'Connecting to database "%s" using DSN "%s"',
$datasource,
$connection['dsn'],
));
}

$conn = $this->migrationManager->getAdapterConnection($datasource);
$res = 0;
$statements = SqlParser::parseString($sql);

if ($this->input->getOption(static::COMMAND_OPTION_FAKE)) {
return;
}

foreach ($statements as $statement) {
try {
if ($this->input->getOption(static::COMMAND_OPTION_VERBOSE)) {
$this->output->writeln(sprintf('Executing statement "%s"', $statement));
}

$conn->exec($statement);
$res++;
} catch (Exception $e) {
if ($this->input->getOption(static::COMMAND_OPTION_FORCE)) {
//continue, but print error message
$this->output->writeln(
sprintf('<error>Failed to execute SQL "%s". Continue migration.</error>', $statement),
);
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new RuntimeException(
sprintf('<error>Failed to execute SQL "%s". Aborting migration.</error>', $statement),
0,
$e,
);
}
}
}

$this->output->writeln(sprintf(
'%d of %d SQL statements executed successfully on datasource "%s"',
$res,
count($statements),
$datasource,
));
}
}
110 changes: 5 additions & 105 deletions src/Propel/Generator/Command/MigrationDownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

namespace Propel\Generator\Command;

use Exception;
use Propel\Generator\Command\Executor\RollbackExecutor;
use Propel\Generator\Manager\MigrationManager;
use Propel\Generator\Util\SqlParser;
use Propel\Runtime\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -41,8 +39,6 @@ protected function configure()

/**
* @inheritDoc
*
* @throws \Propel\Runtime\Exception\RuntimeException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand Down Expand Up @@ -79,108 +75,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$manager->setWorkingDirectory($generatorConfig->getSection('paths')['migrationDir']);

$previousTimestamps = $manager->getAlreadyExecutedMigrationTimestamps();
$nextMigrationTimestamp = array_pop($previousTimestamps);
if (!$nextMigrationTimestamp) {
$output->writeln('No migration were ever executed on this database - nothing to reverse.');

return static::CODE_ERROR;
$rollbackExecutor = new RollbackExecutor($input, $output, $manager);
if ($rollbackExecutor->executeRollbackToPreviousVersion($previousTimestamps)) {
return static::CODE_SUCCESS;
}

$output->writeln(sprintf(
'Executing migration %s down',
$manager->getMigrationClassName($nextMigrationTimestamp),
));

$nbPreviousTimestamps = count($previousTimestamps);
if ($nbPreviousTimestamps) {
$previousTimestamp = array_pop($previousTimestamps);
} else {
$previousTimestamp = 0;
}

$migration = $manager->getMigrationObject($nextMigrationTimestamp);

if (!$input->getOption('fake')) {
if ($migration->preDown($manager) === false) {
if ($input->getOption('force')) {
$output->writeln('<error>preDown() returned false. Continue migration.</error>');
} else {
$output->writeln('<error>preDown() returned false. Aborting migration.</error>');

return static::CODE_ERROR;
}
}
}

foreach ($migration->getDownSQL() as $datasource => $sql) {
$connection = $manager->getConnection($datasource);

if ($input->getOption('verbose')) {
$output->writeln(sprintf(
'Connecting to database "%s" using DSN "%s"',
$datasource,
$connection['dsn'],
));
}

$conn = $manager->getAdapterConnection($datasource);
$res = 0;
$statements = SqlParser::parseString($sql);

if (!$input->getOption('fake')) {
foreach ($statements as $statement) {
try {
if ($input->getOption('verbose')) {
$output->writeln(sprintf('Executing statement "%s"', $statement));
}

$conn->exec($statement);
$res++;
} catch (Exception $e) {
if ($input->getOption('force')) {
//continue, but print error message
$output->writeln(
sprintf('<error>Failed to execute SQL "%s". Continue migration.</error>', $statement),
);
} else {
throw new RuntimeException(
sprintf('<error>Failed to execute SQL "%s". Aborting migration.</error>', $statement),
0,
$e,
);
}
}
}

$output->writeln(sprintf(
'%d of %d SQL statements executed successfully on datasource "%s"',
$res,
count($statements),
$datasource,
));
}

$manager->removeMigrationTimestamp($datasource, $nextMigrationTimestamp);

if ($input->getOption('verbose')) {
$output->writeln(sprintf(
'Downgraded migration date to %d for datasource "%s"',
$previousTimestamp,
$datasource,
));
}
}

if (!$input->getOption('fake')) {
$migration->postDown($manager);
}

if ($nbPreviousTimestamps) {
$output->writeln(sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
} else {
$output->writeln('Reverse migration complete. No more migration available for reverse');
}

return static::CODE_SUCCESS;
return static::CODE_ERROR;
}
}
Loading