]> git.openstreetmap.org Git - rails.git/blob - db/migrate/031_create_countries.rb
Extra little test, setting the language on the diary fixtures.
[rails.git] / db / migrate / 031_create_countries.rb
1 require 'lib/migrate'
2
3 class CreateCountries < ActiveRecord::Migration
4   def self.up
5     create_table :countries, innodb_table do |t|
6       t.column :id,      :integer_pk,              :null => false
7       t.column :code,    :string,     :limit => 2, :null => false
8       t.column :min_lat, :double,                  :null => false
9       t.column :max_lat, :double,                  :null => false
10       t.column :min_lon, :double,                  :null => false
11       t.column :max_lon, :double,                  :null => false
12     end
13
14     add_index :countries, [:code], :name => "countries_code_idx", :unique => true
15
16     Net::HTTP.start('ws.geonames.org') do |http|
17       xml = REXML::Document.new(http.get("/countryInfo").body)
18
19       xml.elements.each("geonames/country") do |ele|
20         code = ele.get_text("countryCode").to_s
21         minlon = ele.get_text("bBoxWest").to_s
22         minlat = ele.get_text("bBoxSouth").to_s
23         maxlon = ele.get_text("bBoxEast").to_s
24         maxlat = ele.get_text("bBoxNorth").to_s
25
26         Country.create(:code => code,
27                        :min_lat => minlat.to_f, :max_lat => maxlat.to_f,
28                        :min_lon => minlon.to_f, :max_lon => maxlon.to_f)
29       end
30     end
31   end
32
33   def self.down
34     drop_table :countries
35   end
36 end