function node_update_8017

Upgrade node schema to the standard entity SQL schema: migrate data.

Related topics

File

drupal/core/modules/node/node.install, line 1092
Install, update and uninstall functions for the node module.

Code

function node_update_8017(&$sandbox) {
  if (!isset($sandbox['progress'])) {

    // Initialize the batch status.
    $sandbox['progress'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(vid) FROM {node_revision}')
      ->fetchField();
  }

  // Prepare the new records.
  $queries = array();
  $schema = _node_update_8016_schema();
  $result = db_query_range('SELECT nr.*, nr.timestamp AS revision_timestamp, nr.uid as revision_uid, 1 AS default_langcode, n.langcode, n.vid = nr.vid AS default_revision, n.uid, n.changed, n.created, n.type FROM {node_revision} nr JOIN {node} n ON nr.nid = n.nid ORDER BY nr.nid ASC, nr.vid ASC', $sandbox['progress'], 50);
  foreach ($result as $row) {
    $sandbox['progress']++;
    foreach ($schema as $table => $table_schema) {

      // We need to store the data table record only when dealing with the
      // default revision.
      if ($row->default_revision || $table == 'node_field_revision') {
        $fields = array_keys($table_schema['fields']);
        $record = array();
        foreach ($fields as $field) {
          if (isset($row->{$field})) {
            $record[$field] = $row->{$field};
          }
        }
        if (!isset($queries[$table])) {
          $queries[$table] = db_insert($table)
            ->fields($fields);
        }
        $queries[$table]
          ->values($record);
      }
    }
  }

  // Store the new records.
  foreach ($queries as $query) {
    $query
      ->execute();
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}