-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathDbalMigrator.php
163 lines (130 loc) · 4.94 KB
/
DbalMigrator.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace Doctrine\Migrations;
use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Exception\MigrationConfigurationConflict;
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tools\BytesFormatter;
use Doctrine\Migrations\Tools\TransactionHelper;
use Doctrine\Migrations\Version\Executor;
use Psr\Log\LoggerInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;
use Throwable;
use function count;
use const COUNT_RECURSIVE;
/**
* The DbalMigrator class is responsible for generating and executing the SQL for a migration.
*
* @internal
*/
class DbalMigrator implements Migrator
{
private Stopwatch $stopwatch;
private LoggerInterface $logger;
private Executor $executor;
private Connection $connection;
private EventDispatcher $dispatcher;
public function __construct(
Connection $connection,
EventDispatcher $dispatcher,
Executor $executor,
LoggerInterface $logger,
Stopwatch $stopwatch
) {
$this->stopwatch = $stopwatch;
$this->logger = $logger;
$this->executor = $executor;
$this->connection = $connection;
$this->dispatcher = $dispatcher;
}
/**
* @return array<string, Query[]>
*/
private function executeMigrations(
MigrationPlanList $migrationsPlan,
MigratorConfiguration $migratorConfiguration
): array {
$allOrNothing = $migratorConfiguration->isAllOrNothing();
if ($allOrNothing) {
$this->assertAllMigrationsAreTransactional($migrationsPlan);
$this->connection->beginTransaction();
}
try {
$this->dispatcher->dispatchMigrationEvent(Events::onMigrationsMigrating, $migrationsPlan, $migratorConfiguration);
$sql = $this->executePlan($migrationsPlan, $migratorConfiguration);
$this->dispatcher->dispatchMigrationEvent(Events::onMigrationsMigrated, $migrationsPlan, $migratorConfiguration);
} catch (Throwable $e) {
if ($allOrNothing) {
TransactionHelper::rollback($this->connection);
}
throw $e;
}
if ($allOrNothing) {
TransactionHelper::commit($this->connection);
}
return $sql;
}
private function assertAllMigrationsAreTransactional(MigrationPlanList $migrationsPlan): void
{
foreach ($migrationsPlan->getItems() as $plan) {
if (! $plan->getMigration()->isTransactional()) {
throw MigrationConfigurationConflict::migrationIsNotTransactional($plan->getMigration());
}
}
}
/**
* @return array<string, Query[]>
*/
private function executePlan(MigrationPlanList $migrationsPlan, MigratorConfiguration $migratorConfiguration): array
{
$sql = [];
$time = 0;
foreach ($migrationsPlan->getItems() as $plan) {
$versionExecutionResult = $this->executor->execute($plan, $migratorConfiguration);
// capture the to Schema for the migration so we have the ability to use
// it as the from Schema for the next migration when we are running a dry run
// $toSchema may be null in the case of skipped migrations
if (! $versionExecutionResult->isSkipped()) {
$migratorConfiguration->setFromSchema($versionExecutionResult->getToSchema());
}
$sql[(string) $plan->getVersion()] = $versionExecutionResult->getSql();
$time += $versionExecutionResult->getTime();
}
return $sql;
}
/**
* @param array<string, Query[]> $sql
*/
private function endMigrations(
StopwatchEvent $stopwatchEvent,
MigrationPlanList $migrationsPlan,
array $sql
): void {
$stopwatchEvent->stop();
$this->logger->notice(
'finished in {duration}ms, used {memory} memory, {migrations_count} migrations executed, {queries_count} sql queries',
[
'duration' => $stopwatchEvent->getDuration(),
'memory' => BytesFormatter::formatBytes($stopwatchEvent->getMemory()),
'migrations_count' => count($migrationsPlan),
'queries_count' => count($sql, COUNT_RECURSIVE) - count($sql),
]
);
}
/**
* {@inheritDoc}
*/
public function migrate(MigrationPlanList $migrationsPlan, MigratorConfiguration $migratorConfiguration): array
{
if (count($migrationsPlan) === 0) {
$this->logger->notice('No migrations to execute.');
return [];
}
$stopwatchEvent = $this->stopwatch->start('migrate');
$sql = $this->executeMigrations($migrationsPlan, $migratorConfiguration);
$this->endMigrations($stopwatchEvent, $migrationsPlan, $sql);
return $sql;
}
}