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
|
|
|
/** |
67
|
|
|
* @param \MongoDB\Database |
68
|
|
|
* @param string $filename |
69
|
|
|
*/ |
70
|
|
|
protected function executeScript(Database $db, $filename) |
71
|
|
|
{ |
72
|
|
|
return $this->version->executeScript($db, $filename); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string |
77
|
|
|
*/ |
78
|
|
|
protected function write($message) |
79
|
|
|
{ |
80
|
|
|
$this->outputWriter->write($message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $message |
85
|
|
|
* |
86
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\IrreversibleException |
87
|
|
|
*/ |
88
|
|
|
protected function throwIrreversibleMigrationException($message = null) |
89
|
|
|
{ |
90
|
|
|
if (null === $message) { |
91
|
|
|
$message = 'This migration is irreversible and cannot be reverted.'; |
92
|
|
|
} |
93
|
|
|
throw new IrreversibleException($message); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Print a warning message if the condition evalutes to TRUE. |
98
|
|
|
* |
99
|
|
|
* @param bool $condition |
100
|
|
|
* @param string $message |
101
|
|
|
*/ |
102
|
|
|
public function warnIf($condition, $message = '') |
103
|
|
|
{ |
104
|
|
|
$message = (strlen($message)) ? $message : 'Unknown Reason'; |
105
|
|
|
|
106
|
|
|
if (true === $condition) { |
107
|
|
|
$this->outputWriter->write(' <warning>Warning during ' . $this->version->getExecutionState() . ': ' . $message . '</warning>'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Abort the migration if the condition evalutes to TRUE. |
113
|
|
|
* |
114
|
|
|
* @param bool $condition |
115
|
|
|
* @param string $message |
116
|
|
|
* |
117
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\AbortException |
118
|
|
|
*/ |
119
|
|
|
public function abortIf($condition, $message = '') |
120
|
|
|
{ |
121
|
|
|
$message = (strlen($message)) ? $message : 'Unknown Reason'; |
122
|
|
|
|
123
|
|
|
if (true === $condition) { |
124
|
|
|
throw new AbortException($message); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Skip this migration (but not the next ones) if condition evalutes to TRUE. |
130
|
|
|
* |
131
|
|
|
* @param bool $condition |
132
|
|
|
* @param string $message |
133
|
|
|
* |
134
|
|
|
* @throws AntiMattr\MongoDB\Migrations\Exception\SkipException |
135
|
|
|
*/ |
136
|
|
|
public function skipIf($condition, $message = '') |
137
|
|
|
{ |
138
|
|
|
$message = (strlen($message)) ? $message : 'Unknown Reason'; |
139
|
|
|
|
140
|
|
|
if (true === $condition) { |
141
|
|
|
throw new SkipException($message); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function preUp(Database $db) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function postUp(Database $db) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function preDown(Database $db) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function postDown(Database $db) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.