-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
DownRunner.php
60 lines (44 loc) · 1.43 KB
/
DownRunner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Migration\Runner;
use RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use Yiisoft\Db\Migration\Migrator;
use Yiisoft\Db\Migration\RevertibleMigrationInterface;
use function microtime;
use function sprintf;
final class DownRunner
{
private ?SymfonyStyle $io = null;
public function __construct(private Migrator $migrator)
{
}
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}
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("\n{$num}Reverting $className");
$start = microtime(true);
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 (time: " . sprintf('%.3f', $time) . 's)</info>'
);
}
}