Skip to content

Commit

Permalink
Improve informing (#225)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
3 people committed Oct 24, 2023
1 parent ade2eca commit 2ef6724
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/Command/DownCommand.php
Expand Up @@ -105,9 +105,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($instances as $i => $instance) {
try {
$this->downRunner->run($instance);
$this->downRunner->run($instance, $i + 1);
} catch (Throwable $e) {
$output->writeln("\n\n\t<error>>>> [ERROR] - Not reverted " . $instance::class . '</error>');
$output->writeln("\n<fg=yellow> >>> Total $i out of $n $migrationWas reverted.</>\n");
$io->error($i > 0 ? 'Partially reverted.' : 'Not reverted.');

Expand Down
6 changes: 2 additions & 4 deletions src/Command/RedoCommand.php
Expand Up @@ -107,9 +107,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($instances as $i => $instance) {
try {
$this->downRunner->run($instance);
$this->downRunner->run($instance, $i + 1);
} catch (Throwable $e) {
$output->writeln("\n\n\t<error>>>> [ERROR] - Not reverted " . $instance::class . '</error>');
$output->writeln("\n<fg=yellow> >>> Total $i out of $n $migrationWas reverted.</>\n");
$io->error($i > 0 ? 'Partially reverted.' : 'Not reverted.');

Expand All @@ -119,9 +118,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach (array_reverse($instances) as $i => $instance) {
try {
$this->updateRunner->run($instance);
$this->updateRunner->run($instance, $n - $i);
} catch (Throwable $e) {
$output->writeln("\n\n\t<error>>>> [ERROR] - Not applied " . $instance::class . '</error>');
$output->writeln("\n<fg=yellow> >>> Total $i out of $n $migrationWas applied.</>\n");
$io->error($i > 0 ? 'Reverted but partially applied.' : 'Reverted but not applied.');

Expand Down
3 changes: 1 addition & 2 deletions src/Command/UpdateCommand.php
Expand Up @@ -141,9 +141,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($instances as $i => $instance) {
try {
$this->updateRunner->run($instance);
$this->updateRunner->run($instance, $i + 1);
} catch (Throwable $e) {
$output->writeln("\n\n\t<error>>>> [ERROR] - Not applied " . $instance::class . '</error>');
$output->writeln("\n<fg=yellow> >>> Total $i out of $n new $migrationWas applied.</>\n");
$io->error($i > 0 ? 'Partially updated.' : 'Not updated.');

Expand Down
21 changes: 16 additions & 5 deletions src/Runner/DownRunner.php
Expand Up @@ -6,6 +6,7 @@

use RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use Yiisoft\Db\Migration\Migrator;
use Yiisoft\Db\Migration\RevertibleMigrationInterface;

Expand All @@ -23,27 +24,37 @@ public function __construct(private Migrator $migrator)
public function setIO(?SymfonyStyle $io): void
{
$this->io = $io;
$this->migrator->setIO($io);
}

public function run(RevertibleMigrationInterface $migration): void
public function run(RevertibleMigrationInterface $migration, int|null $number = null): void
{
if ($this->io === null) {
throw new RuntimeException('You need to set output decorator via `setIO()`.');
}

$num = $number !== null ? $number . '. ' : '';
$className = $migration::class;

$this->io->title("\nReverting $className");
$this->io->title("\n{$num}Reverting $className");

$start = microtime(true);

$this->migrator->down($migration);
try {
$this->migrator->down($migration);
} catch (Throwable $e) {
$time = microtime(true) - $start;

$this->io->writeln(
"\n\n\t<error>>>> [ERROR] - Not reverted (time: " . sprintf('%.3f', $time) . 's)</error>'
);

throw $e;
}

$time = microtime(true) - $start;

$this->io->writeln(
"\n\t<info>>>> [OK] - Reverted $className (time: " . sprintf('%.3f', $time) . 's)</info>'
"\n\t<info>>>> [OK] - Reverted (time: " . sprintf('%.3f', $time) . 's)</info>'
);
}
}
21 changes: 16 additions & 5 deletions src/Runner/UpdateRunner.php
Expand Up @@ -6,6 +6,7 @@

use RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use Yiisoft\Db\Migration\MigrationInterface;
use Yiisoft\Db\Migration\Migrator;

Expand All @@ -23,27 +24,37 @@ public function __construct(private Migrator $migrator)
public function setIO(?SymfonyStyle $io): void
{
$this->io = $io;
$this->migrator->setIO($io);
}

public function run(MigrationInterface $migration): void
public function run(MigrationInterface $migration, int|null $number = null): void
{
if ($this->io === null) {
throw new RuntimeException('You need to set output decorator via `setIO()`.');
}

$num = $number !== null ? $number . '. ' : '';
$className = $migration::class;

$this->io->title("\nApplying $className");
$this->io->title("\n{$num}Applying $className");

$start = microtime(true);

$this->migrator->up($migration);
try {
$this->migrator->up($migration);
} catch (Throwable $e) {
$time = microtime(true) - $start;

$this->io->writeln(
"\n\n\t<error>>>> [ERROR] - Not applied (time: " . sprintf('%.3f', $time) . 's)</error>'
);

throw $e;
}

$time = microtime(true) - $start;

$this->io->writeln(
"\n\t<info>>>> [OK] - Applied $className (time: " . sprintf('%.3f', $time) . 's)</info>'
"\n\t<info>>>> [OK] - Applied (time: " . sprintf('%.3f', $time) . 's)</info>'
);
}
}
27 changes: 15 additions & 12 deletions src/Service/MigrationService.php
Expand Up @@ -65,26 +65,24 @@ public function setIO(?SymfonyStyle $io): void
*
* @return int Whether the action should continue to be executed.
*/
public function before(string $defaultName): int
public function before(string $commandName): int
{
$result = Command::SUCCESS;

switch ($defaultName) {
switch ($commandName) {
case 'migrate:create':
if (empty($this->createNamespace) && empty($this->createPath)) {
$this->io?->error(
'One of `createNamespace` or `createPath` should be specified.'
);

$result = Command::INVALID;
return Command::INVALID;
}

if (!empty($this->createNamespace) && !empty($this->createPath)) {
$this->io?->error(
'Only one of `createNamespace` or `createPath` should be specified.'
);

$result = Command::INVALID;
return Command::INVALID;
}
break;
case 'migrate:up':
Expand All @@ -93,20 +91,20 @@ public function before(string $defaultName): int
'At least one of `updateNamespaces` or `updatePaths` should be specified.'
);

$result = Command::INVALID;
return Command::INVALID;
}
break;
}

return $result;
return Command::SUCCESS;
}

/**
* Returns the migrations that are not applied.
*
* @return array List of new migrations.
*
* @psalm-return array<int, class-string>
* @psalm-return list<class-string>
*/
public function getNewMigrations(): array
{
Expand Down Expand Up @@ -150,6 +148,8 @@ public function getNewMigrations(): array
$class = $namespace . '\\' . $class;
}

/** @psalm-var class-string $class */

if (!isset($applied[$class])) {
$migrations[$time . '\\' . $class] = $class;
}
Expand All @@ -159,7 +159,6 @@ public function getNewMigrations(): array
}
ksort($migrations);

/** @psalm-var array<int, class-string> */
return array_values($migrations);
}

Expand Down Expand Up @@ -267,9 +266,11 @@ public function makeMigration(string $class): MigrationInterface
/**
* @param string[] $classes
*
* @psalm-param class-string[] $classes
* @psalm-param list<class-string> $classes
*
* @return MigrationInterface[]
*
* @psalm-return list<MigrationInterface>
*/
public function makeMigrations(array $classes): array
{
Expand All @@ -293,9 +294,11 @@ public function makeRevertibleMigration(string $class): RevertibleMigrationInter
/**
* @param string[] $classes
*
* @psalm-param class-string[] $classes
* @psalm-param list<class-string> $classes
*
* @return RevertibleMigrationInterface[]
*
* @psalm-return list<RevertibleMigrationInterface>
*/
public function makeRevertibleMigrations(array $classes): array
{
Expand Down
8 changes: 6 additions & 2 deletions tests/Common/Command/AbstractDownCommandTest.php
Expand Up @@ -227,14 +227,14 @@ public function testLimit(): void
'post',
['name:string(50)'],
);
MigrationHelper::createAndApplyMigration(
$createTagClass = MigrationHelper::createAndApplyMigration(
$this->container,
'Create_Tag',
'table',
'tag',
['name:string(50)'],
);
MigrationHelper::createAndApplyMigration(
$createUserClass = MigrationHelper::createAndApplyMigration(
$this->container,
'Create_User',
'table',
Expand All @@ -249,6 +249,10 @@ public function testLimit(): void
$output = $command->getDisplay(true);

$this->assertSame(Command::SUCCESS, $exitCode);
$this->assertStringContainsString("1. $createUserClass", $output);
$this->assertStringContainsString("2. $createTagClass", $output);
$this->assertStringContainsString("1. Reverting $createUserClass", $output);
$this->assertStringContainsString("2. Reverting $createTagClass", $output);
$this->assertStringContainsString('[OK] 2 migrations were reverted.', $output);
}

Expand Down
16 changes: 12 additions & 4 deletions tests/Common/Command/AbstractRedoCommandTest.php
Expand Up @@ -92,14 +92,14 @@ public function testLimit(): void
{
MigrationHelper::useMigrationsNamespace($this->container);

MigrationHelper::createAndApplyMigration(
$createPostClass = MigrationHelper::createAndApplyMigration(
$this->container,
'Create_Post',
'table',
'post',
['name:string(50)'],
);
MigrationHelper::createAndApplyMigration(
$createUserClass = MigrationHelper::createAndApplyMigration(
$this->container,
'Create_User',
'table',
Expand All @@ -110,13 +110,21 @@ public function testLimit(): void
$command = $this->createCommand($this->container);
$command->setInputs(['yes']);

$exitCode = $command->execute(['-l' => '1']);
$exitCode = $command->execute(['-l' => '2']);
$output = $command->getDisplay(true);

$this->assertSame(Command::SUCCESS, $exitCode);
$this->assertStringContainsString("1. $createUserClass", $output);
$this->assertStringContainsString("2. $createPostClass", $output);
$this->assertStringContainsString("1. Reverting $createUserClass", $output);
$this->assertStringContainsString('Drop table user', $output);
$this->assertStringContainsString("2. Reverting $createPostClass", $output);
$this->assertStringContainsString('Drop table post', $output);
$this->assertStringContainsString("2. Applying $createPostClass", $output);
$this->assertStringContainsString('create table post', $output);
$this->assertStringContainsString("1. Applying $createUserClass", $output);
$this->assertStringContainsString('create table user', $output);
$this->assertStringContainsString('1 migration was redone.', $output);
$this->assertStringContainsString('2 migrations were redone.', $output);
$this->assertStringContainsString('Migration redone successfully.', $output);
$this->assertExistsTables($this->container, 'post', 'user');
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Common/Command/AbstractUpdateCommandTest.php
Expand Up @@ -70,9 +70,10 @@ public function testExecuteWithPath(): void

$this->assertStringContainsString(">>> [OK] - '.Done..'.", $output);
$this->assertStringContainsString('Total 1 new migration to be applied:', $output);
$this->assertStringContainsString("1. $className", $output);
$this->assertStringContainsString('Apply the above migration y/n:', $output);
$this->assertStringContainsString("Applying $className", $output);
$this->assertStringContainsString(">>> [OK] - Applied $className", $output);
$this->assertStringContainsString("1. Applying $className", $output);
$this->assertStringContainsString('>>> [OK] - Applied (time:', $output);
$this->assertStringContainsString('>>> Total 1 new migration was applied.', $output);
}

Expand Down

0 comments on commit 2ef6724

Please sign in to comment.