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 setAdapter(AdapterInterface $adapter) |
116
|
1 |
|
{ |
117
|
|
|
$this->adapter = $adapter; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
21 |
|
|
122
|
|
|
/** |
123
|
21 |
|
* {@inheritdoc} |
124
|
21 |
|
*/ |
125
|
|
|
public function getAdapter() |
126
|
|
|
{ |
127
|
|
|
return $this->adapter; |
128
|
|
|
} |
129
|
|
|
|
130
|
13 |
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
13 |
|
*/ |
133
|
|
|
public function setInput(InputInterface $input) |
134
|
|
|
{ |
135
|
|
|
$this->input = $input; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
384 |
|
} |
139
|
|
|
|
140
|
384 |
|
/** |
141
|
384 |
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function getInput() |
144
|
|
|
{ |
145
|
|
|
return $this->input; |
146
|
|
|
} |
147
|
2 |
|
|
148
|
|
|
/** |
149
|
2 |
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function setOutput(OutputInterface $output) |
152
|
|
|
{ |
153
|
|
|
$this->output = $output; |
154
|
|
|
|
155
|
385 |
|
return $this; |
156
|
|
|
} |
157
|
385 |
|
|
158
|
385 |
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function getOutput() |
162
|
|
|
{ |
163
|
|
|
return $this->output; |
164
|
3 |
|
} |
165
|
|
|
|
166
|
3 |
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function getName() |
170
|
|
|
{ |
171
|
|
|
return get_class($this); |
172
|
140 |
|
} |
173
|
|
|
|
174
|
140 |
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
|
|
public function getEnvironment() |
178
|
|
|
{ |
179
|
|
|
return $this->environment; |
180
|
1 |
|
} |
181
|
|
|
|
182
|
1 |
|
/** |
183
|
1 |
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function setVersion($version) |
186
|
|
|
{ |
187
|
|
|
$this->version = $version; |
188
|
|
|
|
189
|
279 |
|
return $this; |
190
|
|
|
} |
191
|
279 |
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function getVersion() |
196
|
|
|
{ |
197
|
10 |
|
return $this->version; |
198
|
|
|
} |
199
|
10 |
|
|
200
|
10 |
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function setMigratingUp($isMigratingUp) |
204
|
|
|
{ |
205
|
|
|
$this->isMigratingUp = $isMigratingUp; |
206
|
2 |
|
|
207
|
|
|
return $this; |
208
|
2 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
|
|
public function isMigratingUp() |
214
|
3 |
|
{ |
215
|
|
|
return $this->isMigratingUp; |
216
|
3 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
|
|
public function execute($sql) |
222
|
1 |
|
{ |
223
|
|
|
return $this->getAdapter()->execute($sql); |
224
|
1 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
|
|
public function query($sql) |
230
|
1 |
|
{ |
231
|
|
|
return $this->getAdapter()->query($sql); |
232
|
1 |
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
|
|
public function getQueryBuilder() |
238
|
1 |
|
{ |
239
|
|
|
return $this->getAdapter()->getQueryBuilder(); |
240
|
1 |
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
|
|
public function fetchRow($sql) |
246
|
3 |
|
{ |
247
|
|
|
return $this->getAdapter()->fetchRow($sql); |
248
|
|
|
} |
249
|
3 |
|
|
250
|
2 |
|
/** |
251
|
2 |
|
* {@inheritdoc} |
252
|
3 |
|
*/ |
253
|
3 |
|
public function fetchAll($sql) |
254
|
|
|
{ |
255
|
|
|
return $this->getAdapter()->fetchAll($sql); |
256
|
|
|
} |
257
|
|
|
|
258
|
1 |
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
1 |
|
*/ |
261
|
1 |
|
public function insert($table, $data) |
262
|
|
|
{ |
263
|
|
|
trigger_error('insert() is deprecated since 0.10.0. Use $this->table($tableName)->insert($data)->save() instead.', E_USER_DEPRECATED); |
264
|
|
|
// convert to table object |
265
|
|
|
if (is_string($table)) { |
266
|
1 |
|
$table = new Table($table, [], $this->getAdapter()); |
267
|
|
|
} |
268
|
1 |
|
$table->insert($data)->save(); |
269
|
1 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* {@inheritdoc} |
273
|
|
|
*/ |
274
|
1 |
|
public function createDatabase($name, $options) |
275
|
|
|
{ |
276
|
1 |
|
$this->getAdapter()->createDatabase($name, $options); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
5 |
|
public function dropDatabase($name) |
283
|
|
|
{ |
284
|
5 |
|
$this->getAdapter()->dropDatabase($name); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* {@inheritdoc} |
289
|
|
|
*/ |
290
|
|
|
public function hasTable($tableName) |
291
|
|
|
{ |
292
|
|
|
return $this->getAdapter()->hasTable($tableName); |
293
|
1 |
|
} |
294
|
|
|
|
295
|
1 |
|
/** |
296
|
1 |
|
* {@inheritdoc} |
297
|
|
|
*/ |
298
|
|
|
public function table($tableName, $options = []) |
299
|
|
|
{ |
300
|
|
|
return new Table($tableName, $options, $this->getAdapter()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* A short-hand method to drop the given database table. |
305
|
|
|
* |
306
|
|
|
* @deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead. |
307
|
|
|
* @param string $tableName Table Name |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
|
|
public function dropTable($tableName) |
311
|
|
|
{ |
312
|
|
|
trigger_error('dropTable() is deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.', E_USER_DEPRECATED); |
313
|
|
|
$this->table($tableName)->drop()->save(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Perform checks on the migration, print a warning |
318
|
|
|
* if there are potential problems. |
319
|
|
|
* |
320
|
|
|
* Right now, the only check is if there is both a `change()` and |
321
|
|
|
* an `up()` or a `down()` method. |
322
|
|
|
* |
323
|
|
|
* @return void |
324
|
|
|
*/ |
325
|
|
|
public function preFlightCheck() |
326
|
|
|
{ |
327
|
|
|
if (method_exists($this, MigrationInterface::CHANGE)) { |
328
|
|
|
if (method_exists($this, MigrationInterface::UP) || |
329
|
|
|
method_exists($this, MigrationInterface::DOWN) ) { |
330
|
|
|
$this->output->writeln(sprintf( |
331
|
|
|
'<comment>warning</comment> Migration contains both change() and/or up()/down() methods. <options=bold>Ignoring up() and down()</>.' |
332
|
|
|
)); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
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.