3 namespace Drupal\migrate\Plugin\migrate\id_map;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Field\BaseFieldDefinition;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\Core\Plugin\PluginBase;
9 use Drupal\migrate\MigrateMessage;
10 use Drupal\migrate\Audit\HighestIdInterface;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\Event\MigrateIdMapMessageEvent;
13 use Drupal\migrate\MigrateException;
14 use Drupal\migrate\MigrateMessageInterface;
15 use Drupal\migrate\Plugin\MigrateIdMapInterface;
16 use Drupal\migrate\Row;
17 use Drupal\migrate\Event\MigrateEvents;
18 use Drupal\migrate\Event\MigrateMapSaveEvent;
19 use Drupal\migrate\Event\MigrateMapDeleteEvent;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24 * Defines the sql based ID map implementation.
26 * It creates one map and one message table per migration entity to store the
27 * relevant information.
31 class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryPluginInterface, HighestIdInterface {
34 * Column name of hashed source id values.
36 const SOURCE_IDS_HASH = 'source_ids_hash';
39 * An event dispatcher instance to use for map events.
41 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
43 protected $eventDispatcher;
46 * The migration map table name.
50 protected $mapTableName;
53 * The message table name.
57 protected $messageTableName;
60 * The migrate message service.
62 * @var \Drupal\migrate\MigrateMessageInterface
67 * The database connection for the map/message tables on the destination.
69 * @var \Drupal\Core\Database\Connection
76 * @var \Drupal\Core\Database\Query\SelectInterface
81 * The migration being done.
83 * @var \Drupal\migrate\Plugin\MigrationInterface
88 * The source ID fields.
92 protected $sourceIdFields;
95 * The destination ID fields.
99 protected $destinationIdFields;
102 * Whether the plugin is already initialized.
106 protected $initialized;
113 protected $result = NULL;
116 * The source identifiers.
120 protected $sourceIds = [];
123 * The destination identifiers.
127 protected $destinationIds = [];
134 protected $currentRow = NULL;
141 protected $currentKey = [];
144 * Constructs an SQL object.
146 * Sets up the tables and builds the maps,
148 * @param array $configuration
150 * @param string $plugin_id
151 * The plugin ID for the migration process to do.
152 * @param mixed $plugin_definition
153 * The configuration for the plugin.
154 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
155 * The migration to do.
156 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
157 * The event dispatcher.
159 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) {
160 parent::__construct($configuration, $plugin_id, $plugin_definition);
161 $this->migration = $migration;
162 $this->eventDispatcher = $event_dispatcher;
163 $this->message = new MigrateMessage();
169 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
175 $container->get('event_dispatcher')
180 * Retrieves the hash of the source identifier values.
184 * @param array $source_id_values
185 * The source identifiers
188 * An hash containing the hashed values of the source identifiers.
190 public function getSourceIDsHash(array $source_id_values) {
191 // When looking up the destination ID we require an array with both the
192 // source key and value, e.g. ['nid' => 41]. In this case, $source_id_values
193 // need to be ordered the same order as $this->sourceIdFields().
194 // However, the Migration process plugin doesn't currently have a way to get
195 // the source key so we presume the values have been passed through in the
197 if (!isset($source_id_values[0])) {
198 $source_id_values_keyed = [];
199 foreach ($this->sourceIdFields() as $field_name => $source_id) {
200 $source_id_values_keyed[] = $source_id_values[$field_name];
202 $source_id_values = $source_id_values_keyed;
204 return hash('sha256', serialize(array_map('strval', $source_id_values)));
208 * The source ID fields.
211 * The source ID fields.
213 protected function sourceIdFields() {
214 if (!isset($this->sourceIdFields)) {
215 // Build the source and destination identifier maps.
216 $this->sourceIdFields = [];
218 foreach ($this->migration->getSourcePlugin()->getIds() as $field => $schema) {
219 $this->sourceIdFields[$field] = 'sourceid' . $count++;
222 return $this->sourceIdFields;
226 * The destination ID fields.
229 * The destination ID fields.
231 protected function destinationIdFields() {
232 if (!isset($this->destinationIdFields)) {
233 $this->destinationIdFields = [];
235 foreach ($this->migration->getDestinationPlugin()->getIds() as $field => $schema) {
236 $this->destinationIdFields[$field] = 'destid' . $count++;
239 return $this->destinationIdFields;
243 * The name of the database map table.
246 * The map table name.
248 public function mapTableName() {
250 return $this->mapTableName;
254 * The name of the database message table.
257 * The message table name.
259 public function messageTableName() {
261 return $this->messageTableName;
265 * Get the fully qualified map table name.
268 * The fully qualified map table name.
270 public function getQualifiedMapTableName() {
271 return $this->getDatabase()->getFullQualifiedTableName($this->mapTableName);
275 * Gets the database connection.
277 * @return \Drupal\Core\Database\Connection
278 * The database connection object.
280 public function getDatabase() {
281 if (!isset($this->database)) {
282 $this->database = \Drupal::database();
285 return $this->database;
289 * Initialize the plugin.
291 protected function init() {
292 if (!$this->initialized) {
293 $this->initialized = TRUE;
294 // Default generated table names, limited to 63 characters.
295 $machine_name = str_replace(':', '__', $this->migration->id());
296 $prefix_length = strlen($this->getDatabase()->tablePrefix());
297 $this->mapTableName = 'migrate_map_' . Unicode::strtolower($machine_name);
298 $this->mapTableName = Unicode::substr($this->mapTableName, 0, 63 - $prefix_length);
299 $this->messageTableName = 'migrate_message_' . Unicode::strtolower($machine_name);
300 $this->messageTableName = Unicode::substr($this->messageTableName, 0, 63 - $prefix_length);
301 $this->ensureTables();
308 public function setMessage(MigrateMessageInterface $message) {
309 $this->message = $message;
313 * Create the map and message tables if they don't already exist.
315 protected function ensureTables() {
316 if (!$this->getDatabase()->schema()->tableExists($this->mapTableName)) {
317 // Generate appropriate schema info for the map and message tables,
318 // and map from the source field names to the map/msg field names.
320 $source_id_schema = [];
322 foreach ($this->migration->getSourcePlugin()->getIds() as $id_definition) {
323 $mapkey = 'sourceid' . $count++;
324 $indexes['source'][] = $mapkey;
325 $source_id_schema[$mapkey] = $this->getFieldSchema($id_definition);
326 $source_id_schema[$mapkey]['not null'] = TRUE;
329 $source_ids_hash[static::SOURCE_IDS_HASH] = [
333 'description' => 'Hash of source ids. Used as primary key',
335 $fields = $source_ids_hash + $source_id_schema;
337 // Add destination identifiers to map table.
338 // @todo How do we discover the destination schema?
340 foreach ($this->migration->getDestinationPlugin()->getIds() as $id_definition) {
341 // Allow dest identifier fields to be NULL (for IGNORED/FAILED cases).
342 $mapkey = 'destid' . $count++;
343 $fields[$mapkey] = $this->getFieldSchema($id_definition);
344 $fields[$mapkey]['not null'] = FALSE;
346 $fields['source_row_status'] = [
351 'default' => MigrateIdMapInterface::STATUS_IMPORTED,
352 'description' => 'Indicates current status of the source row',
354 $fields['rollback_action'] = [
359 'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
360 'description' => 'Flag indicating what to do for this item on rollback',
362 $fields['last_imported'] = [
367 'description' => 'UNIX timestamp of the last time this row was imported',
373 'description' => 'Hash of source row data, for detecting changes',
376 'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
378 'primary key' => [static::SOURCE_IDS_HASH],
379 'indexes' => $indexes,
381 $this->getDatabase()->schema()->createTable($this->mapTableName, $schema);
383 // Now do the message table.
384 if (!$this->getDatabase()->schema()->tableExists($this->messageTableName())) {
391 $fields += $source_ids_hash;
399 $fields['message'] = [
405 'description' => 'Messages generated during a migration process',
407 'primary key' => ['msgid'],
409 $this->getDatabase()->schema()->createTable($this->messageTableName(), $schema);
413 // Add any missing columns to the map table.
414 if (!$this->getDatabase()->schema()->fieldExists($this->mapTableName,
415 'rollback_action')) {
416 $this->getDatabase()->schema()->addField($this->mapTableName, 'rollback_action',
423 'description' => 'Flag indicating what to do for this item on rollback',
427 if (!$this->getDatabase()->schema()->fieldExists($this->mapTableName, 'hash')) {
428 $this->getDatabase()->schema()->addField($this->mapTableName, 'hash',
433 'description' => 'Hash of source row data, for detecting changes',
437 if (!$this->getDatabase()->schema()->fieldExists($this->mapTableName, static::SOURCE_IDS_HASH)) {
438 $this->getDatabase()->schema()->addField($this->mapTableName, static::SOURCE_IDS_HASH, [
442 'description' => 'Hash of source ids. Used as primary key',
449 * Creates schema from an ID definition.
451 * @param array $id_definition
452 * The definition of the field having the structure as the items returned by
453 * MigrateSourceInterface or MigrateDestinationInterface::getIds().
456 * The database schema definition.
458 * @see \Drupal\migrate\Plugin\MigrateSourceInterface::getIds()
459 * @see \Drupal\migrate\Plugin\MigrateDestinationInterface::getIds()
461 protected function getFieldSchema(array $id_definition) {
462 $type_parts = explode('.', $id_definition['type']);
463 if (count($type_parts) == 1) {
464 $type_parts[] = 'value';
466 unset($id_definition['type']);
468 // Get the field storage definition.
469 $definition = BaseFieldDefinition::create($type_parts[0]);
471 // Get a list of setting keys belonging strictly to the field definition.
472 $default_field_settings = $definition->getSettings();
473 // Separate field definition settings from custom settings. Custom settings
474 // are settings passed in $id_definition that are not part of field storage
475 // definition settings.
476 $field_settings = array_intersect_key($id_definition, $default_field_settings);
477 $custom_settings = array_diff_key($id_definition, $default_field_settings);
479 // Resolve schema from field storage definition settings.
480 $schema = $definition
481 ->setSettings($field_settings)
482 ->getColumns()[$type_parts[1]];
484 // Merge back custom settings.
485 return $schema + $custom_settings;
491 public function getRowBySource(array $source_id_values) {
492 $query = $this->getDatabase()->select($this->mapTableName(), 'map')
494 $query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
495 $result = $query->execute();
496 return $result->fetchAssoc();
502 public function getRowByDestination(array $destination_id_values) {
503 $query = $this->getDatabase()->select($this->mapTableName(), 'map')
505 foreach ($this->destinationIdFields() as $field_name => $destination_id) {
506 $query->condition("map.$destination_id", $destination_id_values[$field_name], '=');
508 $result = $query->execute();
509 return $result->fetchAssoc();
515 public function getRowsNeedingUpdate($count) {
517 $result = $this->getDatabase()->select($this->mapTableName(), 'map')
519 ->condition('source_row_status', MigrateIdMapInterface::STATUS_NEEDS_UPDATE)
522 foreach ($result as $row) {
531 public function lookupSourceID(array $destination_id_values) {
532 $source_id_fields = $this->sourceIdFields();
533 $query = $this->getDatabase()->select($this->mapTableName(), 'map');
534 foreach ($source_id_fields as $source_field_name => $idmap_field_name) {
535 $query->addField('map', $idmap_field_name, $source_field_name);
537 foreach ($this->destinationIdFields() as $field_name => $destination_id) {
538 $query->condition("map.$destination_id", $destination_id_values[$field_name], '=');
540 $result = $query->execute();
541 return $result->fetchAssoc() ?: [];
547 public function lookupDestinationId(array $source_id_values) {
548 $results = $this->lookupDestinationIds($source_id_values);
549 return $results ? reset($results) : [];
555 public function lookupDestinationIds(array $source_id_values) {
556 if (empty($source_id_values)) {
560 // Canonicalize the keys into a hash of DB-field => value.
561 $is_associative = !isset($source_id_values[0]);
563 foreach ($this->sourceIdFields() as $field_name => $db_field) {
564 if ($is_associative) {
565 // Associative $source_id_values can have fields out of order.
566 if (isset($source_id_values[$field_name])) {
567 $conditions[$db_field] = $source_id_values[$field_name];
568 unset($source_id_values[$field_name]);
572 // For non-associative $source_id_values, we assume they're the first
574 if (empty($source_id_values)) {
577 $conditions[$db_field] = array_shift($source_id_values);
581 if (!empty($source_id_values)) {
582 throw new MigrateException("Extra unknown items in source IDs");
585 $query = $this->getDatabase()->select($this->mapTableName(), 'map')
586 ->fields('map', $this->destinationIdFields());
587 if (count($this->sourceIdFields()) === count($conditions)) {
588 // Optimization: Use the primary key.
589 $query->condition(self::SOURCE_IDS_HASH, $this->getSourceIDsHash(array_values($conditions)));
592 foreach ($conditions as $db_field => $value) {
593 $query->condition($db_field, $value);
597 return $query->execute()->fetchAll(\PDO::FETCH_NUM);
603 public function saveIdMapping(Row $row, array $destination_id_values, $source_row_status = MigrateIdMapInterface::STATUS_IMPORTED, $rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE) {
604 // Construct the source key.
605 $source_id_values = $row->getSourceIdValues();
606 // Construct the source key and initialize to empty variable keys.
608 foreach ($this->sourceIdFields() as $field_name => $key_name) {
609 // A NULL key value is usually an indication of a problem.
610 if (!isset($source_id_values[$field_name])) {
611 $this->message->display($this->t(
612 'Did not save to map table due to NULL value for key field @field',
613 ['@field' => $field_name]), 'error');
616 $fields[$key_name] = $source_id_values[$field_name];
624 'source_row_status' => (int) $source_row_status,
625 'rollback_action' => (int) $rollback_action,
626 'hash' => $row->getHash(),
629 foreach ($destination_id_values as $dest_id) {
630 $fields['destid' . ++$count] = $dest_id;
632 if ($count && $count != count($this->destinationIdFields())) {
633 $this->message->display(t('Could not save to map table due to missing destination id values'), 'error');
636 if ($this->migration->getTrackLastImported()) {
637 $fields['last_imported'] = time();
639 $keys = [static::SOURCE_IDS_HASH => $this->getSourceIDsHash($source_id_values)];
640 // Notify anyone listening of the map row we're about to save.
641 $this->eventDispatcher->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $fields));
642 $this->getDatabase()->merge($this->mapTableName())
651 public function saveMessage(array $source_id_values, $message, $level = MigrationInterface::MESSAGE_ERROR) {
652 foreach ($this->sourceIdFields() as $field_name => $source_id) {
653 // If any key value is not set, we can't save.
654 if (!isset($source_id_values[$field_name])) {
658 $fields[static::SOURCE_IDS_HASH] = $this->getSourceIDsHash($source_id_values);
659 $fields['level'] = $level;
660 $fields['message'] = $message;
661 $this->getDatabase()->insert($this->messageTableName())
665 // Notify anyone listening of the message we've saved.
666 $this->eventDispatcher->dispatch(MigrateEvents::IDMAP_MESSAGE,
667 new MigrateIdMapMessageEvent($this->migration, $source_id_values, $message, $level));
673 public function getMessageIterator(array $source_id_values = [], $level = NULL) {
674 $query = $this->getDatabase()->select($this->messageTableName(), 'msg')
676 if ($source_id_values) {
677 $query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
681 $query->condition('level', $level);
683 return $query->execute();
689 public function prepareUpdate() {
690 $this->getDatabase()->update($this->mapTableName())
691 ->fields(['source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE])
698 public function processedCount() {
699 return (int) $this->getDatabase()->select($this->mapTableName())
708 public function importedCount() {
709 return (int) $this->getDatabase()->select($this->mapTableName())
710 ->condition('source_row_status', [MigrateIdMapInterface::STATUS_IMPORTED, MigrateIdMapInterface::STATUS_NEEDS_UPDATE], 'IN')
719 public function updateCount() {
720 return $this->countHelper(MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
726 public function errorCount() {
727 return $this->countHelper(MigrateIdMapInterface::STATUS_FAILED);
733 public function messageCount() {
734 return $this->countHelper(NULL, $this->messageTableName());
738 * Counts records in a table.
741 * An integer for the source_row_status column.
742 * @param string $table
743 * (optional) The table to work. Defaults to NULL.
746 * The number of records.
748 protected function countHelper($status, $table = NULL) {
749 $query = $this->getDatabase()->select($table ?: $this->mapTableName());
750 if (isset($status)) {
751 $query->condition('source_row_status', $status);
753 return (int) $query->countQuery()->execute()->fetchField();
759 public function delete(array $source_id_values, $messages_only = FALSE) {
760 if (empty($source_id_values)) {
761 throw new MigrateException('Without source identifier values it is impossible to find the row to delete.');
764 if (!$messages_only) {
765 $map_query = $this->getDatabase()->delete($this->mapTableName());
766 $map_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
767 // Notify anyone listening of the map row we're about to delete.
768 $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
769 $map_query->execute();
771 $message_query = $this->getDatabase()->delete($this->messageTableName());
772 $message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
773 $message_query->execute();
779 public function deleteDestination(array $destination_id_values) {
780 $map_query = $this->getDatabase()->delete($this->mapTableName());
781 $message_query = $this->getDatabase()->delete($this->messageTableName());
782 $source_id_values = $this->lookupSourceID($destination_id_values);
783 if (!empty($source_id_values)) {
784 foreach ($this->destinationIdFields() as $field_name => $destination_id) {
785 $map_query->condition($destination_id, $destination_id_values[$field_name]);
787 // Notify anyone listening of the map row we're about to delete.
788 $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
789 $map_query->execute();
791 $message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
792 $message_query->execute();
799 public function setUpdate(array $source_id_values) {
800 if (empty($source_id_values)) {
801 throw new MigrateException('No source identifiers provided to update.');
803 $query = $this->getDatabase()
804 ->update($this->mapTableName())
805 ->fields(['source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE]);
807 foreach ($this->sourceIdFields() as $field_name => $source_id) {
808 $query->condition($source_id, $source_id_values[$field_name]);
816 public function clearMessages() {
817 $this->getDatabase()->truncate($this->messageTableName())->execute();
823 public function destroy() {
824 $this->getDatabase()->schema()->dropTable($this->mapTableName());
825 $this->getDatabase()->schema()->dropTable($this->messageTableName());
829 * Implementation of \Iterator::rewind().
831 * This is called before beginning a foreach loop.
833 public function rewind() {
834 $this->currentRow = NULL;
836 foreach ($this->sourceIdFields() as $field) {
839 foreach ($this->destinationIdFields() as $field) {
842 $this->result = $this->getDatabase()->select($this->mapTableName(), 'map')
843 ->fields('map', $fields)
850 * Implementation of \Iterator::current().
852 * This is called when entering a loop iteration, returning the current row.
854 public function current() {
855 return $this->currentRow;
859 * Implementation of \Iterator::key().
861 * This is called when entering a loop iteration, returning the key of the
862 * current row. It must be a scalar - we will serialize to fulfill the
863 * requirement, but using getCurrentKey() is preferable.
865 public function key() {
866 return serialize($this->currentKey);
872 public function currentDestination() {
873 if ($this->valid()) {
875 foreach ($this->destinationIdFields() as $destination_field_name => $idmap_field_name) {
876 if (!is_null($this->currentRow[$idmap_field_name])) {
877 $result[$destination_field_name] = $this->currentRow[$idmap_field_name];
890 public function currentSource() {
891 if ($this->valid()) {
893 foreach ($this->sourceIdFields() as $field_name => $source_id) {
894 $result[$field_name] = $this->currentKey[$source_id];
904 * Implementation of \Iterator::next().
906 * This is called at the bottom of the loop implicitly, as well as explicitly
909 public function next() {
910 $this->currentRow = $this->result->fetchAssoc();
911 $this->currentKey = [];
912 if ($this->currentRow) {
913 foreach ($this->sourceIdFields() as $map_field) {
914 $this->currentKey[$map_field] = $this->currentRow[$map_field];
915 // Leave only destination fields.
916 unset($this->currentRow[$map_field]);
922 * Implementation of \Iterator::valid().
924 * This is called at the top of the loop, returning TRUE to process the loop
925 * and FALSE to terminate it.
927 public function valid() {
928 return $this->currentRow !== FALSE;
932 * Returns the migration plugin manager.
934 * @todo Inject as a dependency in https://www.drupal.org/node/2919158.
936 * @return \Drupal\migrate\Plugin\MigrationPluginManagerInterface
937 * The migration plugin manager.
939 protected function getMigrationPluginManager() {
940 return \Drupal::service('plugin.manager.migration');
946 public function getHighestId() {
948 $this->migration->getDestinationPlugin()->getIds(),
949 function (array $id) {
950 if ($id['type'] !== 'integer') {
951 throw new \LogicException('Cannot determine the highest migrated ID without an integer ID column');
956 // List of mapping tables to look in for the highest ID.
958 $this->migration->id() => $this->mapTableName(),
961 // If there's a bundle, it means we have a derived migration and we need to
962 // find all the mapping tables from the related derived migrations.
963 if ($base_id = substr($this->migration->id(), 0, strpos($this->migration->id(), static::DERIVATIVE_SEPARATOR))) {
964 $migration_manager = $this->getMigrationPluginManager();
965 $migrations = $migration_manager->getDefinitions();
966 foreach ($migrations as $migration_id => $migration) {
967 if ($migration['id'] === $base_id) {
968 // Get this derived migration's mapping table and add it to the list
969 // of mapping tables to look in for the highest ID.
970 $stub = $migration_manager->createInstance($migration_id);
971 $map_tables[$migration_id] = $stub->getIdMap()->mapTableName();
976 // Get the highest id from the list of map tables.
978 foreach ($map_tables as $map_table) {
979 if (!$this->getDatabase()->schema()->tableExists($map_table)) {
983 $query = $this->getDatabase()->select($map_table, 'map')
984 ->fields('map', $this->destinationIdFields())
986 foreach (array_values($this->destinationIdFields()) as $order_field) {
987 $query->orderBy($order_field, 'DESC');
989 $ids[] = $query->execute()->fetchField();
992 // Return the highest of all the mapped IDs.
993 return (int) max($ids);