Passed
Pull Request — master (#2007)
by
unknown
02:49
created

AbstractMigration::shouldExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 Input
71
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output 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, array $params = [])
197 10
    {
198
        return $this->getAdapter()->execute($sql, $params);
199 10
    }
200 10
201
    /**
202
     * @inheritDoc
203
     */
204
    public function query($sql, array $params = [])
205
    {
206 2
        return $this->getAdapter()->query($sql, $params);
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 createDatabase($name, $options)
237
    {
238 1
        $this->getAdapter()->createDatabase($name, $options);
239
    }
240 1
241
    /**
242
     * @inheritDoc
243
     */
244
    public function dropDatabase($name)
245
    {
246 3
        $this->getAdapter()->dropDatabase($name);
247
    }
248
249 3
    /**
250 2
     * @inheritDoc
251 2
     */
252 3
    public function createSchema($name)
253 3
    {
254
        $this->getAdapter()->createSchema($name);
255
    }
256
257
    /**
258 1
     * @inheritDoc
259
     */
260 1
    public function dropSchema($name)
261 1
    {
262
        $this->getAdapter()->dropSchema($name);
263
    }
264
265
    /**
266 1
     * @inheritDoc
267
     */
268 1
    public function hasTable($tableName)
269 1
    {
270
        return $this->getAdapter()->hasTable($tableName);
271
    }
272
273
    /**
274 1
     * @inheritDoc
275
     */
276 1
    public function table($tableName, $options = [])
277
    {
278
        $table = new Table($tableName, $options, $this->getAdapter());
279
        $this->tables[] = $table;
280
281
        return $table;
282 5
    }
283
284 5
    /**
285
     * Perform checks on the migration, print a warning
286
     * if there are potential problems.
287
     *
288
     * Right now, the only check is if there is both a `change()` and
289
     * an `up()` or a `down()` method.
290
     *
291
     * @return void
292
     */
293 1
    public function preFlightCheck()
294
    {
295 1
        if (method_exists($this, MigrationInterface::CHANGE)) {
296 1
            if (
297
                method_exists($this, MigrationInterface::UP) ||
298
                method_exists($this, MigrationInterface::DOWN)
299
            ) {
300
                $this->output->writeln(sprintf(
301
                    '<comment>warning</comment> Migration contains both change() and up()/down() methods.  <options=bold>Ignoring up() and down()</>.'
302
                ));
303
            }
304
        }
305
    }
306
307
    /**
308
     * Perform checks on the migration after completion
309
     *
310
     * Right now, the only check is whether all changes were committed
311
     *
312
     * @throws \RuntimeException
313
     * @return void
314
     */
315
    public function postFlightCheck()
316
    {
317
        foreach ($this->tables as $table) {
318
            if ($table->hasPendingActions()) {
319
                throw new RuntimeException('Migration has pending actions after execution!');
320
            }
321
        }
322
    }
323
324
    /**
325
     * Checks to see if the migration should be executed.
326
     *
327
     * Returns true by default.
328
     *
329
     * You can use this to prevent a migration from executing.
330
     *
331
     * @return bool
332
     */
333
    public function shouldExecute()
334
    {
335
        return true;
336
    }
337
}
338