You are here

function location_update_6301 in Location 7.4

Same name and namespace in other branches
  1. 6.3 location.install \location_update_6301()
  2. 7.5 location.install \location_update_6301()
  3. 7.3 location.install \location_update_6301()

Drupal 6 location 3.x update.

File

./location.install, line 791
Installation / uninstallation routines.

Code

function location_update_6301() {
  $t = get_t();
  drupal_set_message($t('Note: Location.module update 6301 will generate several warnings/failures regarding indexes and primary keys if you are upgrading from one of the 6.x test releases. These warnings can be safely disregarded in this case.'));
  $ret = array();

  // Update cache table.
  db_drop_table($ret, 'cache_location');
  $schema['cache_location'] = array(
    'description' => t('Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.'),
    'fields' => array(
      'cid' => array(
        'description' => t('Primary Key: Unique cache ID.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'description' => t('A collection of data to cache.'),
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'expire' => array(
        'description' => t('A Unix timestamp indicating when the cache entry should expire, or 0 for never.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => t('A Unix timestamp indicating when the cache entry was created.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'headers' => array(
        'description' => t('Any custom HTTP headers to be added to cached data.'),
        'type' => 'text',
        'not null' => FALSE,
      ),
      'serialized' => array(
        'description' => t('A flag to indicate whether content is serialized (1) or not (0).'),
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'expire' => array(
        'expire',
      ),
    ),
    'primary key' => array(
      'cid',
    ),
  );
  db_create_table($ret, 'cache_location', $schema['cache_location']);

  // LID 0 causes all sorts of issues, and will break our update routine
  // unless we handle it beforehand.
  // Since we're so nice, we're gonna renumber it for the user.
  if (db_result(db_query('SELECT COUNT(*) FROM {location} WHERE lid = 0'))) {
    $lid = 1 + db_result(db_query('SELECT MAX(lid) FROM {location}'));
    drupal_set_message(t('Note: A location with lid 0 was found in your database. It has been moved to lid %lid. You may wish to verify it manually, as lid 0 is usually a corrupt entry.', array(
      '%lid' => $lid,
    )));

    // $lid is safe to inject here.
    $ret[] = update_sql("UPDATE {location} SET lid = {$lid} WHERE lid = 0");
    $ret[] = update_sql("UPDATE {location_instance} SET lid = {$lid} WHERE lid = 0");
  }

  // Field changes
  // {location}
  // {location}.lid -- Becomes a serial.
  db_drop_primary_key($ret, 'location');
  db_change_field($ret, 'location', 'lid', 'lid', array(
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ), array(
    'primary key' => array(
      'lid',
    ),
  ));

  // (The rest of the changes to this table were moved to update 6302 due to a bug.)
  // {location_instance}
  // Fix oddly named indexes -- Was using the postgresql method for both.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_drop_index($ret, 'location_instance', '{location_instance}_nid_idx');
      db_drop_index($ret, 'location_instance', '{location_instance}_vid_idx');
      db_drop_index($ret, 'location_instance', '{location_instance}_uid_idx');
      db_drop_index($ret, 'location_instance', '{location_instance}_genid_idx');
      db_drop_index($ret, 'location_instance', '{location_instance}_lid_idx');
      break;
    case 'pgsql':
      db_drop_index($ret, 'location_instance', 'nid');
      db_drop_index($ret, 'location_instance', 'vid');
      db_drop_index($ret, 'location_instance', 'uid');
      db_drop_index($ret, 'location_instance', 'genid');
      db_drop_index($ret, 'location_instance', 'lid');
  }

  // Fill in nulls.
  $ret[] = update_sql('UPDATE {location_instance} SET nid = 0 WHERE nid IS NULL');
  $ret[] = update_sql('UPDATE {location_instance} SET vid = 0 WHERE vid IS NULL');
  $ret[] = update_sql('UPDATE {location_instance} SET uid = 0 WHERE uid IS NULL');
  $ret[] = update_sql("UPDATE {location_instance} SET genid = '' WHERE genid IS NULL");

  // {location_instance}.nid
  db_change_field($ret, 'location_instance', 'nid', 'nid', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));

  // {location_instance}.vid
  db_change_field($ret, 'location_instance', 'vid', 'vid', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));

  // {location_instance}.uid
  db_change_field($ret, 'location_instance', 'uid', 'uid', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));

  // {location_instance}.genid
  db_change_field($ret, 'location_instance', 'genid', 'genid', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location_instance}.lid
  db_change_field($ret, 'location_instance', 'lid', 'lid', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));

  // Readd indexes.
  db_add_index($ret, 'location_instance', 'nid', array(
    'nid',
  ));
  db_add_index($ret, 'location_instance', 'vid', array(
    'vid',
  ));
  db_add_index($ret, 'location_instance', 'uid', array(
    'uid',
  ));
  db_add_index($ret, 'location_instance', 'genid', array(
    'genid',
  ));
  db_add_index($ret, 'location_instance', 'lid', array(
    'lid',
  ));

  // {zipcodes}
  // Drop primary key.
  db_drop_primary_key($ret, 'zipcodes');
  return $ret;
}