Skip to content

Commit

Permalink
Add new migration for new surface station data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Nov 5, 2016
1 parent 0425dd7 commit 3106ab2
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ public function up()
{
// Check for the file before attempting to populate the table. Will
// save us from having to rollback the transaction.
$t = $this->createTable('horde_metar_airports', array('autoincrementKey' => array('id')));
if (!$this->_checkForMetarData()) {
throw new Horde_Exception('Unable to locate METAR data.');
// Return instead of failing since this will always fail if
// the data isn't present locally (the data source is defunct).
// The '2' migration will populate the table with the new data
// source.
return;
}

$this->beginDbTransaction();
$t = $this->createTable('horde_metar_airports', array('autoincrementKey' => array('id')));
$t->column('id', 'integer');
$t->column('block', 'integer');
$t->column('station', 'integer');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
class HordeServiceWeatherAirportsChange extends Horde_Db_Migration_Base
{
protected $_handle;

public function up()
{
// Check for the file before attempting to populate the table. Will
// save us from having to rollback the transaction.
if (!$this->_checkForMetarData()) {
throw new Horde_Exception('Unable to locate METAR data.');
}

// Get rid of the old data.
$this->down();
$this->beginDbTransaction();
$t = $this->createTable('horde_metar_airports', array('autoincrementKey' => array('id')));
$t->column('id', 'integer');
$t->column('icao', 'string', array('limit' => 4));
$t->column('name', 'string', array('limit' => 80));
$t->column('state', 'string', array('limit' => 2));
$t->column('country', 'string', array('limit' => 50));
$t->column('municipality', 'string', array('limit' => 80));
$t->column('latitude', 'float');
$t->column('longitude', 'float');
$t->column('elevation', 'float');
$t->end();
$this->_populateTable();
$this->commitDbTransaction();
}

protected function _checkForMetarData()
{
// First see if we have a local copy in the same directory.
$file_name = __DIR__ . DIRECTORY_SEPARATOR . 'airport-codes.csv';
if (file_exists($file_name)) {
$this->_handle = @fopen($file_name, 'rb');
} else {
$file_location = 'https://raw.githubusercontent.com/datasets/airport-codes/master/data/airport-codes.csv';
$this->_handle = @fopen($file_location, 'rb');
}
if (!$this->_handle) {
$this->announce('ERROR: Unable to populate METAR database.');
return false;
}

return true;
}

protected function _populateTable()
{
$line = 0;
$insert = 'INSERT INTO horde_metar_airports '
. '(id, icao, name, state, country, municipality, latitude,'
. 'longitude, elevation) '
. 'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)';

/**
* ident,
* type,
* name,
* latitude_deg,
* longitude_deg,
* elevation_ft,
* continent,
* iso_country,
* iso_region,
* municipality,
* gps_code,
* iata_code,
* local_code
*/
while (($data = fgetcsv($this->_handle)) !== false) {
if (sizeof($data) < 13) {
continue;
}
try {
$this->_connection->insert($insert, array(
$line,
$data[0],
$data[2],
str_replace($data[7] . '-', '', $data[8]),
$data[7],
$data[9],
round($data[3], 4),
round($data[4], 4),
$data[5])
);
} catch (Horde_Db_Exception $e) {
$this->announce('ERROR: ' . $e->getMessage());
$this->rollbackDbTransaction();
return;
}
$line++;
}
$this->announce('Added ' . ($line) . ' airport identifiers to the database.', 'cli.message');
}

public function down()
{
$tableList = $this->tables();
if (in_array('horde_metar_airports', $tableList)) {
$this->dropTable('horde_metar_airports');
}
}

}
14 changes: 8 additions & 6 deletions framework/Service_Weather/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<email>mrubinsk@horde.org</email>
<active>yes</active>
</lead>
<date>2016-11-03</date>
<date>2016-11-05</date>
<version>
<release>2.4.2</release>
<api>1.4.0</api>
<release>2.5.0</release>
<api>1.5.0</api>
</version>
<stability>
<release>stable</release>
Expand Down Expand Up @@ -153,6 +153,7 @@
<dir name="Service">
<dir name="Weather">
<file name="1_horde_service_weather_airports.php" role="data" />
<file name="2_horde_service_weather_airports_change.php" role="data" />
</dir> <!-- /migration/Horde/Service/Weather -->
</dir> <!-- /migration/Horde/Service -->
</dir> <!-- /migration/Horde -->
Expand Down Expand Up @@ -416,6 +417,7 @@
<install as="locale/nl/LC_MESSAGES/Horde_Service_Weather.mo" name="locale/nl/LC_MESSAGES/Horde_Service_Weather.mo" />
<install as="locale/nl/LC_MESSAGES/Horde_Service_Weather.po" name="locale/nl/LC_MESSAGES/Horde_Service_Weather.po" />
<install as="migration/1_horde_service_weather_airports.php" name="migration/Horde/Service/Weather/1_horde_service_weather_airports.php" />
<install as="migration/2_horde_service_weather_airports_change.php" name="migration/Horde/Service/Weather/2_horde_service_weather_airports_change.php" />
<install as="Horde/Service/Weather/AllTests.php" name="test/Horde/Service/Weather/AllTests.php" />
<install as="Horde/Service/Weather/bootstrap.php" name="test/Horde/Service/Weather/bootstrap.php" />
<install as="Horde/Service/Weather/MetarTest.php" name="test/Horde/Service/Weather/MetarTest.php" />
Expand Down Expand Up @@ -983,12 +985,12 @@
</release>
<release>
<version>
<release>2.4.2</release>
<api>1.4.0</api></version>
<release>2.5.0</release>
<api>1.5.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2016-11-03</date>
<date>2016-11-05</date>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
* [mjr] Replace defunct data source for surface station data (Bug #14502).
Expand Down

0 comments on commit 3106ab2

Please sign in to comment.