1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package Phinx |
27
|
|
|
* @subpackage Phinx\Migration |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Migration; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Adapter\AdapterInterface; |
32
|
|
|
use Phinx\Db\Table; |
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Abstract Migration Class. |
38
|
|
|
* |
39
|
|
|
* It is expected that the migrations you write extend from this class. |
40
|
|
|
* |
41
|
|
|
* This abstract class proxies the various database methods to your specified |
42
|
|
|
* adapter. |
43
|
|
|
* |
44
|
|
|
* @author Rob Morgan <[email protected]> |
45
|
|
|
*/ |
46
|
|
|
abstract class AbstractMigration implements MigrationInterface |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $environment; |
52
|
|
|
/** |
53
|
|
|
* @var float |
54
|
|
|
*/ |
55
|
|
|
protected $version; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \Phinx\Db\Adapter\AdapterInterface |
59
|
|
|
*/ |
60
|
|
|
protected $adapter; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
64
|
|
|
*/ |
65
|
|
|
protected $output; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
69
|
|
|
*/ |
70
|
|
|
protected $input; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Whether this migration is being applied or reverted |
74
|
|
|
* |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
protected $isMigratingUp = true; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Class Constructor. |
81
|
|
|
* |
82
|
406 |
|
* @param string $environment Environment Detected |
83
|
|
|
* @param int $version Migration Version |
84
|
406 |
|
* @param \Symfony\Component\Console\Input\InputInterface|null $input |
85
|
406 |
|
* @param \Symfony\Component\Console\Output\OutputInterface|null $output |
86
|
384 |
|
*/ |
87
|
384 |
|
final public function __construct($environment, $version, InputInterface $input = null, OutputInterface $output = null) |
88
|
406 |
|
{ |
89
|
384 |
|
$this->environment = $environment; |
90
|
384 |
|
$this->version = $version; |
|
|
|
|
91
|
|
|
|
92
|
406 |
|
if (!is_null($input)) { |
93
|
406 |
|
$this->setInput($input); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!is_null($output)) { |
97
|
|
|
$this->setOutput($output); |
98
|
|
|
} |
99
|
|
|
|
100
|
406 |
|
$this->init(); |
101
|
|
|
} |
102
|
406 |
|
|
103
|
|
|
/** |
104
|
|
|
* Initialize method. |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
1 |
|
*/ |
108
|
|
|
protected function init() |
109
|
1 |
|
{ |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
1 |
|
*/ |
115
|
|
|
public function up() |
116
|
1 |
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
21 |
|
*/ |
122
|
|
|
public function down() |
123
|
21 |
|
{ |
124
|
21 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function setAdapter(AdapterInterface $adapter) |
130
|
13 |
|
{ |
131
|
|
|
$this->adapter = $adapter; |
132
|
13 |
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
384 |
|
*/ |
139
|
|
|
public function getAdapter() |
140
|
384 |
|
{ |
141
|
384 |
|
return $this->adapter; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
2 |
|
public function setInput(InputInterface $input) |
148
|
|
|
{ |
149
|
2 |
|
$this->input = $input; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
385 |
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
385 |
|
public function getInput() |
158
|
385 |
|
{ |
159
|
|
|
return $this->input; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
3 |
|
*/ |
165
|
|
|
public function setOutput(OutputInterface $output) |
166
|
3 |
|
{ |
167
|
|
|
$this->output = $output; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
140 |
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
140 |
|
*/ |
175
|
|
|
public function getOutput() |
176
|
|
|
{ |
177
|
|
|
return $this->output; |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
1 |
|
*/ |
183
|
1 |
|
public function getName() |
184
|
|
|
{ |
185
|
|
|
return get_class($this); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
279 |
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
279 |
|
public function getEnvironment() |
192
|
|
|
{ |
193
|
|
|
return $this->environment; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
10 |
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
10 |
|
public function setVersion($version) |
200
|
10 |
|
{ |
201
|
|
|
$this->version = $version; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
2 |
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
2 |
|
*/ |
209
|
|
|
public function getVersion() |
210
|
|
|
{ |
211
|
|
|
return $this->version; |
212
|
|
|
} |
213
|
|
|
|
214
|
3 |
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
3 |
|
*/ |
217
|
|
|
public function setMigratingUp($isMigratingUp) |
218
|
|
|
{ |
219
|
|
|
$this->isMigratingUp = $isMigratingUp; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
1 |
|
} |
223
|
|
|
|
224
|
1 |
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function isMigratingUp() |
228
|
|
|
{ |
229
|
|
|
return $this->isMigratingUp; |
230
|
1 |
|
} |
231
|
|
|
|
232
|
1 |
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
|
|
public function execute($sql) |
236
|
|
|
{ |
237
|
|
|
return $this->getAdapter()->execute($sql); |
238
|
1 |
|
} |
239
|
|
|
|
240
|
1 |
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
|
|
public function query($sql) |
244
|
|
|
{ |
245
|
|
|
return $this->getAdapter()->query($sql); |
246
|
3 |
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
3 |
|
* {@inheritdoc} |
250
|
2 |
|
*/ |
251
|
2 |
|
public function getQueryBuilder() |
252
|
3 |
|
{ |
253
|
3 |
|
return $this->getAdapter()->getQueryBuilder(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
1 |
|
*/ |
259
|
|
|
public function fetchRow($sql) |
260
|
1 |
|
{ |
261
|
1 |
|
return $this->getAdapter()->fetchRow($sql); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
1 |
|
*/ |
267
|
|
|
public function fetchAll($sql) |
268
|
1 |
|
{ |
269
|
1 |
|
return $this->getAdapter()->fetchAll($sql); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritdoc} |
274
|
1 |
|
*/ |
275
|
|
|
public function insert($table, $data) |
276
|
1 |
|
{ |
277
|
|
|
trigger_error('insert() is deprecated since 0.10.0. Use $this->table($tableName)->insert($data)->save() instead.', E_USER_DEPRECATED); |
278
|
|
|
// convert to table object |
279
|
|
|
if (is_string($table)) { |
280
|
|
|
$table = new Table($table, [], $this->getAdapter()); |
281
|
|
|
} |
282
|
5 |
|
$table->insert($data)->save(); |
283
|
|
|
} |
284
|
5 |
|
|
285
|
|
|
/** |
286
|
|
|
* {@inheritdoc} |
287
|
|
|
*/ |
288
|
|
|
public function createDatabase($name, $options) |
289
|
|
|
{ |
290
|
|
|
$this->getAdapter()->createDatabase($name, $options); |
291
|
|
|
} |
292
|
|
|
|
293
|
1 |
|
/** |
294
|
|
|
* {@inheritdoc} |
295
|
1 |
|
*/ |
296
|
1 |
|
public function dropDatabase($name) |
297
|
|
|
{ |
298
|
|
|
$this->getAdapter()->dropDatabase($name); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
|
|
public function hasTable($tableName) |
305
|
|
|
{ |
306
|
|
|
return $this->getAdapter()->hasTable($tableName); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
|
|
public function table($tableName, $options = []) |
313
|
|
|
{ |
314
|
|
|
return new Table($tableName, $options, $this->getAdapter()); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* A short-hand method to drop the given database table. |
319
|
|
|
* |
320
|
|
|
* @deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead. |
321
|
|
|
* @param string $tableName Table Name |
322
|
|
|
* @return void |
323
|
|
|
*/ |
324
|
|
|
public function dropTable($tableName) |
325
|
|
|
{ |
326
|
|
|
trigger_error('dropTable() is deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.', E_USER_DEPRECATED); |
327
|
|
|
$this->table($tableName)->drop()->save(); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Perform checks on the migration, print a warning |
332
|
|
|
* if there are potential problems. |
333
|
|
|
* |
334
|
|
|
* Right now, the only check is if there is both a `change()` and |
335
|
|
|
* an `up()` or a `down()` method. |
336
|
|
|
* |
337
|
|
|
* @return void |
338
|
|
|
*/ |
339
|
|
|
public function preFlightCheck() |
340
|
|
|
{ |
341
|
|
|
if (method_exists($this, MigrationInterface::CHANGE)) { |
342
|
|
|
if (method_exists($this, MigrationInterface::UP) || |
343
|
|
|
method_exists($this, MigrationInterface::DOWN) ) { |
344
|
|
|
$this->output->writeln(sprintf( |
345
|
|
|
'<comment>warning</comment> Migration contains both change() and/or up()/down() methods. <options=bold>Ignoring up() and down()</>.' |
346
|
|
|
)); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.