diff --git a/db/manifest b/db/manifest index 641c740df..bad9decf1 100755 --- a/db/manifest +++ b/db/manifest @@ -135,4 +135,5 @@ update_get_work_for_client_type.sql events.sql cascading_delete_constraints_for_release.sql events_social_description.sql +fix_broken_cities.sql diff --git a/db/up/fix_broken_cities.sql b/db/up/fix_broken_cities.sql new file mode 100644 index 000000000..baffc7cf1 --- /dev/null +++ b/db/up/fix_broken_cities.sql @@ -0,0 +1,2 @@ +alter table cities drop column regionname; +alter table cities drop column countryname; diff --git a/ruby/lib/jam_ruby/models/geo_ip_locations.rb b/ruby/lib/jam_ruby/models/geo_ip_locations.rb index 0201ff679..e5ea8a653 100644 --- a/ruby/lib/jam_ruby/models/geo_ip_locations.rb +++ b/ruby/lib/jam_ruby/models/geo_ip_locations.rb @@ -132,7 +132,7 @@ module JamRuby ActiveRecord::Base.logger.debug "DELETE FROM #{REGIONS_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check - sts = self.connection.execute "INSERT INTO #{REGIONS_TABLE} (region, countrycode) SELECT DISTINCT region, countrycode FROM #{CITIES_TABLE};" + sts = self.connection.execute "INSERT INTO #{REGIONS_TABLE} (region, regionname, countrycode) SELECT DISTINCT region, region, countrycode FROM #{CITIES_TABLE};" ActiveRecord::Base.logger.debug "INSERT INTO #{REGIONS_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check @@ -140,7 +140,7 @@ module JamRuby ActiveRecord::Base.logger.debug "DELETE FROM #{COUNTRIES_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check - sts = self.connection.execute "INSERT INTO #{COUNTRIES_TABLE} (countrycode) SELECT DISTINCT countrycode FROM #{REGIONS_TABLE};" + sts = self.connection.execute "INSERT INTO #{COUNTRIES_TABLE} (countrycode, countryname) SELECT DISTINCT countrycode, countrycode FROM #{REGIONS_TABLE};" ActiveRecord::Base.logger.debug "INSERT INTO #{COUNTRIES_TABLE} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger sts.check end diff --git a/web/lib/max_mind_manager.rb b/web/lib/max_mind_manager.rb index 20f1b7da6..4ed9c8c08 100644 --- a/web/lib/max_mind_manager.rb +++ b/web/lib/max_mind_manager.rb @@ -12,25 +12,28 @@ class MaxMindManager < BaseManager city = state = country = nil unless ip_address.nil? || ip_address !~ /^\d+\.\d+\.\d+\.\d+$/ - ActiveRecord::Base.connection_pool.with_connection do |connection| - pg_conn = connection.instance_variable_get("@connection") - ip_as_int = ip_address_to_int(ip_address) - pg_conn.exec("SELECT country, region, city FROM max_mind_geo WHERE ip_start <= $1 AND $2 <= ip_end limit 1", [ip_as_int, ip_as_int]) do |result| - if !result.nil? && result.ntuples > 0 - country = result[0]['country'] - state = result[0]['region'] - city = result[0]['city'] - end - end + #ActiveRecord::Base.connection_pool.with_connection do |connection| + # pg_conn = connection.instance_variable_get("@connection") + # ip_as_int = ip_address_to_int(ip_address) + # pg_conn.exec("SELECT country, region, city FROM max_mind_geo WHERE ip_start <= $1 AND $2 <= ip_end limit 1", [ip_as_int, ip_as_int]) do |result| + # if !result.nil? && result.ntuples > 0 + # country = result[0]['country'] + # state = result[0]['region'] + # city = result[0]['city'] + # end + # end + #end + ip_as_int = ip_address_to_int(ip_address) + block = GeoIpBlocks.lookup(ip_as_int) + location = block ? GeoIpLocations.lookup(block.locid) : nil + if location + country = location.countrycode + state = location.region + city = location.city end end - { - :city => city, - :state => state, - :country => country - } - + a = {:city => city, :state => state, :country => country} end def self.lookup_isp(ip_address) @@ -53,32 +56,41 @@ class MaxMindManager < BaseManager end def self.countries() - ActiveRecord::Base.connection_pool.with_connection do |connection| - pg_conn = connection.instance_variable_get("@connection") - pg_conn.exec("SELECT DISTINCT country FROM max_mind_geo ORDER BY country ASC").map do |tuple| - tuple["country"] - end - end + #ActiveRecord::Base.connection_pool.with_connection do |connection| + # pg_conn = connection.instance_variable_get("@connection") + # pg_conn.exec("SELECT DISTINCT country FROM max_mind_geo ORDER BY country ASC").map do |tuple| + # tuple["country"] + # end + #end + + # returns ordered array of Country objects (countrycode, countryname) + Country.get_all.map { |c| c.countrycode } end def self.regions(country) - ActiveRecord::Base.connection_pool.with_connection do |connection| - pg_conn = connection.instance_variable_get("@connection") - pg_conn.exec("SELECT DISTINCT region FROM max_mind_geo WHERE country = $1 ORDER BY region ASC", [country]).map do |tuple| - tuple["region"] - end - end + #ActiveRecord::Base.connection_pool.with_connection do |connection| + # pg_conn = connection.instance_variable_get("@connection") + # pg_conn.exec("SELECT DISTINCT region FROM max_mind_geo WHERE country = $1 ORDER BY region ASC", [country]).map do |tuple| + # tuple["region"] + # end + #end + + # returns an ordered array of Region objects (region, regionname, countrycode) + Region.get_all(country).map { |r| r.region } end def self.cities(country, region) - ActiveRecord::Base.connection_pool.with_connection do |connection| - pg_conn = connection.instance_variable_get("@connection") - pg_conn.exec("SELECT DISTINCT city FROM max_mind_geo WHERE country = $1 AND region = $2 ORDER BY city ASC", [country, region]).map do |tuple| - tuple["city"] - end - end + #ActiveRecord::Base.connection_pool.with_connection do |connection| + # pg_conn = connection.instance_variable_get("@connection") + # pg_conn.exec("SELECT DISTINCT city FROM max_mind_geo WHERE country = $1 AND region = $2 ORDER BY city ASC", [country, region]).map do |tuple| + # tuple["city"] + # end + #end + + # returns an ordered array of City (city, region, countrycode) + City.get_all(country, region).map { |c| c.city } end @@ -116,7 +128,14 @@ class MaxMindManager < BaseManager ]).clear end + @pg_conn.exec "DELETE FROM cities" + @pg_conn.exec "INSERT INTO cities (city, region, countrycode) SELECT DISTINCT city, region, country FROM max_mind_geo" + @pg_conn.exec "DELETE FROM regions" + @pg_conn.exec "INSERT INTO regions (region, regionname, countrycode) select distinct region, region, countrycode from cities" + + @pg_conn.exec "DELETE FROM countries" + @pg_conn.exec "INSERT INTO countries (countrycode, countryname) SELECT DISTINCT countrycode, countrycode FROM regions" end private diff --git a/web/spec/managers/maxmind_manager_spec.rb b/web/spec/managers/maxmind_manager_spec.rb index 90a0932c1..86fffa19d 100644 --- a/web/spec/managers/maxmind_manager_spec.rb +++ b/web/spec/managers/maxmind_manager_spec.rb @@ -20,7 +20,7 @@ describe MaxMindManager do regions = MaxMindManager.regions("US") regions.length.should == 4 regions.first.should == "AB" - regions.last.should == "DE" # Based on sort order this will be the top. + regions.last.should == "DE" end it "looks up cities successfully" do diff --git a/web/spec/managers/user_manager_spec.rb b/web/spec/managers/user_manager_spec.rb index 61ee5eb31..ba4d3a33d 100644 --- a/web/spec/managers/user_manager_spec.rb +++ b/web/spec/managers/user_manager_spec.rb @@ -31,9 +31,9 @@ describe UserManager do @user.last_name.should == "smith" @user.email.should == "userman1@jamkazam.com" @user.email_confirmed.should be_false - @user.city.should be_nil - @user.state.should be_nil - @user.country.should be_nil + @user.city.should == 'Boston' + @user.state.should == 'MA' + @user.country.should == 'US' @user.instruments.length.should == 1 @user.subscribe_email.should be_true @user.signup_token.should_not be_nil @@ -93,8 +93,8 @@ describe UserManager do signup_confirm_url: "http://localhost:3000/confirm" ) @user.errors.any?.should be_false - @user.city.should == 'City 127' - @user.state.should == 'DE' + @user.city.should == 'Boston' + @user.state.should == 'MA' @user.country.should == 'US' end