diff --git a/lib/geocoder/railtie.rb b/lib/geocoder/railtie.rb index bd7cfb296ebcb8b61a2befb70826d435411ca2b1..ec1113170a587433397d351a14fad6492d980875 100644 --- a/lib/geocoder/railtie.rb +++ b/lib/geocoder/railtie.rb @@ -11,6 +11,7 @@ module Geocoder end rake_tasks do load "tasks/geocoder.rake" + load "tasks/maxmind.rake" end end end diff --git a/lib/tasks/maxmind.rake b/lib/tasks/maxmind.rake new file mode 100644 index 0000000000000000000000000000000000000000..bb2c6d7cd79ed8df9e358e0d969992edb800cd8c --- /dev/null +++ b/lib/tasks/maxmind.rake @@ -0,0 +1,35 @@ +namespace :geocoder do + namespace :maxmind do + namespace :geolite_city do + + desc "Load MaxMind GeoLite City into SQL database" + task load_data: :environment do + clear_tables_mysql + load_table_mysql('maxmind_blocks', 'tmp/GeoLiteCity-Blocks.csv') + load_table_mysql('maxmind_location', 'tmp/GeoLiteCity-Location.csv') + end + end + end +end + +# IMPORTANT: http://stackoverflow.com/questions/10737974/load-data-local-infile-gives-the-error-the-used-command-is-not-allowed-with-this +def load_table_mysql(table, filepath) + q = <<-END + LOAD DATA LOCAL INFILE '#{filepath}' + INTO TABLE #{table} + IGNORE 2 LINES + FIELDS TERMINATED BY ',' + OPTIONALLY ENCLOSED BY '\"' + LINES TERMINATED BY '\n'; + END + ActiveRecord::Base.connection.execute(q) +end + +def clear_tables_mysql + [ + #"LOCK TABLES maxmind_blocks READ, maxmind_location READ", + "DELETE from maxmind_blocks", + "DELETE from maxmind_location", + #"UNLOCK TABLES" + ].each{ |q| ActiveRecord::Base.connection.execute(q) } +end