You are here

function location_update_6302 in Location 7.3

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

Drupal 6 location 3.x update, part 2.

File

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

Code

function location_update_6302() {

  // OK, here's the update to fix the previous update which had a few problems
  // when upgrading from pre-rc 6.x versions.
  // The "mismatch between mysql and postgresql" was actually applicable to
  // 6.x-3.0 pre-rc1 as well, but I didn't notice because I accidentally added
  // the not null when reformatting the schema.
  db_update('location')
    ->fields(array(
    'is_primary' => 0,
  ))
    ->isNull('is_primary')
    ->execute();
  db_change_field('location', 'is_primary', 'is_primary', array(
    'type' => 'int',
    'size' => 'tiny',
    'default' => 0,
    'not null' => TRUE,
  ));

  // Fix zipcode mismatches caused by the same problem.
  // There shouldn't be any rows like this, but it doesn't hurt to be sure.
  db_update('zipcodes')
    ->fields(array(
    'zip' => 0,
  ))
    ->isNull('zip')
    ->execute();

  // Set not null.
  db_change_field('zipcodes', 'zip', 'zip', array(
    'type' => 'varchar',
    'length' => 16,
    'not null' => TRUE,
    'default' => '0',
  ));

  // Prepare latitude and longitude for the same.
  db_update('zipcodes')
    ->fields(array(
    'latitude' => 0.0,
  ))
    ->isNull('latitude')
    ->execute();
  db_update('zipcodes')
    ->fields(array(
    'longitude' => 0.0,
  ))
    ->isNull('longitude')
    ->execute();

  // Set not null.
  db_change_field('zipcodes', 'latitude', 'latitude', array(
    'type' => 'numeric',
    'not null' => TRUE,
    'default' => 0,
    'precision' => 10,
    'scale' => 6,
  ));
  db_change_field('zipcodes', 'longitude', 'longitude', array(
    'type' => 'numeric',
    'not null' => TRUE,
    'default' => 0,
    'precision' => 10,
    'scale' => 6,
  ));

  // Prepare country.
  update_sql("UPDATE {zipcodes} SET country = '' WHERE country IS NULL");
  db_update('zipcodes')
    ->fields(array(
    'country' => '',
  ))
    ->isNull('country')
    ->execute();

  // Set not null.
  db_change_field('zipcodes', 'country', 'country', array(
    'type' => 'char',
    'length' => 2,
    'not null' => TRUE,
    'default' => '',
  ));

  // Fix up possible {location} problems from previous update that could be caused if you had NULLed fields.
  // Set defaults.
  $fields = array(
    'name' => '',
    'street' => '',
    'additional' => '',
    'city' => '',
    'province' => '',
    'postal_code' => '',
    'latitude' => 0.0,
    'longitude' => 0.0,
    'source' => 0,
  );
  foreach ($fields as $field => $value) {
    db_update('location')
      ->fields(array(
      $field => $value,
    ))
      ->isNull($field)
      ->execute();
  }

  // {location}.name -- NOT NULL.
  db_change_field('location', 'name', 'name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.street -- NOT NULL.
  db_change_field('location', 'street', 'street', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.additional -- NOT NULL.
  db_change_field('location', 'additional', 'additional', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.city -- NOT NULL.
  db_change_field('location', 'city', 'city', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.province -- NOT NULL.
  db_change_field('location', 'province', 'province', array(
    'type' => 'varchar',
    'length' => 16,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.postal_code -- NOT NULL.
  db_change_field('location', 'postal_code', 'postal_code', array(
    'type' => 'varchar',
    'length' => 16,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.country -- NOT NULL.
  db_change_field('location', 'country', 'country', array(
    'type' => 'char',
    'length' => 2,
    'not null' => TRUE,
    'default' => '',
  ));

  // {location}.latitude.
  db_change_field('location', 'latitude', 'latitude', array(
    'type' => 'numeric',
    'precision' => 10,
    'scale' => 6,
    'not null' => TRUE,
    'default' => 0.0,
  ));

  // {location}.longitude.
  db_change_field('location', 'longitude', 'longitude', array(
    'type' => 'numeric',
    'precision' => 10,
    'scale' => 6,
    'not null' => TRUE,
    'default' => 0.0,
  ));

  // {location}.source.
  db_change_field('location', 'source', 'source', array(
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 0,
  ));
}