1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2017 Cake Software Foundation |
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\Db\Adapter |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Db\Adapter; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Table; |
32
|
|
|
use Phinx\Db\Table\Column; |
33
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
34
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
35
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Base Abstract Database Adapter. |
39
|
|
|
*/ |
40
|
|
|
abstract class AbstractAdapter implements AdapterInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $options = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
49
|
|
|
*/ |
50
|
|
|
protected $input; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
54
|
|
|
*/ |
55
|
|
|
protected $output; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $schemaTableName = 'phinxlog'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Class Constructor. |
64
|
|
|
* |
65
|
|
|
* @param array $options Options |
66
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input Input Interface |
67
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output Output Interface |
68
|
|
|
*/ |
69
|
341 |
|
public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null) |
70
|
|
|
{ |
71
|
341 |
|
$this->setOptions($options); |
72
|
341 |
|
if ($input !== null) { |
73
|
264 |
|
$this->setInput($input); |
74
|
264 |
|
} |
75
|
341 |
|
if ($output !== null) { |
76
|
264 |
|
$this->setOutput($output); |
77
|
264 |
|
} |
78
|
341 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
287 |
|
public function setOptions(array $options) |
84
|
|
|
{ |
85
|
287 |
|
$this->options = $options; |
86
|
|
|
|
87
|
287 |
|
if (isset($options['default_migration_table'])) { |
88
|
6 |
|
$this->setSchemaTableName($options['default_migration_table']); |
89
|
6 |
|
} |
90
|
|
|
|
91
|
287 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
196 |
|
public function getOptions() |
98
|
|
|
{ |
99
|
196 |
|
return $this->options; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
8 |
|
public function hasOption($name) |
106
|
|
|
{ |
107
|
8 |
|
return isset($this->options[$name]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getOption($name) |
114
|
|
|
{ |
115
|
|
|
if (!$this->hasOption($name)) { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->options[$name]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
269 |
|
*/ |
125
|
|
|
public function setInput(InputInterface $input) |
126
|
269 |
|
{ |
127
|
269 |
|
$this->input = $input; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
218 |
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
218 |
|
public function getInput() |
136
|
|
|
{ |
137
|
|
|
return $this->input; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
269 |
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
269 |
|
public function setOutput(OutputInterface $output) |
144
|
269 |
|
{ |
145
|
|
|
$this->output = $output; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
8 |
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
8 |
|
*/ |
153
|
|
|
public function getOutput() |
154
|
|
|
{ |
155
|
|
|
if ($this->output === null) { |
156
|
8 |
|
$output = new NullOutput(); |
157
|
|
|
$this->setOutput($output); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->output; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getVersions() |
169
|
|
|
{ |
170
|
|
|
$rows = $this->getVersionLog(); |
171
|
|
|
|
172
|
|
|
return array_keys($rows); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
195 |
|
* Gets the schema table name. |
177
|
|
|
* |
178
|
195 |
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getSchemaTableName() |
181
|
|
|
{ |
182
|
|
|
return $this->schemaTableName; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets the schema table name. |
187
|
7 |
|
* |
188
|
|
|
* @param string $schemaTableName Schema Table Name |
189
|
7 |
|
* @return $this |
190
|
7 |
|
*/ |
191
|
|
|
public function setSchemaTableName($schemaTableName) |
192
|
|
|
{ |
193
|
|
|
$this->schemaTableName = $schemaTableName; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
193 |
|
} |
197
|
|
|
|
198
|
193 |
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
public function hasSchemaTable() |
202
|
|
|
{ |
203
|
|
|
return $this->hasTable($this->getSchemaTableName()); |
204
|
191 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
191 |
|
*/ |
209
|
|
|
public function createSchemaTable() |
210
|
191 |
|
{ |
211
|
|
|
try { |
212
|
191 |
|
$options = [ |
213
|
191 |
|
'id' => false, |
214
|
191 |
|
'primary_key' => 'version' |
215
|
191 |
|
]; |
216
|
191 |
|
|
217
|
191 |
|
$table = new Table($this->getSchemaTableName(), $options, $this); |
218
|
191 |
|
$table->addColumn('version', 'biginteger') |
219
|
191 |
|
->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true]) |
220
|
|
|
->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true]) |
221
|
|
|
->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true]) |
222
|
191 |
|
->addColumn('breakpoint', 'boolean', ['default' => false]) |
223
|
|
|
->save(); |
224
|
|
|
} catch (\Exception $exception) { |
225
|
|
|
throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage()); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function getAdapterType() |
233
|
|
|
{ |
234
|
|
|
return $this->getOption('adapter'); |
235
|
208 |
|
} |
236
|
|
|
|
237
|
208 |
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function isValidColumnType(Column $column) |
241
|
|
|
{ |
242
|
|
|
return in_array($column->getType(), $this->getColumnTypes()); |
243
|
|
|
} |
244
|
|
|
|
245
|
218 |
|
/** |
246
|
|
|
* Determines if instead of executing queries a dump to standard output is needed |
247
|
218 |
|
* |
248
|
|
|
* @return bool |
249
|
218 |
|
*/ |
250
|
|
|
public function isDryRunEnabled() |
251
|
|
|
{ |
252
|
|
|
$input = $this->getInput(); |
253
|
|
|
|
254
|
|
|
return ($input && $input->hasOption('dry-run')) ? $input->getOption('dry-run') : false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Determines whether to print the executed queries in the console |
259
|
|
|
* |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
|
|
public function isVerbosityEnabled() |
263
|
|
|
{ |
264
|
|
|
$output = $this->getOutput(); |
265
|
|
|
|
266
|
|
|
return OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity(); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|