1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
8
|
|
|
use Doctrine\Migrations\Exception\MigrationException; |
9
|
|
|
use Doctrine\Migrations\Exception\NoMigrationsToExecute; |
10
|
|
|
use Doctrine\Migrations\Exception\UnknownMigrationVersion; |
11
|
|
|
use Doctrine\Migrations\Tools\BytesFormatter; |
12
|
|
|
use const COUNT_RECURSIVE; |
13
|
|
|
use function count; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
class Migrator |
17
|
|
|
{ |
18
|
|
|
/** @var Configuration */ |
19
|
|
|
private $configuration; |
20
|
|
|
|
21
|
|
|
/** @var MigrationRepository */ |
22
|
|
|
private $migrationRepository; |
23
|
|
|
|
24
|
|
|
/** @var OutputWriter */ |
25
|
|
|
private $outputWriter; |
26
|
|
|
|
27
|
|
|
/** @var Stopwatch */ |
28
|
|
|
private $stopwatch; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
private $noMigrationException = false; |
32
|
|
|
|
33
|
37 |
|
public function __construct( |
34
|
|
|
Configuration $configuration, |
35
|
|
|
MigrationRepository $migrationRepository, |
36
|
|
|
OutputWriter $outputWriter, |
37
|
|
|
Stopwatch $stopwatch |
38
|
|
|
) { |
39
|
37 |
|
$this->configuration = $configuration; |
40
|
37 |
|
$this->migrationRepository = $migrationRepository; |
41
|
37 |
|
$this->outputWriter = $outputWriter; |
42
|
37 |
|
$this->stopwatch = $stopwatch; |
43
|
37 |
|
} |
44
|
|
|
|
45
|
2 |
|
public function setNoMigrationException(bool $noMigrationException = false) : void |
46
|
|
|
{ |
47
|
2 |
|
$this->noMigrationException = $noMigrationException; |
48
|
2 |
|
} |
49
|
|
|
|
50
|
|
|
/** @return string[][] */ |
51
|
2 |
|
public function getSql(?string $to = null) : array |
52
|
|
|
{ |
53
|
2 |
|
return $this->migrate($to, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
public function writeSqlFile(string $path, ?string $to = null) : bool |
57
|
|
|
{ |
58
|
6 |
|
$sql = $this->getSql($to); |
59
|
|
|
|
60
|
6 |
|
$from = $this->migrationRepository->getCurrentVersion(); |
61
|
|
|
|
62
|
6 |
|
if ($to === null) { |
63
|
1 |
|
$to = $this->migrationRepository->getLatestVersion(); |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
$direction = $from > $to |
67
|
1 |
|
? VersionDirection::DOWN |
68
|
6 |
|
: VersionDirection::UP; |
69
|
|
|
|
70
|
6 |
|
$this->outputWriter->write( |
71
|
6 |
|
sprintf("-- Migrating from %s to %s\n", $from, $to) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Since the configuration object changes during the creation we cannot inject things |
76
|
|
|
* properly, so I had to violate LoD here (so please, let's find a way to solve it on v2). |
77
|
|
|
*/ |
78
|
6 |
|
return $this->configuration |
79
|
6 |
|
->getQueryWriter() |
80
|
6 |
|
->write($path, $direction, $sql); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @throws MigrationException |
85
|
|
|
* |
86
|
|
|
* @return string[][] |
87
|
|
|
*/ |
88
|
31 |
|
public function migrate( |
89
|
|
|
?string $to = null, |
90
|
|
|
bool $dryRun = false, |
91
|
|
|
bool $timeAllQueries = false, |
92
|
|
|
?callable $confirm = null |
93
|
|
|
) : array { |
94
|
31 |
|
if ($to === null) { |
95
|
14 |
|
$to = $this->migrationRepository->getLatestVersion(); |
96
|
|
|
} |
97
|
|
|
|
98
|
31 |
|
$from = $this->migrationRepository->getCurrentVersion(); |
99
|
|
|
|
100
|
31 |
|
$versions = $this->migrationRepository->getMigrations(); |
101
|
|
|
|
102
|
31 |
|
if (! isset($versions[$to]) && $to > 0) { |
103
|
1 |
|
throw UnknownMigrationVersion::new($to); |
104
|
|
|
} |
105
|
|
|
|
106
|
30 |
|
$direction = $from > $to |
107
|
3 |
|
? VersionDirection::DOWN |
108
|
30 |
|
: VersionDirection::UP; |
109
|
|
|
|
110
|
30 |
|
$migrationsToExecute = $this->configuration |
111
|
30 |
|
->getMigrationsToExecute($direction, $to); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* If |
115
|
|
|
* there are no migrations to execute |
116
|
|
|
* and there are migrations, |
117
|
|
|
* and the migration from and to are the same |
118
|
|
|
* means we are already at the destination return an empty array() |
119
|
|
|
* to signify that there is nothing left to do. |
120
|
|
|
*/ |
121
|
30 |
|
if ($from === $to && count($migrationsToExecute) === 0 && count($versions) !== 0) { |
122
|
2 |
|
return $this->noMigrations(); |
123
|
|
|
} |
124
|
|
|
|
125
|
29 |
|
if (! $dryRun && $this->migrationsCanExecute($confirm) === false) { |
126
|
1 |
|
return []; |
127
|
|
|
} |
128
|
|
|
|
129
|
28 |
|
$output = $dryRun ? 'Executing dry run of migration' : 'Migrating'; |
130
|
28 |
|
$output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>'; |
131
|
|
|
|
132
|
28 |
|
$this->outputWriter->write(sprintf($output, $direction, $to, $from)); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* If there are no migrations to execute throw an exception. |
136
|
|
|
*/ |
137
|
28 |
|
if (count($migrationsToExecute) === 0 && ! $this->noMigrationException) { |
138
|
1 |
|
throw NoMigrationsToExecute::new(); |
139
|
27 |
|
} elseif (count($migrationsToExecute) === 0) { |
140
|
1 |
|
return $this->noMigrations(); |
141
|
|
|
} |
142
|
|
|
|
143
|
26 |
|
$stopwatchEvent = $this->stopwatch->start('migrate'); |
144
|
|
|
|
145
|
26 |
|
$this->configuration->dispatchMigrationEvent(Events::onMigrationsMigrating, $direction, $dryRun); |
146
|
|
|
|
147
|
26 |
|
$sql = []; |
148
|
|
|
|
149
|
26 |
|
foreach ($migrationsToExecute as $version) { |
150
|
26 |
|
$versionExecutionResult = $version->execute($direction, $dryRun, $timeAllQueries); |
151
|
|
|
|
152
|
26 |
|
$sql[$version->getVersion()] = $versionExecutionResult->getSql(); |
153
|
|
|
} |
154
|
|
|
|
155
|
26 |
|
$this->configuration->dispatchMigrationEvent(Events::onMigrationsMigrated, $direction, $dryRun); |
156
|
|
|
|
157
|
26 |
|
$stopwatchEvent->stop(); |
158
|
|
|
|
159
|
26 |
|
$this->outputWriter->write("\n <comment>------------------------</comment>\n"); |
160
|
|
|
|
161
|
26 |
|
$this->outputWriter->write(sprintf( |
162
|
26 |
|
' <info>++</info> finished in %sms', |
163
|
26 |
|
$stopwatchEvent->getDuration() |
164
|
|
|
)); |
165
|
|
|
|
166
|
26 |
|
$this->outputWriter->write(sprintf( |
167
|
26 |
|
' <info>++</info> used %s memory', |
168
|
26 |
|
BytesFormatter::formatBytes($stopwatchEvent->getMemory()) |
169
|
|
|
)); |
170
|
|
|
|
171
|
26 |
|
$this->outputWriter->write(sprintf( |
172
|
26 |
|
' <info>++</info> %s migrations executed', |
173
|
26 |
|
count($migrationsToExecute) |
174
|
|
|
)); |
175
|
|
|
|
176
|
26 |
|
$this->outputWriter->write(sprintf( |
177
|
26 |
|
' <info>++</info> %s sql queries', |
178
|
26 |
|
count($sql, COUNT_RECURSIVE) - count($sql) |
179
|
|
|
)); |
180
|
|
|
|
181
|
26 |
|
return $sql; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** @return string[][] */ |
185
|
3 |
|
private function noMigrations() : array |
186
|
|
|
{ |
187
|
3 |
|
$this->outputWriter->write('<comment>No migrations to execute.</comment>'); |
188
|
|
|
|
189
|
3 |
|
return []; |
190
|
|
|
} |
191
|
|
|
|
192
|
27 |
|
private function migrationsCanExecute(?callable $confirm = null) : bool |
193
|
|
|
{ |
194
|
27 |
|
return $confirm === null ? true : (bool) $confirm(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|