1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Migrations; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class for running migrations to the current version or a manually specified version. |
26
|
|
|
* |
27
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
28
|
|
|
* @link www.doctrine-project.org |
29
|
|
|
* @since 2.0 |
30
|
|
|
* @author Jonathan H. Wage <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Migration |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The OutputWriter object instance used for outputting information |
36
|
|
|
* |
37
|
|
|
* @var OutputWriter |
38
|
|
|
*/ |
39
|
|
|
private $outputWriter; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Configuration |
43
|
|
|
*/ |
44
|
|
|
private $configuration; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var boolean |
48
|
|
|
*/ |
49
|
|
|
private $noMigrationException; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Construct a Migration instance |
53
|
|
|
* |
54
|
|
|
* @param Configuration $configuration A migration Configuration instance |
55
|
|
|
*/ |
56
|
34 |
|
public function __construct(Configuration $configuration) |
57
|
|
|
{ |
58
|
34 |
|
$this->configuration = $configuration; |
59
|
34 |
|
$this->outputWriter = $configuration->getOutputWriter(); |
60
|
34 |
|
$this->noMigrationException = false; |
61
|
34 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the array of versions and SQL queries that would be executed for |
65
|
|
|
* each version but do not execute anything. |
66
|
|
|
* |
67
|
|
|
* @param string $to The version to migrate to. |
68
|
|
|
* |
69
|
|
|
* @return array $sql The array of SQL queries. |
70
|
|
|
*/ |
71
|
2 |
|
public function getSql($to = null) |
72
|
|
|
{ |
73
|
2 |
|
return $this->migrate($to, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Write a migration SQL file to the given path |
78
|
|
|
* |
79
|
|
|
* @param string $path The path to write the migration SQL file. |
80
|
|
|
* @param string $to The version to migrate to. |
81
|
|
|
* |
82
|
|
|
* @return boolean $written |
83
|
|
|
*/ |
84
|
6 |
|
public function writeSqlFile($path, $to = null) |
85
|
|
|
{ |
86
|
6 |
|
$sql = $this->getSql($to); |
87
|
|
|
|
88
|
6 |
|
$from = $this->configuration->getCurrentVersion(); |
89
|
6 |
|
if ($to === null) { |
90
|
1 |
|
$to = $this->configuration->getLatestVersion(); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
6 |
|
$direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP; |
94
|
|
|
|
95
|
6 |
|
$this->outputWriter->write(sprintf("-- Migrating from %s to %s\n", $from, $to)); |
96
|
|
|
|
97
|
6 |
|
$sqlWriter = new SqlFileWriter( |
98
|
6 |
|
$this->configuration->getMigrationsColumnName(), |
99
|
6 |
|
$this->configuration->getMigrationsTableName(), |
100
|
6 |
|
$path, |
101
|
6 |
|
$this->outputWriter |
102
|
6 |
|
); |
103
|
|
|
|
104
|
6 |
|
return $sqlWriter->write($sql, $direction); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param boolean $noMigrationException Throw an exception or not if no migration is found. Mostly for Continuous Integration. |
109
|
|
|
*/ |
110
|
2 |
|
public function setNoMigrationException($noMigrationException = false) |
111
|
|
|
{ |
112
|
2 |
|
$this->noMigrationException = $noMigrationException; |
113
|
2 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Run a migration to the current version or the given target version. |
117
|
|
|
* |
118
|
|
|
* @param string $to The version to migrate to. |
119
|
|
|
* @param boolean $dryRun Whether or not to make this a dry run and not execute anything. |
120
|
|
|
* @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
121
|
|
|
* @param callable|null $confirm A callback to confirm whether the migrations should be executed. |
122
|
|
|
* |
123
|
|
|
* @return array An array of migration sql statements. This will be empty if the the $confirm callback declines to execute the migration |
124
|
|
|
* |
125
|
|
|
* @throws MigrationException |
126
|
|
|
*/ |
127
|
28 |
|
public function migrate($to = null, $dryRun = false, $timeAllQueries = false, callable $confirm = null) |
128
|
|
|
{ |
129
|
|
|
/** |
130
|
|
|
* If no version to migrate to is given we default to the last available one. |
131
|
|
|
*/ |
132
|
28 |
|
if ($to === null) { |
133
|
11 |
|
$to = $this->configuration->getLatestVersion(); |
134
|
11 |
|
} |
135
|
|
|
|
136
|
28 |
|
$from = (string) $this->configuration->getCurrentVersion(); |
137
|
28 |
|
$to = (string) $to; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Throw an error if we can't find the migration to migrate to in the registered |
141
|
|
|
* migrations. |
142
|
|
|
*/ |
143
|
28 |
|
$migrations = $this->configuration->getMigrations(); |
144
|
28 |
|
if (!isset($migrations[$to]) && $to > 0) { |
145
|
1 |
|
throw MigrationException::unknownMigrationVersion($to); |
146
|
|
|
} |
147
|
|
|
|
148
|
27 |
|
$direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP; |
149
|
27 |
|
$migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to); |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* If |
153
|
|
|
* there are no migrations to execute |
154
|
|
|
* and there are migrations, |
155
|
|
|
* and the migration from and to are the same |
156
|
|
|
* means we are already at the destination return an empty array() |
157
|
|
|
* to signify that there is nothing left to do. |
158
|
|
|
*/ |
159
|
27 |
|
if ($from === $to && empty($migrationsToExecute) && !empty($migrations)) { |
160
|
2 |
|
return $this->noMigrations(); |
161
|
|
|
} |
162
|
|
|
|
163
|
26 |
|
if (!$dryRun && false === $this->migrationsCanExecute($confirm)) { |
164
|
1 |
|
return []; |
165
|
|
|
} |
166
|
|
|
|
167
|
25 |
|
$output = $dryRun ? 'Executing dry run of migration' : 'Migrating'; |
168
|
25 |
|
$output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>'; |
169
|
25 |
|
$this->outputWriter->write(sprintf($output, $direction, $to, $from)); |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* If there are no migrations to execute throw an exception. |
173
|
|
|
*/ |
174
|
25 |
|
if (empty($migrationsToExecute) && !$this->noMigrationException) { |
175
|
1 |
|
throw MigrationException::noMigrationsToExecute(); |
176
|
24 |
|
} elseif (empty($migrationsToExecute)) { |
177
|
1 |
|
return $this->noMigrations(); |
178
|
|
|
} |
179
|
|
|
|
180
|
23 |
|
$sql = []; |
181
|
23 |
|
$time = 0; |
182
|
23 |
|
foreach ($migrationsToExecute as $version) { |
183
|
23 |
|
$versionSql = $version->execute($direction, $dryRun, $timeAllQueries); |
184
|
23 |
|
$sql[$version->getVersion()] = $versionSql; |
185
|
23 |
|
$time += $version->getTime(); |
186
|
23 |
|
} |
187
|
|
|
|
188
|
23 |
|
$this->outputWriter->write("\n <comment>------------------------</comment>\n"); |
189
|
23 |
|
$this->outputWriter->write(sprintf(" <info>++</info> finished in %ss", $time)); |
190
|
23 |
|
$this->outputWriter->write(sprintf(" <info>++</info> %s migrations executed", count($migrationsToExecute))); |
191
|
23 |
|
$this->outputWriter->write(sprintf(" <info>++</info> %s sql queries", count($sql, true) - count($sql))); |
192
|
|
|
|
193
|
23 |
|
return $sql; |
194
|
|
|
} |
195
|
|
|
|
196
|
3 |
|
private function noMigrations() |
197
|
|
|
{ |
198
|
3 |
|
$this->outputWriter->write('<comment>No migrations to execute.</comment>'); |
199
|
3 |
|
return []; |
200
|
|
|
} |
201
|
|
|
|
202
|
24 |
|
private function migrationsCanExecute(callable $confirm=null) |
203
|
|
|
{ |
204
|
24 |
|
return null === $confirm ? true : $confirm(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|