Completed
Push — master ( dcad7a...5df0c5 )
by Mike
02:49
created

Version::getMigration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Types\Type;
23
use Doctrine\DBAL\Migrations\Configuration\Configuration;
24
use Doctrine\DBAL\Migrations\Provider\LazySchemaDiffProvider;
25
use Doctrine\DBAL\Migrations\Provider\SchemaDiffProvider;
26
use Doctrine\DBAL\Migrations\Provider\SchemaDiffProviderInterface;
27
28
/**
29
 * Class which wraps a migration version and allows execution of the
30
 * individual migration version up or down method.
31
 *
32
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
33
 * @link        www.doctrine-project.org
34
 * @since       2.0
35
 * @author      Jonathan H. Wage <[email protected]>
36
 */
37
class Version
38
{
39
    const STATE_NONE = 0;
40
    const STATE_PRE  = 1;
41
    const STATE_EXEC = 2;
42
    const STATE_POST = 3;
43
44
    const DIRECTION_UP = 'up';
45
    const DIRECTION_DOWN = 'down';
46
47
    /**
48
     * The Migrations Configuration instance for this migration
49
     *
50
     * @var Configuration
51
     */
52
    private $configuration;
53
54
    /**
55
     * The OutputWriter object instance used for outputting information
56
     *
57
     * @var OutputWriter
58
     */
59
    private $outputWriter;
60
61
    /**
62
     * The version in timestamp format (YYYYMMDDHHMMSS)
63
     *
64
     * @param int
65
     */
66
    private $version;
67
68
    /**
69
     * The migration instance for this version
70
     *
71
     * @var AbstractMigration
72
     */
73
    private $migration;
74
75
    /**
76
     * @var \Doctrine\DBAL\Connection
77
     */
78
    private $connection;
79
80
    /**
81
     * @var string
82
     */
83
    private $class;
84
85
    /** The array of collected SQL statements for this version */
86
    private $sql = [];
87
88
    /** The array of collected parameters for SQL statements for this version */
89
    private $params = [];
90
91
    /** The array of collected types for SQL statements for this version */
92
    private $types = [];
93
94
    /** The time in seconds that this migration version took to execute */
95
    private $time;
96
97
    /**
98
     * @var int
99
     */
100
    private $state = self::STATE_NONE;
101
102
    /** @var SchemaDiffProviderInterface */
103
    private $schemaProvider;
104
105 106
    public function __construct(Configuration $configuration, $version, $class, SchemaDiffProviderInterface $schemaProvider=null)
106
    {
107 106
        $this->configuration = $configuration;
108 106
        $this->outputWriter = $configuration->getOutputWriter();
109 106
        $this->class = $class;
110 106
        $this->connection = $configuration->getConnection();
111 106
        $this->migration = new $class($this);
112 106
        $this->version = $version;
113
114 106
        if ($schemaProvider !== null) {
115 1
            $this->schemaProvider = $schemaProvider;
116 1
        }
117 106
        if($schemaProvider === null) {
118 105
            $schemaProvider = new SchemaDiffProvider($this->connection->getSchemaManager(),
119 105
                $this->connection->getDatabasePlatform());
120 105
            $this->schemaProvider = LazySchemaDiffProvider::fromDefaultProxyFacyoryConfiguration($schemaProvider);
121 105
        }
122 106
    }
123
124
    /**
125
     * Returns the string version in the format YYYYMMDDHHMMSS
126
     *
127
     * @return string $version
128
     */
129 66
    public function getVersion()
130
    {
131 66
        return $this->version;
132
    }
133
134
    /**
135
     * Returns the Migrations Configuration object instance
136
     *
137
     * @return Configuration $configuration
138
     */
139 102
    public function getConfiguration()
140
    {
141 102
        return $this->configuration;
142
    }
143
144
    /**
145
     * Check if this version has been migrated or not.
146
     *
147
     * @return boolean
148
     */
149 15
    public function isMigrated()
150
    {
151 15
        return $this->configuration->hasVersionMigrated($this);
152
    }
153
154 36
    public function markMigrated()
155
    {
156 36
        $this->markVersion('up');
157 36
    }
158
159 36
    private function markVersion($direction)
160
    {
161 36
        $action = $direction === 'up' ? 'insert' : 'delete';
162
163 36
        $this->configuration->createMigrationTable();
164 36
        $this->connection->$action(
165 36
            $this->configuration->getMigrationsTableName(),
166 36
            [$this->configuration->getMigrationsColumnName() => $this->version]
167 36
        );
168 36
    }
169
170 9
    public function markNotMigrated()
171
    {
172 9
        $this->markVersion('down');
173 9
    }
174
175
    /**
176
     * Add some SQL queries to this versions migration
177
     *
178
     * @param array|string $sql
179
     * @param array        $params
180
     * @param array        $types
181
     */
182 40
    public function addSql($sql, array $params = [], array $types = [])
183
    {
184 40
        if (is_array($sql)) {
185 39
            foreach ($sql as $key => $query) {
186 13
                $this->sql[] = $query;
187 13
                if (!empty($params[$key])) {
188 1
                    $queryTypes = isset($types[$key]) ? $types[$key] : [];
189 1
                    $this->addQueryParams($params[$key], $queryTypes);
190 1
                }
191 39
            }
192 39
        } else {
193 22
            $this->sql[] = $sql;
194 22
            if (!empty($params)) {
195 12
                $this->addQueryParams($params, $types);
196 12
            }
197
        }
198 40
    }
199
200
    /**
201
     * @param mixed[] $params Array of prepared statement parameters
202
     * @param string[] $types Array of the types of each statement parameters
203
     */
204 13
    private function addQueryParams($params, $types)
205
    {
206 13
        $index = count($this->sql) - 1;
207 13
        $this->params[$index] = $params;
208 13
        $this->types[$index] = $types;
209 13
    }
210
211
    /**
212
     * Write a migration SQL file to the given path
213
     *
214
     * @param string $path      The path to write the migration SQL file.
215
     * @param string $direction The direction to execute.
216
     *
217
     * @return boolean $written
218
     */
219 9
    public function writeSqlFile($path, $direction = self::DIRECTION_UP)
220
    {
221 9
        $queries = $this->execute($direction, true);
222
223 9
        if ( ! empty($this->params)) {
224 1
            throw MigrationException::migrationNotConvertibleToSql($this->class);
225
        }
226
227 8
        $this->outputWriter->write("\n-- Version " . $this->version . "\n");
228
229 8
        $sqlQueries = [$this->version => $queries];
230 8
        $sqlWriter = new SqlFileWriter(
231 8
            $this->configuration->getMigrationsColumnName(),
232 8
            $this->configuration->getMigrationsTableName(),
233 8
            $path,
234 8
            $this->outputWriter
235 8
        );
236
237 8
        return $sqlWriter->write($sqlQueries, $direction);
238
    }
239
240
    /**
241
     * @return AbstractMigration
242
     */
243 4
    public function getMigration()
244
    {
245 4
        return $this->migration;
246
    }
247
248
    /**
249
     * Execute this migration version up or down and and return the SQL.
250
     * We are only allowing the addSql call and the schema modification to take effect in the up and down call.
251
     * This is necessary to ensure that the migration is revertable.
252
     * The schema is passed to the pre and post method only to be able to test the presence of some table, And the
253
     * connection that can get used trough it allow for the test of the presence of records.
254
     *
255
     * @param string  $direction      The direction to execute the migration.
256
     * @param boolean $dryRun         Whether to not actually execute the migration SQL and just do a dry run.
257
     * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query.
258
     *
259
     * @return array $sql
260
     *
261
     * @throws \Exception when migration fails
262
     */
263 40
    public function execute($direction, $dryRun = false, $timeAllQueries = false)
264
    {
265 40
        $this->sql = [];
266
267 40
        $transaction = $this->migration->isTransactional();
268 40
        if ($transaction) {
269
            //only start transaction if in transactional mode
270 40
            $this->connection->beginTransaction();
271 40
        }
272
273
        try {
274 40
            $migrationStart = microtime(true);
275
276 40
            $this->state = self::STATE_PRE;
277 40
            $fromSchema = $this->schemaProvider->createFromSchema();
278
279 40
            $this->migration->{'pre' . ucfirst($direction)}($fromSchema);
280
281 39
            if ($direction === self::DIRECTION_UP) {
282 37
                $this->outputWriter->write("\n" . sprintf('  <info>++</info> migrating <comment>%s</comment>', $this->version) . "\n");
283 37
            } else {
284 8
                $this->outputWriter->write("\n" . sprintf('  <info>--</info> reverting <comment>%s</comment>', $this->version) . "\n");
285
            }
286
287 39
            $this->state = self::STATE_EXEC;
288
289 39
            $toSchema = $this->schemaProvider->createToSchema($fromSchema);
290 39
            $this->migration->$direction($toSchema);
291
292 38
            $this->addSql($this->schemaProvider->getSqlDiffToMigrate($fromSchema, $toSchema));
293
294 38
            $this->executeRegisteredSql($dryRun, $timeAllQueries);
295
296 38
            $this->state = self::STATE_POST;
297 38
            $this->migration->{'post' . ucfirst($direction)}($toSchema);
298
299 38
            if (! $dryRun) {
300 27
                if ($direction === self::DIRECTION_UP) {
301 27
                    $this->markMigrated();
302 27
                } else {
303 6
                    $this->markNotMigrated();
304
                }
305 27
            }
306
307 38
            $migrationEnd = microtime(true);
308 38
            $this->time = round($migrationEnd - $migrationStart, 2);
309 38
            if ($direction === self::DIRECTION_UP) {
310 36
                $this->outputWriter->write(sprintf("\n  <info>++</info> migrated (%ss)", $this->time));
311 36
            } else {
312 8
                $this->outputWriter->write(sprintf("\n  <info>--</info> reverted (%ss)", $this->time));
313
            }
314
315 38
            if ($transaction) {
316
                //commit only if running in transactional mode
317 38
                $this->connection->commit();
318 38
            }
319
320 38
            $this->state = self::STATE_NONE;
321
322 38
            return $this->sql;
323 6
        } catch (SkipMigrationException $e) {
324 5
            if ($transaction) {
325
                //only rollback transaction if in transactional mode
326 5
                $this->connection->rollBack();
327 5
            }
328
329 5
            if ($dryRun === false) {
330
                // now mark it as migrated
331 4
                if ($direction === self::DIRECTION_UP) {
332 4
                    $this->markMigrated();
333 4
                } else {
334 1
                    $this->markNotMigrated();
335
                }
336 4
            }
337
338 5
            $this->outputWriter->write(sprintf("\n  <info>SS</info> skipped (Reason: %s)",  $e->getMessage()));
339
340 5
            $this->state = self::STATE_NONE;
341
342 5
            return [];
343 1
        } catch (\Exception $e) {
344
345 1
            $this->outputWriter->write(sprintf(
346 1
                '<error>Migration %s failed during %s. Error %s</error>',
347 1
                $this->version, $this->getExecutionState(), $e->getMessage()
348 1
            ));
349
350 1
            if ($transaction) {
351
                //only rollback transaction if in transactional mode
352 1
                $this->connection->rollBack();
353 1
            }
354
355 1
            $this->state = self::STATE_NONE;
356 1
            throw $e;
357
        }
358
    }
359
360 8
    public function getExecutionState()
361
    {
362 8
        switch ($this->state) {
363 8
            case self::STATE_PRE:
364 1
                return 'Pre-Checks';
365 7
            case self::STATE_POST:
366 1
                return 'Post-Checks';
367 6
            case self::STATE_EXEC:
368 2
                return 'Execution';
369 4
            default:
370 4
                return 'No State';
371 4
        }
372
    }
373
374 18
    private function outputQueryTime($queryStart, $timeAllQueries = false)
375
    {
376 18
        if ($timeAllQueries !== false) {
377 1
            $queryEnd = microtime(true);
378 1
            $queryTime = round($queryEnd - $queryStart, 4);
379
380 1
            $this->outputWriter->write(sprintf("  <info>%ss</info>", $queryTime));
381 1
        }
382 18
    }
383
384
    /**
385
     * Returns the time this migration version took to execute
386
     *
387
     * @return integer $time The time this migration version took to execute
388
     */
389 22
    public function getTime()
390
    {
391 22
        return $this->time;
392
    }
393
394 2
    public function __toString()
395
    {
396 2
        return $this->version;
397
    }
398
399 38
    private function executeRegisteredSql($dryRun = false, $timeAllQueries = false)
400
    {
401 38
        if (! $dryRun) {
402 27
            if (!empty($this->sql)) {
403 16
                foreach ($this->sql as $key => $query) {
404 16
                    $queryStart = microtime(true);
405
406 16
                    if ( ! isset($this->params[$key])) {
407 15
                        $this->outputWriter->write('     <comment>-></comment> ' . $query);
408 15
                        $this->connection->executeQuery($query);
409 15
                    } else {
410 7
                        $this->outputWriter->write(sprintf('    <comment>-</comment> %s (with parameters)', $query));
411 7
                        $this->connection->executeQuery($query, $this->params[$key], $this->types[$key]);
412
                    }
413
414 16
                    $this->outputQueryTime($queryStart, $timeAllQueries);
415 16
                }
416 16
            } else {
417 11
                $this->outputWriter->write(sprintf(
418 11
                    '<error>Migration %s was executed but did not result in any SQL statements.</error>',
419 11
                    $this->version
420 11
                ));
421
            }
422 27
        } else {
423 11
            foreach ($this->sql as $idx => $query) {
424 11
                $this->outputSqlQuery($idx, $query);
425 11
            }
426
        }
427 38
    }
428
429
    /**
430
     * Outputs a SQL query via the `OutputWriter`.
431
     *
432
     * @param int $idx The SQL query index. Used to look up params.
433
     * @param string $query the query to output
434
     * @return void
435
     */
436 11
    private function outputSqlQuery($idx, $query)
437
    {
438 11
        $params = $this->formatParamsForOutput(
439 11
            isset($this->params[$idx]) ? $this->params[$idx] : [],
440 11
            isset($this->types[$idx]) ? $this->types[$idx] : []
441 11
        );
442
443 11
        $this->outputWriter->write(rtrim(sprintf(
444 11
            '     <comment>-></comment> %s %s',
445 11
            $query,
446
            $params
447 11
        )));
448 11
    }
449
450
    /**
451
     * Formats a set of sql parameters for output with dry run.
452
     *
453
     * @param $params The query parameters
454
     * @param $types The types of the query params. Default type is a string
455
     * @return string|null a string of the parameters present.
456
     */
457 11
    private function formatParamsForOutput(array $params, array $types)
458
    {
459 11
        if (empty($params)) {
460 7
            return '';
461
        }
462
463 5
        $platform = $this->connection->getDatabasePlatform();
464 5
        $out = [];
465 5
        foreach ($params as $key => $value) {
466 5
            $type = isset($types[$key]) ? $types[$key] : 'string';
467 5
            $outval = Type::getType($type)->convertToDatabaseValue($value, $platform);
468 5
            $out[] = is_string($key) ? sprintf(':%s => %s', $key, $outval) : $outval;
469 5
        }
470
471 5
        return sprintf('with parameters (%s)', implode(', ', $out));
472
    }
473
}
474