Completed
Pull Request — master (#1575)
by
unknown
02:58
created

AbstractAdapter::updateCreatedTableName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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 Phinx\Util\Literal;
34
use Symfony\Component\Console\Input\InputInterface;
35
use Symfony\Component\Console\Output\NullOutput;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
/**
39
 * Base Abstract Database Adapter.
40
 */
41
abstract class AbstractAdapter implements AdapterInterface
42
{
43
    /**
44
     * @var array
45
     */
46
    protected $options = [];
47
48
    /**
49
     * @var \Symfony\Component\Console\Input\InputInterface
50
     */
51
    protected $input;
52
53
    /**
54
     * @var \Symfony\Component\Console\Output\OutputInterface
55
     */
56
    protected $output;
57
58
    /**
59
     * @var string[]
60
     */
61
    protected $createdTables = [];
62
63
    /**
64
     * @var string
65
     */
66
    protected $schemaTableName = 'phinxlog';
67
68
    /**
69 341
     * Class Constructor.
70
     *
71 341
     * @param array $options Options
72 341
     * @param \Symfony\Component\Console\Input\InputInterface $input Input Interface
73 264
     * @param \Symfony\Component\Console\Output\OutputInterface  $output Output Interface
74 264
     */
75 341
    public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null)
76 264
    {
77 264
        $this->setOptions($options);
78 341
        if ($input !== null) {
79
            $this->setInput($input);
80
        }
81
        if ($output !== null) {
82
            $this->setOutput($output);
83 287
        }
84
    }
85 287
86
    /**
87 287
     * {@inheritdoc}
88 6
     */
89 6
    public function setOptions(array $options)
90
    {
91 287
        $this->options = $options;
92
93
        if (isset($options['default_migration_table'])) {
94
            $this->setSchemaTableName($options['default_migration_table']);
95
        }
96
97 196
        return $this;
98
    }
99 196
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getOptions()
104
    {
105 8
        return $this->options;
106
    }
107 8
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function hasOption($name)
112
    {
113
        return isset($this->options[$name]);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getOption($name)
120
    {
121
        if (!$this->hasOption($name)) {
122
            return null;
123
        }
124 269
125
        return $this->options[$name];
126 269
    }
127 269
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function setInput(InputInterface $input)
132
    {
133 218
        $this->input = $input;
134
135 218
        return $this;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 269
    public function getInput()
142
    {
143 269
        return $this->input;
144 269
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function setOutput(OutputInterface $output)
150 8
    {
151
        $this->output = $output;
152 8
153
        return $this;
154
    }
155
156 8
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getOutput()
160
    {
161
        if ($this->output === null) {
162
            $output = new NullOutput();
163
            $this->setOutput($output);
164
        }
165
166
        return $this->output;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     *
172
     * @return array
173
     */
174
    public function getVersions()
175
    {
176 195
        $rows = $this->getVersionLog();
177
178 195
        return array_keys($rows);
179
    }
180
181
    /**
182
     * Gets the schema table name.
183
     *
184
     * @return string
185
     */
186
    public function getSchemaTableName()
187 7
    {
188
        return $this->schemaTableName;
189 7
    }
190 7
191
    /**
192
     * Sets the schema table name.
193
     *
194
     * @param string $schemaTableName Schema Table Name
195
     * @return $this
196 193
     */
197
    public function setSchemaTableName($schemaTableName)
198 193
    {
199
        $this->schemaTableName = $schemaTableName;
200
201
        return $this;
202
    }
203
204 191
    /**
205
     * {@inheritdoc}
206
     */
207
    public function hasSchemaTable()
208 191
    {
209
        return $this->hasTable($this->getSchemaTableName());
210 191
    }
211
212 191
    /**
213 191
     * {@inheritdoc}
214 191
     */
215 191
    public function createSchemaTable()
216 191
    {
217 191
        try {
218 191
            $options = [
219 191
                'id' => false,
220
                'primary_key' => 'version'
221
            ];
222 191
223
            $table = new Table($this->getSchemaTableName(), $options, $this);
224
            $table->addColumn('version', 'biginteger')
225
                ->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true])
226
                ->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true])
227
                ->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true])
228
                ->addColumn('breakpoint', 'boolean', ['default' => false])
229
                ->save();
230
        } catch (\Exception $exception) {
231
            throw new \InvalidArgumentException(
232
                'There was a problem creating the schema table: ' . $exception->getMessage(),
233
                intval($exception->getCode()),
234
                $exception
235 208
            );
236
        }
237 208
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function getAdapterType()
243
    {
244
        return $this->getOption('adapter');
245 218
    }
246
247 218
    /**
248
     * {@inheritdoc}
249 218
     */
250
    public function isValidColumnType(Column $column)
251
    {
252
        return $column->getType() instanceof Literal || in_array($column->getType(), $this->getColumnTypes());
253
    }
254
255
    /**
256
     * Determines if instead of executing queries a dump to standard output is needed
257
     *
258
     * @return bool
259
     */
260
    public function isDryRunEnabled()
261
    {
262
        $input = $this->getInput();
263
264
        return ($input && $input->hasOption('dry-run')) ? (bool)$input->getOption('dry-run') : false;
265
    }
266
267
    /**
268
     * Adds user-created tables (e.g. not phinxlog) to a cached list
269
     *
270
     * @param string $tableName The name of the table
271
     * @return void
272
     */
273
    public function addCreatedTable($tableName)
274
    {
275
        if (substr_compare($tableName, 'phinxlog', -strlen('phinxlog')) !== 0) {
276
            $this->createdTables[] = $tableName;
277
        }
278
    }
279
280
    /**
281
     * Updates the name of the cached table
282
     *
283
     * @param string $tableName Original name of the table
284
     * @param string $newTableName New name of the table
285
     * @return void
286
     */
287
    public function updateCreatedTableName($tableName, $newTableName)
288
    {
289
        $key = array_search($tableName, $this->createdTables);
290
        if ($key !== false) {
291
            $this->createdTables[$key] = $newTableName;
292
        }
293
    }
294
295
    /**
296
     * Removes table from the cached created list
297
     *
298
     * @param string $tableName The name of the table
299
     * @return void
300
     */
301
    public function removeCreatedTable($tableName)
302
    {
303
        if (($key = array_search($tableName, $this->createdTables)) !== false) {
304
            unset($this->createdTables[$key]);
305
        }
306
    }
307
308
    /**
309
     * Check if the table is in the cached list of created tables
310
     *
311
     * @param string $tableName The name of the table
312
     * @return bool
313
     */
314
    public function hasCreatedTable($tableName)
315
    {
316
        return in_array($tableName, $this->createdTables);
317
    }
318
}
319