Skip to content

Commit

Permalink
Allow to use --write-path to save the migration execution log to file
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 23, 2020
1 parent ebd2551 commit f9e693f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 60 deletions.
38 changes: 18 additions & 20 deletions lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function implode;
use function is_string;
use function is_writable;
use function sprintf;

/**
* The ExecuteCommand class is responsible for executing migration versions up or down manually.
Expand Down Expand Up @@ -114,42 +115,39 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized();

$versions = $input->getArgument('versions');
$path = $input->getOption('write-sql');
$direction = $input->getOption('down') !== false
? Direction::DOWN
: Direction::UP;

$migrator = $this->getDependencyFactory()->getMigrator();
$path = $input->getOption('write-sql') ?? getcwd();
if (is_string($path) && ! is_writable($path)) {
$this->io->error(sprintf('The path "%s" not writeable!', $path));

return 1;
}

$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator();
$plan = $planCalculator->getPlanForVersions(array_map(static function (string $version) : Version {
return new Version($version);
}, $versions), $direction);

if ($migratorConfiguration->isDryRun()) {
$sql = $migrator->migrate($plan, $migratorConfiguration);

$path = is_string($path) ? $path : getcwd();

if (! is_string($path) || ! is_writable($path)) {
$this->io->error('Path not writeable!');

return 1;
}

$writer = $this->getDependencyFactory()->getQueryWriter();
$writer->write($path, $direction, $sql);

return 0;
}

$this->getDependencyFactory()->getLogger()->notice(
'Executing' . ($migratorConfiguration->isDryRun() ? ' (dry-run)' : '') . ' {versions} {direction}',
[
'direction' => $plan->getDirection(),
'versions' => implode(', ', $versions),
]
);
$migrator->migrate($plan, $migratorConfiguration);

$migrator = $this->getDependencyFactory()->getMigrator();
$sql = $migrator->migrate($plan, $migratorConfiguration);

if (is_string($path)) {
$writer = $this->getDependencyFactory()->getQueryWriter();
$writer->write($path, $direction, $sql);
}

$this->io->newLine();

return 0;
}
Expand Down
34 changes: 14 additions & 20 deletions lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ protected function execute(InputInterface $input, OutputInterface $output) : int

$allowNoMigration = $input->getOption('allow-no-migration');
$versionAlias = $input->getArgument('version');
$path = $input->getOption('write-sql');

$path = $input->getOption('write-sql') ?? getcwd();
if (is_string($path) && ! is_writable($path)) {
$this->io->error(sprintf('The path "%s" not writeable!', $path));

return 1;
}

try {
$version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($versionAlias);
Expand Down Expand Up @@ -162,24 +168,6 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
return 0;
}

$migrator = $this->getDependencyFactory()->getMigrator();
if ($migratorConfiguration->isDryRun()) {
$sql = $migrator->migrate($plan, $migratorConfiguration);

$path = is_string($path) ? $path : getcwd();

if (! is_string($path) || ! is_writable($path)) {
$this->io->error('Path not writeable!');

return 1;
}

$writer = $this->getDependencyFactory()->getQueryWriter();
$writer->write($path, $plan->getDirection(), $sql);

return 0;
}

$this->getDependencyFactory()->getLogger()->notice(
'Migrating' . ($migratorConfiguration->isDryRun() ? ' (dry-run)' : '') . ' {direction} to {to}',
[
Expand All @@ -188,7 +176,13 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
]
);

$migrator->migrate($plan, $migratorConfiguration);
$migrator = $this->getDependencyFactory()->getMigrator();
$sql = $migrator->migrate($plan, $migratorConfiguration);

if (is_string($path)) {
$writer = $this->getDependencyFactory()->getQueryWriter();
$writer->write($path, $plan->getDirection(), $sql);
}

$this->io->newLine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ public function getMigratorConfiguration(InputInterface $input) : MigratorConfig
{
$timeAllQueries = $input->hasOption('query-time') ? (bool) $input->getOption('query-time') : false;
$dryRun = $input->hasOption('dry-run') ? (bool) $input->getOption('dry-run') : false;
$writeSql = $input->hasOption('write-sql') ? $input->getOption('write-sql') : false;
$allOrNothing = $input->hasOption('all-or-nothing') ? (bool) $input->getOption('all-or-nothing') : $this->configuration->isAllOrNothing();

return (new MigratorConfiguration())
->setDryRun($dryRun || $writeSql !== false)
->setDryRun($dryRun)
->setTimeAllQueries($timeAllQueries)
->setAllOrNothing($allOrNothing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,37 @@ class ExecuteCommandTest extends MigrationTestCase
private $planCalculator;

/**
* @param mixed $arg
* @param bool|string|null $arg
*
* @dataProvider getWriteSqlValues
*/
public function testWriteSql($arg, string $path) : void
public function testWriteSql(bool $dryRun, $arg, ?string $path) : void
{
$this->migrator
->expects(self::once())
->method('migrate')
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) : array {
self::assertTrue($configuration->isDryRun());
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) use ($dryRun) : array {
self::assertSame($dryRun, $configuration->isDryRun());

return ['A'];
});

