1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dami\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument, |
6
|
|
|
Symfony\Component\Console\Input\InputOption, |
7
|
|
|
Symfony\Component\Console\Input\InputInterface, |
8
|
|
|
Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class MigrateCommand extends ContainerAwareCommand |
11
|
|
|
{ |
12
|
|
|
protected function configure() |
13
|
|
|
{ |
14
|
|
|
parent::configure(); |
15
|
|
|
|
16
|
|
|
$this |
17
|
|
|
->setDescription('Migrate database.') |
18
|
|
|
->addArgument('to-version', InputArgument::OPTIONAL, 'Migrate to specific version of migrations'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
22
|
|
|
{ |
23
|
|
|
parent::execute($input, $output); |
24
|
|
|
|
25
|
|
|
$version = $input->hasArgument('to-version') ? $input->getArgument('to-version') : null; |
26
|
|
|
$migration = $this->getContainer()->get('dami.migration'); |
27
|
|
|
|
28
|
|
|
$message = function($name, $version) use ($output) { |
29
|
|
|
$output->write(sprintf("\n<comment>Migration %s %s</comment>", |
30
|
|
|
$version, $name)); |
31
|
|
|
}; |
32
|
|
|
try { |
33
|
|
|
$numberMigrations = $migration->migrate($version, $message); |
34
|
|
|
if ($numberMigrations > 0) { |
35
|
|
View Code Duplication |
if ($numberMigrations == 1) { |
|
|
|
|
36
|
|
|
$output->write(sprintf("\n<info>%d migration was executed.</info>", $numberMigrations)); |
37
|
|
|
} else { |
38
|
|
|
$output->write(sprintf("\n<info>%d migrations were executed.</info>", $numberMigrations)); |
39
|
|
|
} |
40
|
|
|
} else { |
41
|
|
|
$output->writeln(sprintf('<comment>No migrations detected.</comment>')); |
42
|
|
|
} |
43
|
|
|
} catch(\PDOException $e) { |
44
|
|
|
$output->writeln("\n<error>There was something wrong during migration. Database schema has not been changed.</error>"); |
45
|
|
|
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
46
|
|
|
$output->writeln(sprintf("\n<error>%s</error>", $e->getMessage())); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
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.