Improved 'insert-sql' command

This commit is contained in:
William DURAND 2011-04-06 15:05:32 +02:00
parent 3c4bf8f662
commit 03effb8a47

View file

@ -31,12 +31,15 @@ class InsertSqlCommand extends PhingCommand
$this
->setDescription('Insert SQL for current model')
->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action.')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
->setHelp(<<<EOT
The <info>propel:insert-sql</info> command connects to the database and executes all SQL statements found in <comment>app/propel/sql/*schema.sql</comment>.
<info>php app/console propel:insert-sql</info>
The <info>--force</info> parameter has to be used to actually insert SQL.
The <info>--connection</info> parameter allows you to change the connection to use.
The default connection is the active connection (propel.dbal.default_connection).
EOT
)
->setName('propel:insert-sql')
@ -51,7 +54,25 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('force')) {
$this->callPhing('insert-sql');
$container = $this->getApplication()->getKernel()->getContainer();
$propelConfiguration = $container->get('propel.configuration');
$name = $input->getOption('connection') ? $input->getOption('connection') : $container->getParameter('propel.dbal.default_connection');
if (isset($propelConfiguration['datasources'][$name])) {
$defaultConfig = $propelConfiguration['datasources'][$name];
} else {
throw new \InvalidArgumentException(sprintf('Connection named %s doesn\'t exist', $name));
}
$output->writeln(sprintf('<info>Generate XML schema from connection named <comment>%s</comment></info>', $name));
$this->callPhing('insert-sql', array(
'propel.database.url' => $defaultConfig['connection']['dsn'],
'propel.database.database' => $defaultConfig['adapter'],
'propel.database.user' => $defaultConfig['connection']['user'],
'propel.database.password' => $defaultConfig['connection']['password'],
'propel.schema.dir' => $this->getApplication()->getKernel()->getRootDir() . '/propel/schema/',
));
} else {
$output->writeln('<error>You have to use --force to execute all SQL statements.</error>');
}