Commit d394fef4 authored by alexpott's avatar alexpott

Issue #2404397 by mikeryan: Support injection of database configuration in SqlBase

parent 37c5acf1
......@@ -15,14 +15,11 @@
/**
* Sources whose data may be fetched via DBTNG.
*
* You can specify a 'target' configuration key to specify the database target
* to use for a specific migration source. You can specify an additional
* database target in settings.php. For example:
* @code
* $databases['migrate']['other_database'] = [ ... ];
* @endcode
* Given that you can specify 'target: other_database' in the 'source' part of
* a migration configuration entity.
* By default, an existing database connection with key 'migrate' and target
* 'default' is used. These may be overridden with explicit 'key' and/or
* 'target' configuration keys. In addition, if the configuration key 'database'
* is present, it is used as a database connection information array to define
* the connection.
*/
abstract class SqlBase extends SourcePluginBase {
......@@ -66,11 +63,22 @@ public function __toString() {
*/
public function getDatabase() {
if (!isset($this->database)) {
$target = 'default';
if (isset($this->configuration['target'])) {
$target = $this->configuration['target'];
}
$this->database = Database::getConnection($target, 'migrate');
else {
$target = 'default';
}
if (isset($this->configuration['key'])) {
$key = $this->configuration['key'];
}
else {
$key = 'migrate';
}
if (isset($this->configuration['database'])) {
Database::addConnectionInfo($key, $target, $this->configuration['database']);
}
$this->database = Database::getConnection($target, $key);
}
return $this->database;
}
......
......@@ -22,14 +22,36 @@ class SqlBaseTest extends MigrateTestBase {
*/
public function testConnectionTypes() {
$sql_base = new TestSqlBase();
// Check the default values.
$this->assertIdentical($sql_base->getDatabase()->getTarget(), 'default');
$this->assertIdentical($sql_base->getDatabase()->getKey(), 'migrate');
$target = 'test_db_target';
$config = array('target' => $target);
$key = 'test_migrate_connection';
$config = array('target' => $target, 'key' => $key);
$sql_base->setConfiguration($config);
Database::addConnectionInfo('migrate', $target, Database::getConnectionInfo('default')['default']);
Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
// Validate we've injected our custom key and target.
$this->assertIdentical($sql_base->getDatabase()->getTarget(), $target);
$this->assertIdentical($sql_base->getDatabase()->getKey(), $key);
// Now test we can have SqlBase create the connection from an info array.
$sql_base = new TestSqlBase();
$target = 'test_db_target2';
$key = 'test_migrate_connection2';
$database = Database::getConnectionInfo('default')['default'];
$config = array('target' => $target, 'key' => $key, 'database' => $database);
$sql_base->setConfiguration($config);
// Call getDatabase() to get the connection defined.
$sql_base->getDatabase();
// Validate the connection has been created with the right values.
$this->assertIdentical(Database::getConnectionInfo($key)[$target], $database);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment