-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow running dbal:run-sql
using arbitrary connection
#3950
Conversation
This slight modification allows running the `dbal:run-sql` command using an arbitrary database connection, which is specially useful in CLI applications that use multiple database connections. Backwards compatibility is maintained by using the default 'db' value for the input option.
How do you add a new |
There's another similar pull request #3956. |
When defining a generic <?php
require 'vendor/autoload.php';
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Symfony\Component\Console\Helper\HelperSet;
$dbParams = include 'migrations-db.php';
$connection = DriverManager::getConnection($dbParams);
return new HelperSet([
'db' => new ConnectionHelper($connection),
'db.read' => new ConnectionHelper($connection), // for example
'db.write' => new ConnectionHelper($connection), // for example
]); For documentation purposes, I was thinking about the following example: <?php
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
$dbParams = [...]; // A multidimensional array with connection parameters
$cli = new Application('app name', 'app version');
$cli->setHelperSet(new HelperSet([
'db.read' => new ConnectionHelper(
DriverManager::getConnection($dbParams['db.read'])
),
'db.write' => new ConnectionHelper(
DriverManager::getConnection(($dbParams['db.write'])
),
'pgsql' => new ConnectionHelper(
DriverManager::getConnection($dbParams['db.pgsql'])
),
'mysql' => new ConnectionHelper(
DriverManager::getConnection(($dbParams['db.mysql'])
),
// and so on...
]));
$cli->addCommand(new RunSqlCommand());
// Continue configuring your cli app, add commands, etc. Regarding #3956, IMO its too much code to solve a very specific use case. Maybe, a new ConnectionProviderHelper could be created, but I still think its unnecessary. I would be happy to add this same modification to the ReservedWordCommand, if we are in agreement. |
BTW I think I mentioned
|
Closing in favor of #3956. |
Summary
This slight modification allows running the
dbal:run-sql
command using an arbitrary database connection, which is specially useful in CLI applications that use multiple database connections. Backwards compatibility is maintained by using the default 'db' value for the input option.The command now includes a
--connection
option, which defaults to 'db'. If specified, a ConnectionHelper is searched for that matches the provided connection name.