|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Phinx\Migration; |
|
9
|
|
|
|
|
10
|
|
|
use Phinx\Db\Adapter\AdapterInterface; |
|
11
|
|
|
use Phinx\Db\Table; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Abstract Migration Class. |
|
18
|
|
|
* |
|
19
|
|
|
* It is expected that the migrations you write extend from this class. |
|
20
|
|
|
* |
|
21
|
|
|
* This abstract class proxies the various database methods to your specified |
|
22
|
|
|
* adapter. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Rob Morgan <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractMigration implements MigrationInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $environment; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $version; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \Phinx\Db\Adapter\AdapterInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $adapter; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $output; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $input; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Whether this migration is being applied or reverted |
|
55
|
|
|
* |
|
56
|
|
|
* @var bool |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $isMigratingUp = true; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* List of all the table objects created by this migration |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $tables = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $environment Environment Detected |
|
69
|
|
|
* @param int $version Migration Version |
|
70
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input |
|
71
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output |
|
72
|
|
|
*/ |
|
73
|
|
|
final public function __construct($environment, $version, InputInterface $input = null, OutputInterface $output = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->environment = $environment; |
|
76
|
|
|
$this->version = $version; |
|
77
|
|
|
|
|
78
|
|
|
if ($input !== null) { |
|
79
|
|
|
$this->setInput($input); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
406 |
|
if ($output !== null) { |
|
83
|
|
|
$this->setOutput($output); |
|
84
|
406 |
|
} |
|
85
|
406 |
|
} |
|
86
|
384 |
|
|
|
87
|
384 |
|
/** |
|
88
|
406 |
|
* @inheritDoc |
|
89
|
384 |
|
*/ |
|
90
|
384 |
|
public function setAdapter(AdapterInterface $adapter) |
|
91
|
|
|
{ |
|
92
|
406 |
|
$this->adapter = $adapter; |
|
93
|
406 |
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritDoc |
|
99
|
|
|
*/ |
|
100
|
406 |
|
public function getAdapter() |
|
101
|
|
|
{ |
|
102
|
406 |
|
return $this->adapter; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
1 |
|
*/ |
|
108
|
|
|
public function setInput(InputInterface $input) |
|
109
|
1 |
|
{ |
|
110
|
|
|
$this->input = $input; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
1 |
|
|
|
115
|
|
|
/** |
|
116
|
1 |
|
* @inheritDoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getInput() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->input; |
|
121
|
21 |
|
} |
|
122
|
|
|
|
|
123
|
21 |
|
/** |
|
124
|
21 |
|
* @inheritDoc |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setOutput(OutputInterface $output) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->output = $output; |
|
129
|
|
|
|
|
130
|
13 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
13 |
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @inheritDoc |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getOutput() |
|
137
|
|
|
{ |
|
138
|
384 |
|
return $this->output; |
|
139
|
|
|
} |
|
140
|
384 |
|
|
|
141
|
384 |
|
/** |
|
142
|
|
|
* @inheritDoc |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getName() |
|
145
|
|
|
{ |
|
146
|
|
|
return static::class; |
|
147
|
2 |
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
/** |
|
150
|
|
|
* @inheritDoc |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getEnvironment() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->environment; |
|
155
|
385 |
|
} |
|
156
|
|
|
|
|
157
|
385 |
|
/** |
|
158
|
385 |
|
* @inheritDoc |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setVersion($version) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->version = $version; |
|
163
|
|
|
|
|
164
|
3 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
3 |
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @inheritDoc |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getVersion() |
|
171
|
|
|
{ |
|
172
|
140 |
|
return $this->version; |
|
173
|
|
|
} |
|
174
|
140 |
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @inheritDoc |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setMigratingUp($isMigratingUp) |
|
179
|
|
|
{ |
|
180
|
1 |
|
$this->isMigratingUp = $isMigratingUp; |
|
181
|
|
|
|
|
182
|
1 |
|
return $this; |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @inheritDoc |
|
187
|
|
|
*/ |
|
188
|
|
|
public function isMigratingUp() |
|
189
|
279 |
|
{ |
|
190
|
|
|
return $this->isMigratingUp; |
|
191
|
279 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @inheritDoc |
|
195
|
|
|
*/ |
|
196
|
|
|
public function execute($sql) |
|
197
|
10 |
|
{ |
|
198
|
|
|
return $this->getAdapter()->execute($sql); |
|
199
|
10 |
|
} |
|
200
|
10 |
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @inheritDoc |
|
203
|
|
|
*/ |
|
204
|
|
|
public function query($sql) |
|
205
|
|
|
{ |
|
206
|
2 |
|
return $this->getAdapter()->query($sql); |
|
207
|
|
|
} |
|
208
|
2 |
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @inheritDoc |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getQueryBuilder() |
|
213
|
|
|
{ |
|
214
|
3 |
|
return $this->getAdapter()->getQueryBuilder(); |
|
215
|
|
|
} |
|
216
|
3 |
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @inheritDoc |
|
219
|
|
|
*/ |
|
220
|
|
|
public function fetchRow($sql) |
|
221
|
|
|
{ |
|
222
|
1 |
|
return $this->getAdapter()->fetchRow($sql); |
|
223
|
|
|
} |
|
224
|
1 |
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @inheritDoc |
|
227
|
|
|
*/ |
|
228
|
|
|
public function fetchAll($sql) |
|
229
|
|
|
{ |
|
230
|
1 |
|
return $this->getAdapter()->fetchAll($sql); |
|
231
|
|
|
} |
|
232
|
1 |
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @inheritDoc |
|
235
|
|
|
*/ |
|
236
|
|
|
public function createDatabase($name, $options) |
|
237
|
|
|
{ |
|
238
|
1 |
|
$this->getAdapter()->createDatabase($name, $options); |
|
239
|
|
|
} |
|
240
|
1 |
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @inheritDoc |
|
243
|
|
|
*/ |
|
244
|
|
|
public function dropDatabase($name) |
|
245
|
|
|
{ |
|
246
|
3 |
|
$this->getAdapter()->dropDatabase($name); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
3 |
|
/** |
|
250
|
2 |
|
* @inheritDoc |
|
251
|
2 |
|
*/ |
|
252
|
3 |
|
public function createSchema($name) |
|
253
|
3 |
|
{ |
|
254
|
|
|
$this->getAdapter()->createSchema($name); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
1 |
|
* @inheritDoc |
|
259
|
|
|
*/ |
|
260
|
1 |
|
public function dropSchema($name) |
|
261
|
1 |
|
{ |
|
262
|
|
|
$this->getAdapter()->dropSchema($name); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
1 |
|
* @inheritDoc |
|
267
|
|
|
*/ |
|
268
|
1 |
|
public function hasTable($tableName) |
|
269
|
1 |
|
{ |
|
270
|
|
|
return $this->getAdapter()->hasTable($tableName); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
1 |
|
* @inheritDoc |
|
275
|
|
|
*/ |
|
276
|
1 |
|
public function table($tableName, $options = []) |
|
277
|
|
|
{ |
|
278
|
|
|
$table = new Table($tableName, $options, $this->getAdapter()); |
|
279
|
|
|
$this->tables[] = $table; |
|
280
|
|
|
|
|
281
|
|
|
return $table; |
|
282
|
5 |
|
} |
|
283
|
|
|
|
|
284
|
5 |
|
/** |
|
285
|
|
|
* A short-hand method to drop the given database table. |
|
286
|
|
|
* |
|
287
|
|
|
* @deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $tableName Table name |
|
290
|
|
|
* |
|
291
|
|
|
* @return void |
|
292
|
|
|
*/ |
|
293
|
1 |
|
public function dropTable($tableName) |
|
294
|
|
|
{ |
|
295
|
1 |
|
trigger_error('dropTable() is deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.', E_USER_DEPRECATED); |
|
296
|
1 |
|
$this->table($tableName)->drop()->save(); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Perform checks on the migration, print a warning |
|
301
|
|
|
* if there are potential problems. |
|
302
|
|
|
* |
|
303
|
|
|
* Right now, the only check is if there is both a `change()` and |
|
304
|
|
|
* an `up()` or a `down()` method. |
|
305
|
|
|
* |
|
306
|
|
|
* @return void |
|
307
|
|
|
*/ |
|
308
|
|
|
public function preFlightCheck() |
|
309
|
|
|
{ |
|
310
|
|
|
if (method_exists($this, MigrationInterface::CHANGE)) { |
|
311
|
|
|
if ( |
|
312
|
|
|
method_exists($this, MigrationInterface::UP) || |
|
313
|
|
|
method_exists($this, MigrationInterface::DOWN) |
|
314
|
|
|
) { |
|
315
|
|
|
$this->output->writeln(sprintf( |
|
316
|
|
|
'<comment>warning</comment> Migration contains both change() and up()/down() methods. <options=bold>Ignoring up() and down()</>.' |
|
317
|
|
|
)); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Perform checks on the migration after completion |
|
324
|
|
|
* |
|
325
|
|
|
* Right now, the only check is whether all changes were committed |
|
326
|
|
|
* |
|
327
|
|
|
* @throws \RuntimeException |
|
328
|
|
|
* |
|
329
|
|
|
* @return void |
|
330
|
|
|
*/ |
|
331
|
|
|
public function postFlightCheck() |
|
332
|
|
|
{ |
|
333
|
|
|
foreach ($this->tables as $table) { |
|
334
|
|
|
if ($table->hasPendingActions()) { |
|
335
|
|
|
throw new RuntimeException('Migration has pending actions after execution!'); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Checks to see if the migration should be executed. |
|
342
|
|
|
* |
|
343
|
|
|
* Returns true by default. |
|
344
|
|
|
* |
|
345
|
|
|
* You can use this to prevent a migration from executing. |
|
346
|
|
|
* |
|
347
|
|
|
* @return bool |
|
348
|
|
|
*/ |
|
349
|
|
|
public function shouldExecute() |
|
350
|
|
|
{ |
|
351
|
|
|
return true; |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|