use Symfony\Component\DependencyInjection\ContainerInterface;
/**
- * Sources whose data may be fetched via DBTNG.
+ * Sources whose data may be fetched via a database connection.
*
- * 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.
+ * Available configuration keys:
+ * - database_state_key: (optional) Name of the state key which contains an
+ * array with database connection information.
+ * - key: (optional) The database key name. Defaults to 'migrate'.
+ * - target: (optional) The database target name. Defaults to 'default'.
+ * - batch_size: (optional) Number of records to fetch from the database during
+ * each batch. If omitted, all records are fetched in a single query.
+ * - ignore_map: (optional) Source data is joined to the map table by default.
+ * If set to TRUE, the map table will not be joined.
+ *
+ * For other optional configuration keys inherited from the parent class, refer
+ * to \Drupal\migrate\Plugin\migrate\source\SourcePluginBase.
+ *
+ * About the source database determination:
+ * - If the source plugin configuration contains 'database_state_key', its value
+ * is taken as the name of a state key which contains an array with the
+ * database configuration.
+ * - Otherwise, if the source plugin configuration contains 'key', the database
+ * configuration with that name is used.
+ * - If both 'database_state_key' and 'key' are omitted in the source plugin
+ * configuration, the database connection named 'migrate' is used by default.
+ * - If all of the above steps fail, RequirementsException is thrown.
+ *
+ * Drupal Database API supports multiple database connections. The connection
+ * parameters are defined in $databases array in settings.php or
+ * settings.local.php. It is also possible to modify the $databases array in
+ * runtime. For example, Migrate Drupal, which provides the migrations from
+ * Drupal 6 / 7, asks for the source database connection parameters in the UI
+ * and then adds the $databases['migrate'] connection in runtime before the
+ * migrations are executed.
+ *
+ * As described above, the default source database is $databases['migrate']. If
+ * the source plugin needs another source connection, the database connection
+ * parameters should be added to the $databases array as, for instance,
+ * $databases['foo']. The source plugin can then use this connection by setting
+ * 'key' to 'foo' in its configuration.
+ *
+ * For a complete example on migrating data from an SQL source, refer to
+ * https://www.drupal.org/docs/8/api/migrate-api/migrating-data-from-sql-source
+ *
+ * @see https://www.drupal.org/docs/8/api/database-api
+ * @see \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
*/
abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPluginInterface, RequirementsInterface {
*/
public function getDatabase() {
if (!isset($this->database)) {
- // See if the database info is in state - if not, fallback to
- // configuration.
+ // Look first for an explicit state key containing the configuration.
if (isset($this->configuration['database_state_key'])) {
$this->database = $this->setUpDatabase($this->state->get($this->configuration['database_state_key']));
}
+ // Next, use explicit configuration in the source plugin.
+ elseif (isset($this->configuration['key'])) {
+ $this->database = $this->setUpDatabase($this->configuration);
+ }
+ // Next, try falling back to the global state key.
elseif (($fallback_state_key = $this->state->get('migrate.fallback_state_key'))) {
$this->database = $this->setUpDatabase($this->state->get($fallback_state_key));
}
+ // If all else fails, let setUpDatabase() fallback to the 'migrate' key.
else {
- $this->database = $this->setUpDatabase($this->configuration);
+ $this->database = $this->setUpDatabase([]);
}
}
return $this->database;
}
// 2. If we are using high water marks, also include rows above the mark.
// But, include all rows if the high water mark is not set.
- if ($this->getHighWaterProperty() && ($high_water = $this->getHighWater())) {
+ if ($this->getHighWaterProperty()) {
$high_water_field = $this->getHighWaterField();
- $conditions->condition($high_water_field, $high_water, '>');
+ $high_water = $this->getHighWater();
+ if ($high_water) {
+ $conditions->condition($high_water_field, $high_water, '>');
+ $condition_added = TRUE;
+ }
+ // Always sort by the high water field, to ensure that the first run
+ // (before we have a high water value) also has the results in a
+ // consistent order.
$this->query->orderBy($high_water_field);
- $condition_added = TRUE;
}
if ($condition_added) {
$this->query->condition($conditions);
/**
* {@inheritdoc}
*/
- public function count() {
+ public function count($refresh = FALSE) {
return $this->query()->countQuery()->execute()->fetchField();
}