You are here

function drush_mongodb_migrate_prepare in MongoDB 7

Drush callback; prepares for migration by updating {field_config}.

File

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

Code

function drush_mongodb_migrate_prepare() {
  $fields = db_query('SELECT field_name FROM {field_config} WHERE storage_type = :storage_type OR data LIKE :mongodb_migrate', array(
    ':storage_type' => 'field_sql_storage',
    ':mongodb_migrate' => '%mongodb_migrate%',
  ))
    ->fetchCol();
  foreach ($fields as $key => $field_name) {
    $field = field_read_field($field_name);
    if (!db_table_exists(_field_sql_storage_tablename($field))) {
      unset($fields[$key]);
      continue;
    }

    // Updating storage is forbidden by field_update_field(). Bugger. Write our
    // own, simplified update.
    $field['settings']['mongodb_migrate'] = 1;
    $field['storage_type'] = 'mongodb_field_storage';
    $field['storage_module'] = 'mongodb_field_storage';
    $field['storage']['type'] = 'mongodb_field_storage';
    $field['storage']['module'] = 'mongodb_field_storage';
    $data = $field;
    unset($data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['cardinality'], $data['active'], $data['deleted']);
    $field['data'] = $data;
    drupal_write_record('field_config', $field, array(
      'id',
    ));
  }
  $info = field_info_fields();
  $all_entity_types = array();
  foreach ($fields as $field_name) {
    $field = $info[$field_name];
    db_update(_field_sql_storage_tablename($field))
      ->fields(array(
      'deleted' => 0,
    ))
      ->condition('deleted', 2)
      ->execute();
    foreach ($field['bundles'] as $entity_type => $bundles) {
      mongodb_collection("migrate.{$entity_type}")
        ->drop();
      $all_entity_types[] = $entity_type;
    }
  }
  variable_set('mongodb_migrate_fields', array_flip($fields));
  variable_set('mongodb_migrate_types', array_unique($all_entity_types));
  field_cache_clear();
}