|
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\Exception\AbortException; |
|
15
|
|
|
use AntiMattr\MongoDB\Migrations\Exception\IrreversibleException; |
|
16
|
|
|
use AntiMattr\MongoDB\Migrations\Exception\SkipException; |
|
17
|
|
|
use MongoDB\Collection; |
|
18
|
|
|
use MongoDB\Database; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractMigration |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var \AntiMattr\MongoDB\Migrations\Configuration\Configuration |
|
27
|
|
|
*/ |
|
28
|
|
|
private $configuration; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \AntiMattr\MongoDB\Migrations\OutputWriter |
|
32
|
|
|
*/ |
|
33
|
|
|
private $outputWriter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \AntiMattr\MongoDB\Migrations\Version |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $version; |
|
39
|
|
|
|
|
40
|
2 |
|
public function __construct(Version $version) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->configuration = $version->getConfiguration(); |
|
43
|
2 |
|
$this->outputWriter = $this->configuration->getOutputWriter(); |
|
44
|
2 |
|
$this->version = $version; |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get custom migration description. |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract public function getDescription(); |
|
53
|
|
|
|
|
54
|
|
|
abstract public function up(Database $db); |
|
55
|
|
|
|
|
56
|
|
|
abstract public function down(Database $db); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param \MongoDB\Collection |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function analyze(Collection $collection) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->version->analyze($collection); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function isDryRun(): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->configuration->isDryRun(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param \MongoDB\Database |
|
73
|
|
|
* @param string $filename |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function executeScript(Database $db, $filename) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->version->executeScript($db, $filename); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function write($message) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->outputWriter->write($message); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $message |
|
90
|
|
|
* |
|
91
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\IrreversibleException |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function throwIrreversibleMigrationException($message = null) |
|
94
|
|
|
{ |
|
95
|
|
|
if (null === $message) { |
|
96
|
|
|
$message = 'This migration is irreversible and cannot be reverted.'; |
|
97
|
|
|
} |
|
98
|
|
|
throw new IrreversibleException($message); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Print a warning message if the condition evalutes to TRUE. |
|
103
|
|
|
* |
|
104
|
|
|
* @param bool $condition |
|
105
|
|
|
* @param string $message |
|
106
|
|
|
*/ |
|
107
|
|
|
public function warnIf($condition, $message = '') |
|
108
|
|
|
{ |
|
109
|
|
|
$message = (strlen($message)) ? $message : 'Unknown Reason'; |
|
110
|
|
|
|
|
111
|
|
|
if (true === $condition) { |
|
112
|
|
|
$this->outputWriter->write(' <warning>Warning during ' . $this->version->getExecutionState() . ': ' . $message . '</warning>'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Abort the migration if the condition evalutes to TRUE. |
|
118
|
|
|
* |
|
119
|
|
|
* @param bool $condition |
|
120
|
|
|
* @param string $message |
|
121
|
|
|
* |
|
122
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\AbortException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function abortIf($condition, $message = '') |
|
125
|
|
|
{ |
|
126
|
|
|
$message = (strlen($message)) ? $message : 'Unknown Reason'; |
|
127
|
|
|
|
|
128
|
|
|
if (true === $condition) { |
|
129
|
|
|
throw new AbortException($message); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Skip this migration (but not the next ones) if condition evalutes to TRUE. |
|
135
|
|
|
* |
|
136
|
|
|
* @param bool $condition |
|
137
|
|
|
* @param string $message |
|
138
|
|
|
* |
|
139
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\SkipException |
|
140
|
|
|
*/ |
|
141
|
|
|
public function skipIf($condition, $message = '') |
|
142
|
|
|
{ |
|
143
|
|
|
$message = (strlen($message)) ? $message : 'Unknown Reason'; |
|
144
|
|
|
|
|
145
|
|
|
if (true === $condition) { |
|
146
|
|
|
throw new SkipException($message); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function preUp(Database $db) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function postUp(Database $db) |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function preDown(Database $db) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function postDown(Database $db) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.