You are here

function drush_mongodb_migrate in MongoDB 7

Drush callback; migrate all field data into MongoDB.

File

mongodb_migrate/mongodb_migrate.drush.inc, line 70
Drush 8 plugin for mongodb_migrate.

Code

function drush_mongodb_migrate() {
  $timeout = drush_get_option('timeout', 900);
  $finish_time = FALSE;
  if ($timeout > 0) {
    $finish_time = time() + $timeout;
  }
  $limit = drush_get_option('count', 0);
  foreach (variable_get('mongodb_migrate_types', array()) as $entity_type) {
    $counter = 0;
    $collection = mongodb_collection("migrate.{$entity_type}");
    $entity_info = entity_get_info($entity_type);
    $id_field = $entity_info['entity keys']['id'];
    do {
      $max = iterator_to_array($collection
        ->find(array(), array(
        '_id' => 1,
      ))
        ->sort(array(
        '_id' => -1,
      ))
        ->limit(1));
      $max = $max ? key($max) : 0;
      $query = db_select($entity_info['base table'], 'e')
        ->fields('e', array(
        $id_field,
      ))
        ->condition($id_field, $max, '>')
        ->orderBy($id_field, 'ASC')
        ->range(0, 1);
      $entity_id = $query
        ->execute()
        ->fetchField();
      if ($entity_id) {
        $collection->db
          ->resetError();
        $collection
          ->insert(array(
          '_id' => (int) $entity_id,
          'timestamp' => time(),
        ), mongodb_default_write_options());
        $lastError = $collection->db
          ->lastError();
        if (empty($lastError['err'])) {
          $entities = entity_load($entity_type, array(
            $entity_id,
          ));
          $entity = reset($entities);
          entity_save($entity_type, $entity);
          drush_print("Migrating {$entity_type} {$entity_id}");
        }
        else {
          drush_print("Not migrating {$entity_type} {$entity_id}");
        }
      }
      if ($finish_time && time() > $finish_time) {
        return;
      }
    } while ($entity_id && (!$limit || ++$counter < $limit));
  }
}