|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2014 Matthew Fitzgerald |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace AntiMattr\MongoDB\Migrations; |
|
13
|
|
|
|
|
14
|
|
|
use AntiMattr\MongoDB\Migrations\Configuration\Configuration; |
|
15
|
|
|
use AntiMattr\MongoDB\Migrations\Exception\NoMigrationsToExecuteException; |
|
16
|
|
|
use AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Migration |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The OutputWriter object instance used for outputting information. |
|
25
|
|
|
* |
|
26
|
|
|
* @var OutputWriter |
|
27
|
|
|
*/ |
|
28
|
|
|
private $outputWriter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Configuration |
|
32
|
|
|
*/ |
|
33
|
|
|
private $configuration; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Construct a Migration instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Configuration $configuration A migration Configuration instance |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function __construct(Configuration $configuration) |
|
41
|
|
|
{ |
|
42
|
4 |
|
$this->configuration = $configuration; |
|
43
|
4 |
|
$this->outputWriter = $configuration->getOutputWriter(); |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Run a migration to the current version or the given target version. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $to The version to migrate to |
|
50
|
|
|
* |
|
51
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException |
|
52
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\NoMigrationsToExecuteException |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function migrate($to = null) |
|
55
|
|
|
{ |
|
56
|
4 |
|
if (null === $to) { |
|
57
|
1 |
|
$to = $this->configuration->getLatestVersion(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$from = $this->configuration->getCurrentVersion(); |
|
61
|
4 |
|
$from = (string) $from; |
|
62
|
4 |
|
$to = (string) $to; |
|
63
|
|
|
|
|
64
|
4 |
|
$migrations = $this->configuration->getMigrations(); |
|
65
|
4 |
|
if (!isset($migrations[$to]) && $to > 0) { |
|
66
|
1 |
|
throw new UnknownVersionException($to); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
$direction = $from > $to ? 'down' : 'up'; |
|
70
|
3 |
|
$migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to); |
|
71
|
|
|
|
|
72
|
3 |
|
if ($from === $to && empty($migrationsToExecute) && $migrations) { |
|
73
|
1 |
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$this->outputWriter->write(sprintf('Migrating <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from)); |
|
77
|
|
|
|
|
78
|
2 |
|
if (empty($migrationsToExecute)) { |
|
79
|
1 |
|
throw new NoMigrationsToExecuteException('Could not find any migrations to execute.'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$time = 0; |
|
83
|
1 |
|
foreach ($migrationsToExecute as $version) { |
|
84
|
1 |
|
$version->execute($direction); |
|
85
|
1 |
|
$time += $version->getTime(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$this->outputWriter->write("\n <comment>------------------------</comment>\n"); |
|
89
|
1 |
|
$this->outputWriter->write(sprintf(' <info>++</info> finished in %s', $time)); |
|
90
|
1 |
|
$this->outputWriter->write(sprintf(' <info>++</info> %s migrations executed', count($migrationsToExecute))); |
|
91
|
1 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|