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

enable verbosity for OutputWrite #859

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/OutputWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function setCallback(callable $callback) : void
$this->callback = $callback;
}

public function write(string $message) : void
public function write(string $message, int $option = 0) : void
{
($this->callback)($message);
($this->callback)($message, $option);
}

public function getLastMessage() : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ protected function getMigrationConfiguration(
$this->migrationConfiguration = $configHelper->getMigrationConfig($input);

$this->migrationConfiguration->getOutputWriter()->setCallback(
static function (string $message) use ($output) : void {
$output->writeln($message);
static function (string $message, int $options) use ($output) : void {
$output->writeln($message, $options);
}
);
}
Expand Down
23 changes: 15 additions & 8 deletions lib/Doctrine/Migrations/Version/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Migrations\Provider\SchemaDiffProviderInterface;
use Doctrine\Migrations\Stopwatch;
use Doctrine\Migrations\Tools\BytesFormatter;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function count;
use function rtrim;
Expand Down Expand Up @@ -197,7 +198,8 @@ private function executeMigration(

$migration->{'pre' . ucfirst($direction)}($fromSchema);

$this->outputWriter->write("\n" . $this->getMigrationHeader($version, $migration, $direction) . "\n");
$this->outputWriter->write("\n" . $this->getMigrationHeader($version, $migration, $direction));
$this->outputWriter->write("\n", OutputInterface::VERBOSITY_VERBOSE);

$version->setState(State::EXEC);

Expand Down Expand Up @@ -239,15 +241,17 @@ private function executeMigration(
$versionExecutionResult->setTime($stopwatchEvent->getDuration());
$versionExecutionResult->setMemory($stopwatchEvent->getMemory());

$this->outputWriter->write("\n", OutputInterface::VERBOSITY_VERBOSE);

if ($direction === Direction::UP) {
$this->outputWriter->write(sprintf(
"\n <info>++</info> migrated (took %sms, used %s memory)",
' <info>++</info> migrated (took %sms, used %s memory)',
$stopwatchEvent->getDuration(),
BytesFormatter::formatBytes($stopwatchEvent->getMemory())
));
} else {
$this->outputWriter->write(sprintf(
"\n <info>--</info> reverted (took %sms, used %s memory)",
' <info>--</info> reverted (took %sms, used %s memory)',
$stopwatchEvent->getDuration(),
BytesFormatter::formatBytes($stopwatchEvent->getMemory())
));
Expand Down Expand Up @@ -377,11 +381,14 @@ private function outputSqlQuery(int $idx, string $query) : void
$this->types[$idx] ?? []
);

$this->outputWriter->write(rtrim(sprintf(
' <comment>-></comment> %s %s',
$query,
$params
)));
$this->outputWriter->write(
rtrim(sprintf(
' <comment>-></comment> %s %s',
$query,
$params
)),
OutputInterface::VERBOSITY_VERBOSE
);
}

private function getFromSchema(MigratorConfiguration $migratorConfiguration) : Schema
Expand Down
52 changes: 38 additions & 14 deletions tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,35 @@ public function testExecuteUp() : void
{
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>++</info> migrating <comment>001</comment>\n");
->with("\n <info>++</info> migrating <comment>001</comment>");

$this->outputWriter->expects(self::at(1))
->method('write')
->with(' <comment>-></comment> SELECT 1');
->with("\n");

$this->outputWriter->expects(self::at(2))
->method('write')
->with(' <info>100ms</info>');
->with(' <comment>-></comment> SELECT 1');

$this->outputWriter->expects(self::at(3))
->method('write')
->with(' <comment>-></comment> SELECT 2');
->with(' <info>100ms</info>');

$this->outputWriter->expects(self::at(4))
->method('write')
->with(' <info>100ms</info>');
->with(' <comment>-></comment> SELECT 2');

$this->outputWriter->expects(self::at(5))
->method('write')
->with("\n <info>++</info> migrated (took 100ms, used 100 memory)");
->with(' <info>100ms</info>');

$this->outputWriter->expects(self::at(6))
->method('write')
->with("\n");

$this->outputWriter->expects(self::at(7))
->method('write')
->with(' <info>++</info> migrated (took 100ms, used 100 memory)');

$migratorConfiguration = (new MigratorConfiguration())
->setTimeAllQueries(true);
Expand Down Expand Up @@ -114,7 +122,11 @@ public function executeUpShouldAppendDescriptionWhenItIsNotEmpty() : void
{
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>++</info> migrating <comment>001 (testing)</comment>\n");
->with("\n <info>++</info> migrating <comment>001 (testing)</comment>");

$this->outputWriter->expects(self::at(1))
->method('write')
->with("\n");

$migratorConfiguration = (new MigratorConfiguration())
->setTimeAllQueries(true);
Expand All @@ -131,27 +143,35 @@ public function testExecuteDown() : void
{
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>--</info> reverting <comment>001</comment>\n");
->with("\n <info>--</info> reverting <comment>001</comment>");

$this->outputWriter->expects(self::at(1))
->method('write')
->with(' <comment>-></comment> SELECT 3');
->with("\n");

$this->outputWriter->expects(self::at(2))
->method('write')
->with(' <info>100ms</info>');
->with(' <comment>-></comment> SELECT 3');

$this->outputWriter->expects(self::at(3))
->method('write')
->with(' <comment>-></comment> SELECT 4');
->with(' <info>100ms</info>');

$this->outputWriter->expects(self::at(4))
->method('write')
->with(' <info>100ms</info>');
->with(' <comment>-></comment> SELECT 4');

$this->outputWriter->expects(self::at(5))
->method('write')
->with("\n <info>--</info> reverted (took 100ms, used 100 memory)");
->with(' <info>100ms</info>');

$this->outputWriter->expects(self::at(6))
->method('write')
->with("\n");

$this->outputWriter->expects(self::at(7))
->method('write')
->with(' <info>--</info> reverted (took 100ms, used 100 memory)');

$migratorConfiguration = (new MigratorConfiguration())
->setTimeAllQueries(true);
Expand Down Expand Up @@ -181,7 +201,11 @@ public function executeDownShouldAppendDescriptionWhenItIsNotEmpty() : void
{
$this->outputWriter->expects(self::at(0))
->method('write')
->with("\n <info>--</info> reverting <comment>001 (testing)</comment>\n");
->with("\n <info>--</info> reverting <comment>001 (testing)</comment>");

$this->outputWriter->expects(self::at(1))
->method('write')
->with("\n");

$migratorConfiguration = (new MigratorConfiguration())
->setTimeAllQueries(true);
Expand Down
28 changes: 14 additions & 14 deletions tests/Doctrine/Migrations/Tests/Version/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ public function testDryRunCausesSqlToBeOutputViaTheOutputWriter() : void
$version->execute(Direction::UP, (new MigratorConfiguration())
->setDryRun(true));

self::assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
self::assertContains('SELECT 1 WHERE 1', $messages[1]);
self::assertCount(5, $messages, 'should have written three messages (header, footer, 1 SQL statement, 2x new line)');
self::assertContains('SELECT 1 WHERE 1', $messages[2]);
}

public function testDryRunWithQuestionMarkedParamsOutputsParamsWithSqlStatement() : void
Expand All @@ -491,9 +491,9 @@ public function testDryRunWithQuestionMarkedParamsOutputsParamsWithSqlStatement(
$version->execute(Direction::UP, (new MigratorConfiguration())
->setDryRun(true));

self::assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
self::assertContains('INSERT INTO test VALUES (?, ?)', $messages[1]);
self::assertContains('with parameters ([one], [two])', $messages[1]);
self::assertCount(5, $messages, 'should have written three messages (header, footer, 1 SQL statement, 2x new line)');
self::assertContains('INSERT INTO test VALUES (?, ?)', $messages[2]);
self::assertContains('with parameters ([one], [two])', $messages[2]);
}

public function testDryRunWithNamedParametersOutputsParamsAndNamesWithSqlStatement() : void
Expand All @@ -516,9 +516,9 @@ public function testDryRunWithNamedParametersOutputsParamsAndNamesWithSqlStateme
$version->execute(Direction::UP, (new MigratorConfiguration())
->setDryRun(true));

self::assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
self::assertContains('INSERT INTO test VALUES (:one, :two)', $messages[1]);
self::assertContains('with parameters (:one => [one], :two => [two])', $messages[1]);
self::assertCount(5, $messages, 'should have written three messages (header, footer, 1 SQL statement, 2x new line)');
self::assertContains('INSERT INTO test VALUES (:one, :two)', $messages[2]);
self::assertContains('with parameters (:one => [one], :two => [two])', $messages[2]);
}

/** @return mixed[][] */
Expand Down Expand Up @@ -568,9 +568,9 @@ public function testDryRunWithParametersOfComplexTypesCorrectFormatsParameters(
$version->execute(Direction::UP, (new MigratorConfiguration())
->setDryRun(true));

self::assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
self::assertContains('INSERT INTO test VALUES (?)', $messages[1]);
self::assertContains(sprintf('with parameters (%s)', $output), $messages[1]);
self::assertCount(5, $messages, 'should have written three messages (header, footer, 1 SQL statement, 2x new line)');
self::assertContains('INSERT INTO test VALUES (?)', $messages[2]);
self::assertContains(sprintf('with parameters (%s)', $output), $messages[2]);
}

public function testRunWithInsertNullValue() : void
Expand Down Expand Up @@ -599,9 +599,9 @@ public function testRunWithInsertNullValue() : void
$version->execute(Direction::UP, (new MigratorConfiguration())
->setDryRun(true));

self::assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
self::assertContains('INSERT INTO test VALUES (?)', $messages[1]);
self::assertContains('with parameters ([])', $messages[1]);
self::assertCount(5, $messages, 'should have written three messages (header, footer, 1 SQL statement, 2x new line)');
self::assertContains('INSERT INTO test VALUES (?)', $messages[2]);
self::assertContains('with parameters ([])', $messages[2]);
}

/**
Expand Down