$this->queryWriter->expects(self::once())
->method('write')
->with($path, 'down', ['A']);
if ($arg === false) {
$this->queryWriter
->expects(self::never())
->method('write');
} else {
$this->queryWriter
->expects(self::once())
->method('write')
->with($path, 'down', ['A']);
}

$this->executeCommandTester->execute([
'versions' => ['1'],
'--down' => true,
'--write-sql' => $arg,
'--dry-run' => $dryRun,
]);

self::assertSame(0, $this->executeCommandTester->getStatusCode());
Expand All @@ -81,8 +89,14 @@ public function testWriteSql($arg, string $path) : void
public function getWriteSqlValues() : array
{
return [
[true, getcwd()],
[ __DIR__ . '/_files', __DIR__ . '/_files'],
// dry-run, write-path, path
[true, false, null],
[true, null, getcwd()],
[true, __DIR__ . '/_files', __DIR__ . '/_files'],

[false, false, null],
[false, null, getcwd()],
[false, __DIR__ . '/_files', __DIR__ . '/_files'],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,40 @@ public function testExecutedUnavailableMigrationsCancel() : void
}

/**
* @param mixed $arg
* @param bool|string|null $arg
*
* @dataProvider getWriteSqlValues
*/
public function testExecuteWriteSql($arg, string $path) : void
public function testExecuteWriteSql(bool $dryRun, $arg, ?string $path) : void
{
$migrator = $this->createMock(DbalMigrator::class);

$this->dependencyFactory->setService(Migrator::class, $migrator);

$migrator->expects(self::once())
->method('migrate')
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) : array {
->willReturnCallback(static function (MigrationPlanList $planList, MigratorConfiguration $configuration) use ($dryRun) : array {
self::assertSame($dryRun, $configuration->isDryRun());

return ['A'];
});

$this->queryWriter->expects(self::once())
->method('write')
->with($path, 'up', ['A']);
if ($arg === false) {
$this->queryWriter
->expects(self::never())
->method('write');
} else {
$this->queryWriter
->expects(self::once())
->method('write')
->with($path, 'up', ['A']);
}

$this->migrateCommandTester->execute(
['--write-sql' => $arg],
[
'--write-sql' => $arg,
'--dry-run' => $dryRun,
],
['interactive' => false]
);
self::assertSame(0, $this->migrateCommandTester->getStatusCode());
Expand All @@ -198,8 +210,14 @@ public function testExecuteWriteSql($arg, string $path) : void
public function getWriteSqlValues() : array
{
return [
[true, getcwd()],
[ __DIR__ . '/_files', __DIR__ . '/_files'],
// dry-run, write-path, path
[true, false, null],
[true, null, getcwd()],
[true, __DIR__ . '/_files', __DIR__ . '/_files'],

[false, false, null],
[false, null, getcwd()],
[false, __DIR__ . '/_files', __DIR__ . '/_files'],
];
}

Expand Down

0 comments on commit f9e693f

Please sign in to comment.