1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Console\Traits\DbHelper; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* MigrateCommand |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
class MigrateUpCommand extends Command |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
use DbHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Configuration of command |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this |
24
|
|
|
->setName('migrate:up') |
25
|
|
|
->setDescription('Command for run migration') |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute method of command |
31
|
|
|
* |
32
|
|
|
* @param InputInterface $input |
33
|
|
|
* @param OutputInterface $output |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
38
|
|
|
{ |
39
|
|
|
if (!is_dir(MIGRATIONS_PATH) || !is_readable(MIGRATIONS_PATH)) { |
40
|
|
|
throw new \RunTimeException(sprintf('Migrations path `%s` is not good', MIGRATIONS_PATH)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$output->writeln([ |
44
|
|
|
'<info>Run migrations</info>', |
45
|
|
|
sprintf('Ensure table `%s` presence', $this->migrationsTable) |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$this->safeCreateTable($this->migrationsTable); |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
$output->writeln([ |
52
|
|
|
sprintf('Can\'t ensure table `%s` presence. Please verify DB connection params and presence of database named', $this->migrationsTable), |
53
|
|
|
sprintf('Error: `%s`', $e->getMessage()), |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$finder = new Finder(); |
58
|
|
|
$finder->files()->name('*.php')->in(MIGRATIONS_PATH); |
59
|
|
|
|
60
|
|
|
$this->runActions($finder, $output, $this->migrationsTable, 'up'); |
61
|
|
|
|
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.