Completed
Push — master ( d748e1...415000 )
by Mark
18s queued 13s
created

AbstractMigration::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Migration;
9
10
use Phinx\Db\Adapter\AdapterInterface;
11
use Phinx\Db\Table;
12
use RuntimeException;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Abstract Migration Class.
18
 *
19
 * It is expected that the migrations you write extend from this class.
20
 *
21
 * This abstract class proxies the various database methods to your specified
22
 * adapter.
23
 *
24
 * @author Rob Morgan <[email protected]>
25
 */
26
abstract class AbstractMigration implements MigrationInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    protected $environment;
32
33
    /**
34
     * @var int
35
     */
36
    protected $version;
37
38
    /**
39
     * @var \Phinx\Db\Adapter\AdapterInterface
40
     */
41
    protected $adapter;
42
43
    /**
44
     * @var \Symfony\Component\Console\Output\OutputInterface
45
     */
46
    protected $output;
47
48
    /**
49
     * @var \Symfony\Component\Console\Input\InputInterface
50
     */
51
    protected $input;
52
53
    /**
54
     * Whether this migration is being applied or reverted
55
     *
56
     * @var bool
57
     */
58
    protected $isMigratingUp = true;
59
60
    /**
61
     * List of all the table objects created by this migration
62
     *
63
     * @var array
64
     */
65
    protected $tables = [];
66
67
    /**
68
     * @param string $environment Environment Detected
69
     * @param int $version Migration Version
70
     * @param \Symfony\Component\Console\Input\InputInterface|null $input
71
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
72
     */
73
    final public function __construct($environment, $version, InputInterface $input = null, OutputInterface $output = null)
74
    {
75
        $this->environment = $environment;
76
        $this->version = $version;
77
78
        if ($input !== null) {
79
            $this->setInput($input);
80
        }
81
82 406
        if ($output !== null) {
83
            $this->setOutput($output);
84 406
        }
85 406
    }
86 384
87 384
    /**
88 406
     * @inheritDoc
89 384
     */
90 384
    public function setAdapter(AdapterInterface $adapter)
