|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use RDV\Bundle\MigrationBundle\Log\OutputLogger; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
use RDV\Bundle\MigrationBundle\Migration\Loader\MigrationsLoader; |
|
12
|
|
|
use RDV\Bundle\MigrationBundle\Migration\MigrationExecutor; |
|
13
|
|
|
|
|
14
|
|
|
class LoadMigrationsCommand extends ContainerAwareCommand |
|
15
|
|
|
{ |
|
16
|
|
|
const DEFAULT_TIMEOUT = 60; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritdoc |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function configure() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->setName('rdv:migration:load') |
|
24
|
|
|
->setDescription('Execute migration scripts.') |
|
25
|
|
|
->addOption( |
|
26
|
|
|
'force', |
|
27
|
|
|
null, |
|
28
|
|
|
InputOption::VALUE_NONE, |
|
29
|
|
|
'Causes the generated by migrations SQL statements to be physically executed against your database.' |
|
30
|
|
|
) |
|
31
|
|
|
->addOption( |
|
32
|
|
|
'dry-run', |
|
33
|
|
|
null, |
|
34
|
|
|
InputOption::VALUE_NONE, |
|
35
|
|
|
'Outputs list of migrations without apply them.' |
|
36
|
|
|
) |
|
37
|
|
|
->addOption( |
|
38
|
|
|
'show-queries', |
|
39
|
|
|
null, |
|
40
|
|
|
InputOption::VALUE_NONE, |
|
41
|
|
|
'Outputs list of database queries for each migration file.' |
|
42
|
|
|
) |
|
43
|
|
|
->addOption( |
|
44
|
|
|
'bundles', |
|
45
|
|
|
null, |
|
46
|
|
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
|
47
|
|
|
'A list of bundles to load data from. If option is not set, migrations will be taken from all bundles.' |
|
48
|
|
|
) |
|
49
|
|
|
->addOption( |
|
50
|
|
|
'exclude', |
|
51
|
|
|
null, |
|
52
|
|
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
|
53
|
|
|
'A list of bundle names which migrations should be skipped.' |
|
54
|
|
|
) |
|
55
|
|
|
->addOption( |
|
56
|
|
|
'timeout', |
|
57
|
|
|
null, |
|
58
|
|
|
InputOption::VALUE_OPTIONAL, |
|
59
|
|
|
'Timeout for child command execution', |
|
60
|
|
|
self::DEFAULT_TIMEOUT |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
68
|
|
|
{ |
|
69
|
|
|
$force = $input->getOption('force'); |
|
70
|
|
|
$dryRun = $input->getOption('dry-run'); |
|
71
|
|
|
|
|
72
|
|
|
if ($force || $dryRun) { |
|
73
|
|
|
$output->writeln($dryRun ? 'List of migrations:' : 'Process migrations...'); |
|
74
|
|
|
|
|
75
|
|
|
$migrationLoader = $this->getMigrationLoader($input); |
|
76
|
|
|
$migrations = $migrationLoader->getMigrations(); |
|
77
|
|
|
if (!empty($migrations)) { |
|
78
|
|
|
if ($input->getOption('dry-run') && !$input->getOption('show-queries')) { |
|
79
|
|
|
foreach ($migrations as $item) { |
|
80
|
|
|
$output->writeln(sprintf(' <comment>> %s</comment>', get_class($item->getMigration()))); |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
$logger = new OutputLogger($output, true, null, ' '); |
|
84
|
|
|
$queryLogger = new OutputLogger( |
|
85
|
|
|
$output, |
|
86
|
|
|
true, |
|
87
|
|
|
$input->getOption('show-queries') ? null : OutputInterface::VERBOSITY_QUIET, |
|
88
|
|
|
' ' |
|
89
|
|
|
); |
|
90
|
|
|
$executor = $this->getMigrationExecutor(); |
|
91
|
|
|
$executor->setLogger($logger); |
|
92
|
|
|
$executor->getQueryExecutor()->setLogger($queryLogger); |
|
93
|
|
|
$executor->executeUp($migrations, $input->getOption('dry-run')); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
|
|
$output->writeln( |
|
98
|
|
|
'<comment>ATTENTION</comment>: Database backup is highly recommended before executing this command.' |
|
99
|
|
|
); |
|
100
|
|
|
$output->writeln(''); |
|
101
|
|
|
$output->writeln('To force execution run command with <info>--force</info> option:'); |
|
102
|
|
|
$output->writeln(sprintf(' <info>%s --force</info>', $this->getName())); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param InputInterface $input |
|
108
|
|
|
* @return MigrationsLoader |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getMigrationLoader(InputInterface $input) |
|
111
|
|
|
{ |
|
112
|
|
|
$migrationLoader = $this->getContainer()->get('rdv_migration.migrations.loader'); |
|
113
|
|
|
$bundles = $input->getOption('bundles'); |
|
114
|
|
|
if (!empty($bundles)) { |
|
115
|
|
|
$migrationLoader->setBundles($bundles); |
|
116
|
|
|
} |
|
117
|
|
|
$excludeBundles = $input->getOption('exclude'); |
|
118
|
|
|
if (!empty($excludeBundles)) { |
|
119
|
|
|
$migrationLoader->setExcludeBundles($excludeBundles); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $migrationLoader; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return MigrationExecutor |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function getMigrationExecutor() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getContainer()->get('rdv_migration.migrations.executor'); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|