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\Migration\Manager\Environment; |
33
|
|
|
use Phinx\Db\Table; |
34
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
35
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Abstract Migration Class. |
39
|
|
|
* |
40
|
|
|
* It is expected that the migrations you write extend from this class. |
41
|
|
|
* |
42
|
|
|
* This abstract class proxies the various database methods to your specified |
43
|
|
|
* adapter. |
44
|
|
|
* |
45
|
|
|
* @author Rob Morgan <[email protected]> |
46
|
|
|
*/ |
47
|
|
|
abstract class AbstractMigration implements MigrationInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var float |
51
|
|
|
*/ |
52
|
|
|
protected $version; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var \Phinx\Db\Adapter\AdapterInterface |
56
|
|
|
*/ |
57
|
|
|
protected $adapter; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
61
|
|
|
*/ |
62
|
|
|
protected $output; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
66
|
|
|
*/ |
67
|
|
|
protected $input; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var Environment |
71
|
|
|
*/ |
72
|
|
|
protected $environment; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Whether this migration is being applied or reverted |
76
|
|
|
* |
77
|
|
|
* @var bool |
78
|
|
|
*/ |
79
|
|
|
protected $isMigratingUp = true; |
80
|
|
|
|
81
|
|
|
/** |
82
|
406 |
|
* Class Constructor. |
83
|
|
|
* |
84
|
406 |
|
* @param int $version Migration Version |
85
|
406 |
|
* @param \Symfony\Component\Console\Input\InputInterface|null $input |
86
|
384 |
|
* @param \Symfony\Component\Console\Output\OutputInterface|null $output |
87
|
384 |
|
* @param Environment|null $environment |
88
|
406 |
|
*/ |
89
|
384 |
|
final public function __construct($version, InputInterface $input = null, OutputInterface $output = null, Environment $environment = null) |
90
|
384 |
|
{ |
91
|
|
|
$this->version = $version; |
|
|
|
|
92
|
406 |
|
if (!is_null($input)) { |
93
|
406 |
|
$this->setInput($input); |
94
|
|
|
} |
95
|
|
|
if (!is_null($output)) { |
96
|
|
|
$this->setOutput($output); |
97
|
|
|
} |
98
|
|
|
if (!is_null($environment)){ |
99
|
|
|
$this->setEnvironment($environment); |
100
|
406 |
|
} |
101
|
|
|
|
102
|
406 |
|
$this->init(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Initialize method. |
107
|
1 |
|
* |
108
|
|
|
* @return void |
109
|
1 |
|
*/ |
110
|
|
|
protected function init() |
111
|
|
|
{ |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
1 |
|
*/ |
117
|
|
|
public function up() |
118
|
|
|
{ |
119
|
|
|
} |
120
|
|
|
|
121
|
21 |
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
21 |
|
*/ |
124
|
21 |
|
public function down() |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
13 |
|
*/ |
131
|
|
|
public function setAdapter(AdapterInterface $adapter) |
132
|
13 |
|
{ |
133
|
|
|
$this->adapter = $adapter; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
384 |
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
384 |
|
*/ |
141
|
384 |
|
public function getAdapter() |
142
|
|
|
{ |
143
|
|
|
return $this->adapter; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
2 |
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
2 |
|
public function setEnvironment(Environment $environment) |
150
|
|
|
{ |
151
|
|
|
$this->environment = $environment; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
385 |
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
385 |
|
*/ |
158
|
385 |
|
public function getEnvironment() |
159
|
|
|
{ |
160
|
|
|
return $this->environment; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
3 |
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
3 |
|
public function setInput(InputInterface $input) |
167
|
|
|
{ |
168
|
|
|
$this->input = $input; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
140 |
|
|
173
|
|
|
/** |
174
|
140 |
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function getInput() |
177
|
|
|
{ |
178
|
|
|
return $this->input; |
179
|
|
|
} |
180
|
1 |
|
|
181
|
|
|
/** |
182
|
1 |
|
* {@inheritdoc} |
183
|
1 |
|
*/ |
184
|
|
|
public function setOutput(OutputInterface $output) |
185
|
|
|
{ |
186
|
|
|
$this->output = $output; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
279 |
|
} |
190
|
|
|
|
191
|
279 |
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function getOutput() |
195
|
|
|
{ |
196
|
|
|
return $this->output; |
197
|
10 |
|
} |
198
|
|
|
|
199
|
10 |
|
/** |
200
|
10 |
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function getName() |
203
|
|
|
{ |
204
|
|
|
return get_class($this); |
205
|
|
|
} |
206
|
2 |
|
|
207
|
|
|
/** |
208
|
2 |
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function setVersion($version) |
211
|
|
|
{ |
212
|
|
|
$this->version = $version; |
213
|
|
|
|
214
|
3 |
|
return $this; |
215
|
|
|
} |
216
|
3 |
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getVersion() |
221
|
|
|
{ |
222
|
1 |
|
return $this->version; |
223
|
|
|
} |
224
|
1 |
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
|
|
public function setMigratingUp($isMigratingUp) |
229
|
|
|
{ |
230
|
1 |
|
$this->isMigratingUp = $isMigratingUp; |
231
|
|
|
|
232
|
1 |
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
1 |
|
public function isMigratingUp() |
239
|
|
|
{ |
240
|
1 |
|
return $this->isMigratingUp; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
3 |
|
public function execute($sql) |
247
|
|
|
{ |
248
|
|
|
return $this->getAdapter()->execute($sql); |
249
|
3 |
|
} |
250
|
2 |
|
|
251
|
2 |
|
/** |
252
|
3 |
|
* {@inheritdoc} |
253
|
3 |
|
*/ |
254
|
|
|
public function query($sql) |
255
|
|
|
{ |
256
|
|
|
return $this->getAdapter()->query($sql); |
257
|
|
|
} |
258
|
1 |
|
|
259
|
|
|
/** |
260
|
1 |
|
* {@inheritdoc} |
261
|
1 |
|
*/ |
262
|
|
|
public function fetchRow($sql) |
263
|
|
|
{ |
264
|
|
|
return $this->getAdapter()->fetchRow($sql); |
265
|
|
|
} |
266
|
1 |
|
|
267
|
|
|
/** |
268
|
1 |
|
* {@inheritdoc} |
269
|
1 |
|
*/ |
270
|
|
|
public function fetchAll($sql) |
271
|
|
|
{ |
272
|
|
|
return $this->getAdapter()->fetchAll($sql); |
273
|
|
|
} |
274
|
1 |
|
|
275
|
|
|
/** |
276
|
1 |
|
* {@inheritdoc} |
277
|
|
|
*/ |
278
|
|
View Code Duplication |
public function insert($table, $data) |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
// convert to table object |
281
|
|
|
if (is_string($table)) { |
282
|
5 |
|
$table = new Table($table, [], $this->getAdapter()); |
283
|
|
|
} |
284
|
5 |
|
$table->insert($data)->save(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* {@inheritdoc} |
289
|
|
|
*/ |
290
|
|
|
public function createDatabase($name, $options) |
291
|
|
|
{ |
292
|
|
|
$this->getAdapter()->createDatabase($name, $options); |
293
|
1 |
|
} |
294
|
|
|
|
295
|
1 |
|
/** |
296
|
1 |
|
* {@inheritdoc} |
297
|
|
|
*/ |
298
|
|
|
public function dropDatabase($name) |
299
|
|
|
{ |
300
|
|
|
$this->getAdapter()->dropDatabase($name); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* {@inheritdoc} |
305
|
|
|
*/ |
306
|
|
|
public function hasTable($tableName) |
307
|
|
|
{ |
308
|
|
|
return $this->getAdapter()->hasTable($tableName); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* {@inheritdoc} |
313
|
|
|
*/ |
314
|
|
|
public function table($tableName, $options = []) |
315
|
|
|
{ |
316
|
|
|
return new Table($tableName, $options, $this->getAdapter()); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* A short-hand method to drop the given database table. |
321
|
|
|
* |
322
|
|
|
* @param string $tableName Table Name |
323
|
|
|
* @return void |
324
|
|
|
*/ |
325
|
|
|
public function dropTable($tableName) |
326
|
|
|
{ |
327
|
|
|
$this->table($tableName)->drop(); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
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.