You are here

function location_update_6301 in Location 7.3

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.4 location.install \location_update_6301()

Drupal 6 location 3.x update.

File

./location.install, line 808
Install, update and uninstall functions for the location module.

Code

function location_update_6301() {
  $message = '';

  // Update cache table.
  db_drop_table('cache_location');
  $schema['cache_location'] = array(
    'description' => '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' => 'Primary Key: Unique cache ID.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'description' => 'A collection of data to cache.',
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'expire' => array(
        'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'A Unix timestamp indicating when the cache entry was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'headers' => array(
        'description' => 'Any custom HTTP headers to be added to cached data.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'serialized' => array(
        'description' => '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('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.
  $has_rows = (bool) db_query_range('SELECT 1 FROM {location} WHERE lid = 0', 0, 1)
    ->fetchField();
  if ($has_rows) {
    $lid = 1 + db_query('SELECT MAX(lid) FROM {location}')
      ->fetchField();
    $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,
    ));
    db_update('location')
      ->fields(array(
      'lid' => $lid,
    ))
      ->condition('lid', 0)
      ->execute();
    db_update('location_instance')
      ->fields(array(
      'lid' => $lid,
    ))
      ->condition('lid', 0)
      ->execute();
  }

  // Field changes.
  // {location}.lid -- Becomes a serial.
  db_drop_primary_key('location');
  db_change_field('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.
  db_drop_index('location_instance', '{location_instance}_nid_idx');
  db_drop_index('location_instance', '{location_instance}_vid_idx');
  db_drop_index('location_instance', '{location_instance}_uid_idx');
  db_drop_index('location_instance', '{location_instance}_genid_idx');
  db_drop_index('location_instance', '{location_instance}_lid_idx');
  db_drop_index('location_instance', 'nid');
  db_drop_index('location_instance', 'vid');
  db_drop_index('location_instance', 'uid');
  db_drop_index('location_instance', 'genid');
  db_drop_index('location_instance', 'lid');

  // Fill in nulls.
  db_update('location_instance')
    ->fields(array(
    'nid' => 0,
  ))
    ->isNull('nid')
    ->execute();
  db_update('location_instance')
    ->fields(array(
    'vid' => 0,
  ))
    ->isNull('vid')
    ->execute();
  db_update('location_instance')
    ->fields(array(
    'uid' => 0,
  ))
    ->isNull('uid')
    ->execute();
  db_update('location_instance')
    ->fields(array(
    'genid' => 0,
  ))
    ->isNull('genid')
    ->execute();

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

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

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

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

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

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

  // {zipcodes}
  // Drop primary key.
  db_drop_primary_key('zipcodes');
  if ($message) {
    $message .= '<br /><br />';
  }
  return $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.');
}