function weather_update_7100 in Weather 7
Implement hook_update_N().
Change tables of weather module to match the new schema.
File
- ./
weather.install, line 315 - Install, update and uninstall functions for the weather module.
Code
function weather_update_7100(&$sandbox) {
// The cron option is no longer provided.
variable_del('weather_use_cron');
// Table 'weather' is replaced by 'weather_metar'. Because all data
// is updated at least after one hour, there's no need to migrate
// old data from the the 'weather' table, so we simply remove it.
db_drop_table('weather');
// Define and create the new table.
$schema = array(
'description' => 'Raw and parsed METAR data for all currently used ICAO codes.',
'fields' => array(
'icao' => array(
'description' => 'ICAO code of the METAR station.',
'type' => 'varchar',
'length' => 4,
'not null' => TRUE,
'default' => '',
),
'reported_on' => array(
'description' => 'UTC time of weather report.',
'type' => 'int',
'not null' => TRUE,
),
'next_update_on' => array(
'description' => 'UTC time of next scheduled update.',
'type' => 'int',
'not null' => TRUE,
),
'sky_condition' => array(
'description' => 'Sky condition, cloud covering.',
'type' => 'varchar',
'length' => 25,
'not null' => FALSE,
'default' => NULL,
),
'phenomena' => array(
'description' => 'Phenomena like rain, snow, thunderstorm.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => NULL,
),
'temperature' => array(
'description' => 'Temperature in degree celsius.',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'dewpoint' => array(
'description' => 'Dewpoint temperature in degree celsius.',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'wind_direction' => array(
'description' => 'Wind direction in degrees.',
'type' => 'varchar',
'length' => 8,
'not null' => FALSE,
'default' => NULL,
),
'wind_speed' => array(
'description' => 'Wind speed in km/h.',
'type' => 'float',
'not null' => FALSE,
'default' => NULL,
),
'wind_gusts' => array(
'description' => 'Wind gust speed in km/h.',
'type' => 'float',
'not null' => FALSE,
'default' => NULL,
),
'pressure' => array(
'description' => 'Pressure in hPa.',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'visibility' => array(
'description' => 'Visibility in meter.',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'sunrise_on' => array(
'description' => 'UTC time of sunrise (NULL if there is no sunrise).',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'sunset_on' => array(
'description' => 'UTC time of sunset (NULL if there is no sunset).',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'image' => array(
'description' => 'Filename of the weather image file.',
'type' => 'varchar',
'length' => 35,
'not null' => FALSE,
'default' => NULL,
),
'raw' => array(
'description' => 'Raw METAR data.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
),
),
'primary key' => array(
'icao',
),
);
db_create_table('weather_metar', $schema);
// Create new table for locations.
$schema = array(
'description' => 'Configuration of a location.',
'fields' => array(
'id' => array(
'description' => 'Location id.',
'type' => 'serial',
),
'display_type' => array(
'description' => 'Type of display (system-wide, user, location, default, ...).',
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
'default' => '',
),
'display_number' => array(
'description' => 'Display number.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'icao' => array(
'description' => 'ICAO code of the METAR station.',
'type' => 'varchar',
'length' => 4,
'not null' => TRUE,
'default' => '',
),
'real_name' => array(
'description' => 'The name to display for the ICAO code.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'weight' => array(
'description' => 'Weight of the location.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'display_type' => array(
'weather_display' => 'type',
),
'display_number' => array(
'weather_display' => 'number',
),
),
);
db_create_table('weather_location', $schema);
// Create new table for displays.
$schema = array(
'description' => 'Configuration of a display (for example, a block).',
'fields' => array(
'type' => array(
'description' => 'Type of display (system-wide, user, location, default, ...).',
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
'default' => '',
),
'number' => array(
'description' => 'Display number.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'units' => array(
'description' => 'Units for display (Celsius/Fahrenheit, mmHg/hPa etc.).',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'serialize' => TRUE,
),
'settings' => array(
'description' => 'Settings for display (Show raw METAR, abbrev. wind directions etc.).',
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
'serialize' => TRUE,
),
),
'primary key' => array(
'type',
'number',
),
);
db_create_table('weather_display', $schema);
// Migrate default settings from 'weather_config'.
$result = db_query('SELECT * FROM {weather_config} WHERE uid=0 AND cid=1')
->fetchObject();
if (!empty($result)) {
$display['type'] = 'default';
$display['number'] = 1;
$display['units'] = serialize(_weather_convert_units($result->units));
$display['settings'] = serialize(_weather_convert_settings($result->settings, $result->units));
db_insert('weather_display')
->fields($display)
->execute();
}
// Migrate system-wide blocks.
$result = db_query('SELECT * FROM {weather_config} WHERE uid<0 ORDER BY cid ASC');
$display_number = 0;
foreach ($result as $row) {
$location = array();
if ($display_number < abs($row->uid)) {
// This is a new system-wide display. Migrate display data as well.
$display = array();
$display_number = abs($row->uid);
$display['type'] = 'system-wide';
$display['number'] = $display_number;
$display['units'] = serialize(_weather_convert_units($row->units));
$display['settings'] = serialize(_weather_convert_settings($row->settings, $row->units));
db_insert('weather_display')
->fields($display)
->execute();
}
// Migrate the location.
$location['display_type'] = 'system-wide';
$location['display_number'] = $display_number;
$location['icao'] = $row->icao;
$location['real_name'] = $row->real_name;
$location['weight'] = $row->weight;
db_insert('weather_location')
->fields($location)
->execute();
}
// Migrate custom user blocks.
$result = db_query('SELECT * FROM {weather_config} WHERE uid>0 ORDER BY cid ASC');
$display_number = 0;
foreach ($result as $row) {
$location = array();
if ($display_number < $row->uid) {
// This is a new custom user display. Migrate display data as well.
$display = array();
$display_number = $row->uid;
$display['type'] = 'user';
$display['number'] = $display_number;
$display['units'] = serialize(_weather_convert_units($row->units));
$display['settings'] = serialize(_weather_convert_settings($row->settings, $row->units));
db_insert('weather_display')
->fields($display)
->execute();
}
// Migrate the location.
$location['display_type'] = 'user';
$location['display_number'] = $display_number;
$location['icao'] = $row->icao;
$location['real_name'] = $row->real_name;
$location['weight'] = $row->weight;
db_insert('weather_location')
->fields($location)
->execute();
}
// Remove old table
db_drop_table('weather_config');
return t('Converted tables to new internal schema.');
}