Problem/Motivation

At the time of this writing, MigrateSqlSource has a getDatabase() method, which tries to build a database connection by pulling information out of its environment and the migration's configuration. This hugely violates best practices (specifically, dependency injection), compromises testability, and introduces a certain amount of brittleness (if the database has been defined incorrectly by the calling code).

Proposed Resolution

Source plugins based on MigrateSqlSource should receive a database connection object in their constructor, or by a setter method.

Remaining Tasks

  • Refactor MigrateSqlSource and its entire descendant tree to accept a connection from the calling code
  • Review the patch
  • Write and test patches for migrate_upgrade and migrate_plus utilizing the new injection.
  • Commit and par-tay

API Changes

Some plugins' constructors will change, but no drastic external-facing API changes.

UI Changes

None.

Comments

phenaproxima created an issue. See original summary.

mikeryan’s picture

So, how precisely would this be used by migrate_upgrade/migrate_plus? Relevant related issue: #2548977: How best to persist source database credentials?.

mikeryan’s picture

Issue summary: View changes
phenaproxima’s picture

One possibility is to use specialized factories:

/** @var \Drupal\Core\Database\Connection $database */
$database = get_source_db_connection(); // Build the source connection somehow

/** @var \Drupal\Component\Plugin\Factory\FactoryInterface */
$factory = new MigrateSqlSourceFactory();
$factory->setSourceDatabase($database);

$migration = Migration::load('my_migration');
$migration->setSourceFactory($factory);

// Calls the factory, which injects the proper DB connection. External API
// remains the same.
$migration->getSourcePlugin();

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mikeryan’s picture

Status: Active » Closed (duplicate)
Related issues: +#2681869: Provide clean way to merge configuration into migration plugins