1 | <?php |
||
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) |
|
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) |
|
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 | } |
||
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 | $path, |
||
101 | 6 | $this->outputWriter |
|
102 | ); |
||
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) |
|
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 | } |
||
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 | } 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 | } |
||
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() |
|
201 | |||
202 | 24 | private function migrationsCanExecute(callable $confirm=null) |
|
206 | } |
||
207 |