forked from propelorm/Propel2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigrationDownCommand.php
159 lines (130 loc) · 5.59 KB
/
MigrationDownCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Propel\Generator\Config\GeneratorConfig;
use Propel\Generator\Manager\MigrationManager;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class MigrationDownCommand extends AbstractCommand
{
const DEFAULT_OUTPUT_DIRECTORY = 'generated-migrations';
const DEFAULT_MIGRATION_TABLE = 'propel_migration';
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this
->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY)
->addOption('migration-table', null, InputOption::VALUE_REQUIRED, 'Migration table name', self::DEFAULT_MIGRATION_TABLE)
->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use', array())
->setName('migration:down')
->setDescription('Execute migrations down')
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$generatorConfig = new GeneratorConfig(array(
'propel.platform.class' => $input->getOption('platform'),
));
$this->createDirectory($input->getOption('output-dir'));
$manager = new MigrationManager();
$manager->setGeneratorConfig($generatorConfig);
$connections = array();
foreach ($input->getOption('connection') as $connection) {
list($name, $dsn, $infos) = $this->parseConnection($connection);
$connections[$name] = array_merge(array('dsn' => $dsn), $infos);
}
$manager->setConnections($connections);
$manager->setMigrationTable($input->getOption('migration-table'));
$manager->setWorkingDirectory($input->getOption('output-dir'));
$previousTimestamps = $manager->getAlreadyExecutedMigrationTimestamps();
if (!$nextMigrationTimestamp = array_pop($previousTimestamps)) {
$output->writeln('No migration were ever executed on this database - nothing to reverse.');
return false;
}
$output->writeln(sprintf(
'Executing migration %s down',
$manager->getMigrationClassName($nextMigrationTimestamp)
));
if ($nbPreviousTimestamps = count($previousTimestamps)) {
$previousTimestamp = array_pop($previousTimestamps);
} else {
$previousTimestamp = 0;
}
$migration = $manager->getMigrationObject($nextMigrationTimestamp);
if (false === $migration->preDown($manager)) {
$output->writeln('<error>preDown() returned false. Aborting migration.</error>');
return false;
}
foreach ($migration->getDownSQL() as $datasource => $sql) {
$connection = $manager->getConnection($datasource);
if ($input->getOption('verbose')) {
$output->writeln(sprintf(
'Connecting to database "%s" using DSN "%s"',
$datasource,
$connection['dsn']
));
}
$pdo = $manager->getPdoConnection($datasource);
$res = 0;
$statements = SqlParser::parseString($sql);
foreach ($statements as $statement) {
try {
if ($input->getOption('verbose')) {
$output->writeln(sprintf('Executing statement "%s"', $statement));
}
$stmt = $pdo->prepare($statement);
$stmt->execute();
$res++;
} catch (PDOException $e) {
$output->writeln(sprintf('<error>Failed to execute SQL "%s"</error>', $statement));
}
}
if (!$res) {
$output->writeln('No statement was executed. The version was not updated.');
$output->writeln(sprintf(
'Please review the code in "%s"',
$manager->getMigrationDir() . DIRECTORY_SEPARATOR . $manager->getMigrationClassName($nextMigrationTimestamp)
));
$output->writeln('<error>Migration aborted</error>');
return false;
}
$output->writeln(sprintf(
'%d of %d SQL statements executed successfully on datasource "%s"',
$res,
count($statements),
$datasource
));
$manager->updateLatestMigrationTimestamp($datasource, $previousTimestamp);
if ($input->getOption('verbose')) {
$output->writeln(sprintf(
'Downgraded migration date to %d for datasource "%s"',
$previousTimestamp,
$datasource
));
}
}
$migration->postDown($manager);
if ($nbPreviousTimestamps) {
$output->writeln(sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
} else {
$output->writeln('Reverse migration complete. No more migration available for reverse');
}
}
}