function user_update_8015

Move existing {users}.data into {_d7_users_data} migration table.

Related topics

File

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

Code

function user_update_8015(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;

    // The anonymous user cannot have data, so start with uid 1.
    $sandbox['last'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(uid) FROM {users} WHERE uid > 0')
      ->fetchField();
  }

  // Process 20 user records at a time. E.g., if there are 10 data keys per user
  // record, that leads to an insert query with 200 values.
  $result = db_query_range('SELECT uid, data FROM {users} WHERE uid > :uid ORDER BY uid ASC', 0, 20, array(
    ':uid' => $sandbox['last'],
  ))
    ->fetchAllKeyed();
  $query = db_insert('_d7_users_data')
    ->fields(array(
    'uid',
    'name',
    'value',
  ));
  $has_values = FALSE;
  foreach ($result as $uid => $data) {
    $sandbox['progress']++;
    $sandbox['last'] = $uid;
    if (empty($data)) {
      continue;
    }
    $data = unserialize($data);
    if (!empty($data) && is_array($data)) {
      $has_values = TRUE;
      foreach ($data as $name => $value) {
        $query
          ->values(array(
          'uid' => $uid,
          'name' => $name,
          'value' => serialize($value),
        ));
      }
    }
  }
  if ($has_values) {
    $query
      ->execute();
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}