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