91
    {
92 406
        $this->adapter = $adapter;
93 406
94
        return $this;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 406
    public function getAdapter()
101
    {
102 406
        return $this->adapter;
103
    }
104
105
    /**
106
     * @inheritDoc
107 1
     */
108
    public function setInput(InputInterface $input)
109 1
    {
110
        $this->input = $input;
111
112
        return $this;
113
    }
114 1
115
    /**
116 1
     * @inheritDoc
117
     */
118
    public function getInput()
119
    {
120
        return $this->input;
121 21
    }
122
123 21
    /**
124 21
     * @inheritDoc
125
     */
126
    public function setOutput(OutputInterface $output)
127
    {
128
        $this->output = $output;
129
130 13
        return $this;
131
    }
132 13
133
    /**
134
     * @inheritDoc
135
     */
136
    public function getOutput()
137
    {
138 384
        return $this->output;
139
    }
140 384
141 384
    /**
142
     * @inheritDoc
143
     */
144
    public function getName()
145
    {
146
        return static::class;
147 2
    }
148
149 2
    /**
150
     * @inheritDoc
151
     */
152
    public function getEnvironment()
153
    {
154
        return $this->environment;
155 385
    }
156
157 385
    /**
158 385
     * @inheritDoc
159
     */
160
    public function setVersion($version)
161
    {
162
        $this->version = $version;
163
164 3
        return $this;
165
    }
166 3
167
    /**
168
     * @inheritDoc
169
     */
170
    public function getVersion()
171
    {
172 140
        return $this->version;
173
    }
174 140
175
    /**
176
     * @inheritDoc
177
     */
178
    public function setMigratingUp($isMigratingUp)
179
    {
180 1
        $this->isMigratingUp = $isMigratingUp;
181
182 1
        return $this;
183 1
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public function isMigratingUp()
189 279
    {
190
        return $this->isMigratingUp;
191 279
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196
    public function execute($sql)
197 10
    {
198
        return $this->getAdapter()->execute($sql);
199 10
    }
200 10
201
    /**
202
     * @inheritDoc
203
     */
204
    public function query($sql)
205
    {
206 2
        return $this->getAdapter()->query($sql);
207
    }
208 2
209
    /**
210
     * @inheritDoc
211
     */
212
    public function getQueryBuilder()
213
    {
214 3
        return $this->getAdapter()->getQueryBuilder();
215
    }
216 3
217
    /**
218
     * @inheritDoc
219
     */
220
    public function fetchRow($sql)
221
    {
222 1
        return $this->getAdapter()->fetchRow($sql);
223
    }
224 1
225
    /**
226
     * @inheritDoc
227
     */
228
    public function fetchAll($sql)
229
    {
230 1
        return $this->getAdapter()->fetchAll($sql);
231
    }
232 1
233
    /**
234
     * @inheritDoc
235
     */
236
    public function insert($table, $data)
237
    {
238 1
        trigger_error('insert() is deprecated since 0.10.0. Use $this->table($tableName)->insert($data)->save() instead.', E_USER_DEPRECATED);
239
        // convert to table object
240 1
        if (is_string($table)) {
241
            $table = new Table($table, [], $this->getAdapter());
242
        }
243
        $table->insert($data)->save();
244
    }
245
246 3
    /**
247
     * @inheritDoc
248
     */
249 3
    public function createDatabase($name, $options)
250 2
    {
251 2
        $this->getAdapter()->createDatabase($name, $options);
252 3
    }
253 3
254
    /**
255
     * @inheritDoc
256
     */
257
    public function dropDatabase($name)
258 1
    {
259
        $this->getAdapter()->dropDatabase($name);
260 1
    }
261 1
262
    /**
263
     * @inheritDoc
264
     */
265
    public function hasTable($tableName)
266 1
    {
267
        return $this->getAdapter()->hasTable($tableName);
268 1
    }
269 1
270
    /**
271
     * @inheritDoc
272
     */
273
    public function table($tableName, $options = [])
274 1
    {
275
        $table = new Table($tableName, $options, $this->getAdapter());
276 1
        $this->tables[] = $table;
277
278
        return $table;
279
    }
280
281
    /**
282 5
     * A short-hand method to drop the given database table.
283
     *
284 5
     * @deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.
285
     *
286
     * @param string $tableName Table Name
287
     *
288
     * @return void
289
     */
290
    public function dropTable($tableName)
291
    {
292
        trigger_error('dropTable() is deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.', E_USER_DEPRECATED);
293 1
        $this->table($tableName)->drop()->save();
294
    }
295 1
296 1
    /**
297
     * Perform checks on the migration, print a warning
298
     * if there are potential problems.
299
     *
300
     * Right now, the only check is if there is both a `change()` and
301
     * an `up()` or a `down()` method.
302
     *
303
     * @param string|null $direction
304
     *
305
     * @return void
306
     */
307
    public function preFlightCheck($direction = null)
308
    {
309
        if (method_exists($this, MigrationInterface::CHANGE)) {
310
            if (method_exists($this, MigrationInterface::UP) ||
311
                method_exists($this, MigrationInterface::DOWN)
312
            ) {
313
                $this->output->writeln(sprintf(
314
                    '<comment>warning</comment> Migration contains both change() and/or up()/down() methods.  <options=bold>Ignoring up() and down()</>.'
315
                ));
316
            }
317
        }
318
    }
319
320
    /**
321
     * Perform checks on the migration after completion
322
     *
323
     * Right now, the only check is whether all changes were committed
324
     *
325
     * @param string|null $direction direction of migration
326
     *
327
     * @throws \RuntimeException
328
     *
329
     * @return void
330
     */
331
    public function postFlightCheck($direction = null)
332
    {
333
        foreach ($this->tables as $table) {
334
            if ($table->hasPendingActions()) {
335
                throw new RuntimeException('Migration has pending actions after execution!');
336
            }
337
        }
338
    }
339
}
340