You are here

function weather_update_2 in Weather 5

Same name and namespace in other branches
  1. 5.6 weather.install \weather_update_2()

File

./weather.install, line 159

Code

function weather_update_2() {
  $ret = array();

  // these were used in version 1.x of this module, they are no longer
  // needed in the 2.x series
  variable_del('weather_country');
  variable_del('weather_icao');
  variable_del('weather_icao_name');
  variable_del('weather_place');
  variable_del('weather_units');
  variable_del('weather_use_cron');

  // we don't need the wid column. We make icao the new primary key,
  // however, we must first remove double entries. To achieve this,
  // we simply empty the whole table.
  $ret[] = update_sql("DELETE FROM {weather}");

  // drop the primary key
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {weather} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {weather} ADD PRIMARY KEY (icao)");
      $ret[] = update_sql("ALTER TABLE {weather} DROP wid");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {weather} DROP CONSTRAINT {weather}_pkey");
      $ret[] = update_sql("ALTER TABLE {weather} ADD PRIMARY KEY (icao)");
      $ret[] = update_sql("ALTER TABLE {weather} DROP COLUMN wid");
      break;
  }

  // remove the primary key from the sequence table
  $ret[] = update_sql("DELETE FROM {sequences} WHERE name='{weather_wid}'");

  // change the column reported_on to next_update_on,
  // which is the timestamp of next possible update
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {weather} CHANGE reported_on next_update_on INTEGER DEFAULT 0 NOT NULL");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {weather} RENAME COLUMN reported_on TO next_update_on");
      break;
  }

  // the table stores the configuration of a custom user weather block
  // uid: user id
  // icao: ICAO code of the METAR station
  // real_name: the name to display for the ICAO code
  // units: units for display (metric, imperial)
  $sql = "CREATE TABLE {weather_config} (\n    uid INTEGER DEFAULT 0 NOT NULL,\n    icao VARCHAR(4) DEFAULT '' NOT NULL,\n    real_name VARCHAR(255) DEFAULT '' NOT NULL,\n    units VARCHAR(8) DEFAULT 'metric' NOT NULL,\n    PRIMARY KEY (uid)\n  )";

  // ensure the utf8 character set on MySQL (syntax for 4.1 and above)
  if ($GLOBALS['db_type'] == 'mysql' or $GLOBALS['db_type'] == 'mysqli') {
    $sql .= " /*!40100 DEFAULT CHARACTER SET utf8 */";
  }
  $ret[] = update_sql($sql);
  return $ret;
}