1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Migrations; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
6
|
|
|
use Doctrine\DBAL\Migrations\Event\MigrationsEventArgs; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class for running migrations to the current version or a manually specified version. |
10
|
|
|
* |
11
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
12
|
|
|
* @link www.doctrine-project.org |
13
|
|
|
* @since 2.0 |
14
|
|
|
* @author Jonathan H. Wage <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Migration |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The OutputWriter object instance used for outputting information |
20
|
|
|
* |
21
|
|
|
* @var OutputWriter |
22
|
|
|
*/ |
23
|
|
|
private $outputWriter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Configuration |
27
|
|
|
*/ |
28
|
|
|
private $configuration; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var boolean |
32
|
|
|
*/ |
33
|
|
|
private $noMigrationException; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Construct a Migration instance |
37
|
|
|
* |
38
|
|
|
* @param Configuration $configuration A migration Configuration instance |
39
|
|
|
*/ |
40
|
37 |
|
public function __construct(Configuration $configuration) |
41
|
|
|
{ |
42
|
37 |
|
$this->configuration = $configuration; |
43
|
37 |
|
$this->outputWriter = $configuration->getOutputWriter(); |
44
|
37 |
|
$this->noMigrationException = false; |
45
|
37 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the array of versions and SQL queries that would be executed for |
49
|
|
|
* each version but do not execute anything. |
50
|
|
|
* |
51
|
|
|
* @param string $to The version to migrate to. |
52
|
|
|
* |
53
|
|
|
* @return array $sql The array of SQL queries. |
54
|
|
|
*/ |
55
|
2 |
|
public function getSql($to = null) |
56
|
|
|
{ |
57
|
2 |
|
return $this->migrate($to, true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Write a migration SQL file to the given path |
62
|
|
|
* |
63
|
|
|
* @param string $path The path to write the migration SQL file. |
64
|
|
|
* @param string $to The version to migrate to. |
65
|
|
|
* |
66
|
|
|
* @return boolean $written |
67
|
|
|
*/ |
68
|
6 |
|
public function writeSqlFile($path, $to = null) |
69
|
|
|
{ |
70
|
6 |
|
$sql = $this->getSql($to); |
71
|
6 |
|
$from = $this->configuration->getCurrentVersion(); |
72
|
|
|
|
73
|
6 |
|
if ($to === null) { |
74
|
1 |
|
$to = $this->configuration->getLatestVersion(); |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
$direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP; |
78
|
|
|
|
79
|
6 |
|
$this->outputWriter->write(sprintf("-- Migrating from %s to %s\n", $from, $to)); |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* Since the configuration object changes during the creation we cannot inject things |
83
|
|
|
* properly, so I had to violate LoD here (so please, let's find a way to solve it on v2). |
84
|
|
|
*/ |
85
|
6 |
|
return $this->configuration->getQueryWriter() |
86
|
6 |
|
->write($path, $direction, $sql); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param boolean $noMigrationException Throw an exception or not if no migration is found. Mostly for Continuous Integration. |
91
|
|
|
*/ |
92
|
2 |
|
public function setNoMigrationException($noMigrationException = false) |
93
|
|
|
{ |
94
|
2 |
|
$this->noMigrationException = $noMigrationException; |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Run a migration to the current version or the given target version. |
99
|
|
|
* |
100
|
|
|
* @param string $to The version to migrate to. |
101
|
|
|
* @param boolean $dryRun Whether or not to make this a dry run and not execute anything. |
102
|
|
|
* @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
103
|
|
|
* @param callable|null $confirm A callback to confirm whether the migrations should be executed. |
104
|
|
|
* |
105
|
|
|
* @return array An array of migration sql statements. This will be empty if the the $confirm callback declines to execute the migration |
106
|
|
|
* |
107
|
|
|
* @throws MigrationException |
108
|
|
|
*/ |
109
|
31 |
|
public function migrate($to = null, $dryRun = false, $timeAllQueries = false, callable $confirm = null) |
110
|
|
|
{ |
111
|
|
|
/** |
112
|
|
|
* If no version to migrate to is given we default to the last available one. |
113
|
|
|
*/ |
114
|
31 |
|
if ($to === null) { |
115
|
14 |
|
$to = $this->configuration->getLatestVersion(); |
116
|
|
|
} |
117
|
|
|
|
118
|
31 |
|
$from = (string) $this->configuration->getCurrentVersion(); |
119
|
31 |
|
$to = (string) $to; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Throw an error if we can't find the migration to migrate to in the registered |
123
|
|
|
* migrations. |
124
|
|
|
*/ |
125
|
31 |
|
$migrations = $this->configuration->getMigrations(); |
126
|
31 |
|
if ( ! isset($migrations[$to]) && $to > 0) { |
127
|
1 |
|
throw MigrationException::unknownMigrationVersion($to); |
128
|
|
|
} |
129
|
|
|
|
130
|
30 |
|
$direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP; |
131
|
30 |
|
$migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* If |
135
|
|
|
* there are no migrations to execute |
136
|
|
|
* and there are migrations, |
137
|
|
|
* and the migration from and to are the same |
138
|
|
|
* means we are already at the destination return an empty array() |
139
|
|
|
* to signify that there is nothing left to do. |
140
|
|
|
*/ |
141
|
30 |
|
if ($from === $to && empty($migrationsToExecute) && ! empty($migrations)) { |
142
|
2 |
|
return $this->noMigrations(); |
143
|
|
|
} |
144
|
|
|
|
145
|
29 |
|
if ( ! $dryRun && false === $this->migrationsCanExecute($confirm)) { |
146
|
1 |
|
return []; |
147
|
|
|
} |
148
|
|
|
|
149
|
28 |
|
$output = $dryRun ? 'Executing dry run of migration' : 'Migrating'; |
150
|
28 |
|
$output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>'; |
151
|
28 |
|
$this->outputWriter->write(sprintf($output, $direction, $to, $from)); |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* If there are no migrations to execute throw an exception. |
155
|
|
|
*/ |
156
|
28 |
|
if (empty($migrationsToExecute) && ! $this->noMigrationException) { |
157
|
1 |
|
throw MigrationException::noMigrationsToExecute(); |
158
|
|
|
} elseif (empty($migrationsToExecute)) { |
159
|
1 |
|
return $this->noMigrations(); |
160
|
|
|
} |
161
|
|
|
|
162
|
26 |
|
$this->configuration->dispatchEvent( |
163
|
26 |
|
Events::onMigrationsMigrating, |
164
|
26 |
|
new MigrationsEventArgs($this->configuration, $direction, $dryRun) |
165
|
|
|
); |
166
|
|
|
|
167
|
26 |
|
$sql = []; |
168
|
26 |
|
$time = 0; |
169
|
|
|
|
170
|
26 |
|
foreach ($migrationsToExecute as $version) { |
171
|
26 |
|
$versionSql = $version->execute($direction, $dryRun, $timeAllQueries); |
172
|
26 |
|
$sql[$version->getVersion()] = $versionSql; |
173
|
26 |
|
$time += $version->getTime(); |
174
|
|
|
} |
175
|
|
|
|
176
|
26 |
|
$this->configuration->dispatchEvent( |
177
|
26 |
|
Events::onMigrationsMigrated, |
178
|
26 |
|
new MigrationsEventArgs($this->configuration, $direction, $dryRun) |
179
|
|
|
); |
180
|
|
|
|
181
|
26 |
|
$this->outputWriter->write("\n <comment>------------------------</comment>\n"); |
182
|
26 |
|
$this->outputWriter->write(sprintf(" <info>++</info> finished in %ss", $time)); |
183
|
26 |
|
$this->outputWriter->write(sprintf(" <info>++</info> %s migrations executed", count($migrationsToExecute))); |
184
|
26 |
|
$this->outputWriter->write(sprintf(" <info>++</info> %s sql queries", count($sql, true) - count($sql))); |
185
|
|
|
|
186
|
26 |
|
return $sql; |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
private function noMigrations() : array |
190
|
|
|
{ |
191
|
3 |
|
$this->outputWriter->write('<comment>No migrations to execute.</comment>'); |
192
|
|
|
|
193
|
3 |
|
return []; |
194
|
|
|
} |
195
|
|
|
|
196
|
27 |
|
private function migrationsCanExecute(callable $confirm = null) : bool |
197
|
|
|
{ |
198
|
27 |
|
return null === $confirm ? true : (bool) $confirm(); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|