You are here

function follow_update_6001 in Follow 6

Update existing data to the new schema.

File

./follow.install, line 123
Follow module's install and uninstall code.

Code

function follow_update_6001(&$sandbox = NULL) {
  $ret = array();

  // Check to make sure the url column exists before running this update. The only
  // instance I can think of where this might happen is a D5 to D6 upgrade.
  if (db_column_exists('follow_links', 'url')) {
    if (!isset($sandbox['max_lid'])) {
      $sandbox['current_lid'] = 0;
      $sandbox['max_lid'] = db_result(db_query('SELECT MAX(lid) FROM {follow_links}'));
    }
    $result = db_query("SELECT lid, url FROM {follow_links} WHERE lid > %d ORDER BY lid ASC LIMIT 100", $sandbox['current_lid']);
    while ($link = db_fetch_object($result)) {
      $parsed_url = follow_parse_url($link->url);
      $path = db_escape_string($parsed_url['path']);
      $options = serialize($parsed_url['options']);

      // We can't actually pass this directly to update_sql() because of the
      // serialized array options. @see http://j.mp/b9yAxH and http://j.mp/bR3uOe
      $result = db_query("UPDATE {follow_links} SET path = '%s', options = '%s' WHERE lid = %d", $path, $options, $link->lid);
      $ret[] = array(
        'success' => $result !== FALSE,
        'query' => check_plain("UPDATE {follow_links} SET path = '{$path}', options = '{$options}' WHERE lid = {$link->lid}"),
      );
      $sandbox['current_lid'] = $link->lid;
    }
    $ret['#finished'] = empty($sandbox['max_lid']) ? 1 : $sandbox['current_lid'] / $sandbox['max_lid'];
  }
  return $ret;
}