I'm using D10, drush 12, ddev.
In settings.local.php I have:
$databases['migrate']['default'] = array (
'database' => 'd7',
'username' => 'db',
'password' => 'db',
'host' => 'db',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
In the code when I do:
$db = Database::getAllConnectionInfo();
the var_dump shows 'migrate' being part of the array:
array(2) {
["default"]=>
array(1) {
["default"]=>
array(11) {
["database"]=>
string(2) "db"
["username"]=>
string(2) "db"
["password"]=>
string(2) "db"
["host"]=>
string(2) "db"
["port"]=>
int(3306)
["driver"]=>
string(5) "mysql"
["prefix"]=>
string(0) ""
["namespace"]=>
string(34) "Drupal\mysql\Driver\Database\mysql"
["autoload"]=>
string(45) "core/modules/mysql/src/Driver/Database/mysql/"
["pdo"]=>
array(6) {
[3]=>
int(2)
[1000]=>
bool(true)
[1005]=>
bool(true)
[20]=>
bool(true)
[1013]=>
bool(false)
[17]=>
bool(true)
}
["init_commands"]=>
array(1) {
["sql_mode"]=>
string(33) "SET sql_mode = 'ANSI,TRADITIONAL'"
}
}
}
["migrate"]=>
array(1) {
["default"]=>
array(9) {
["database"]=>
string(2) "d7"
["username"]=>
string(2) "db"
["password"]=>
string(2) "db"
["host"]=>
string(2) "db"
["port"]=>
string(4) "3306"
["driver"]=>
string(5) "mysql"
["prefix"]=>
string(0) ""
["namespace"]=>
string(34) "Drupal\mysql\Driver\Database\mysql"
["autoload"]=>
string(45) "core/modules/mysql/src/Driver/Database/mysql/"
}
}
}
but when I try to create a connection, with
return Database::getConnection('migrate');
I receive:
In ExceptionHandler.php line 46:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.node_type' doesn't exist: SELECT `name`,`type`,`description` from node_type ORDER BY `name`; Array
(
)
In StatementWrapperIterator.php line 110:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.node_type' doesn't exist
and dumping the connection variable shows a connection to the default db but not to the migrate db.
Let me add context and more code. I began with a working module for D10 providing a drush 12 custom command. At that point, the command made no use of a database. I simply wanted to add an option to the command that makes use of a database. I mention this for two reasons: to point out that the structure of using a drush custom command had already bene in place and working, and to point out that this is a drush command, in case it could be how drush interacts with the database(s) that makes a difference.
I had been bootstrapping drush to the configuration level. In adding the new feature I upped that to the database level:
#[CLI\Bootstrap(level: DrupalBootLevels::DATABASE)]
I added the following use entries:
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
Once I have determined that the drush command has been entered with the option request to use the database, I call the method to establish the connection and do a simple query (I prefer to added new functionality in steps...easier to test).
if ($options['db']) {
$db = $this->getD7DbConnection();
if (!$db) {
$this->logger()->error(dt("There is no migrate database configured in the site settings."));
return NULL;
}
$file = Drupal::service('extension.path.resolver')->getPath('module', 'mymodule') . '/data/items.db.yml';
return $this->scanDb($db, $file);
}
This method succeeds. It contains the db connection info showing two db's, default and migrate, shown above in the var_dump:
private function getD7DbConnection() {
$db = Database::getAllConnectionInfo();
if (isset($db['migrate'])) {
return Database::getConnection('migrate');
}
return FALSE;
}
This method then fails. I threw a simple query into it for now just to prove or disprove a connection:
private function scanDb(Connection $db, $file) {
$results = $db->query('SELECT * from node_type ORDER BY name;');
return $results;
}
And that's all there is. At this point I'm simply attempting to connect to the db, as a result of the request via drush option, and do a simple query.