From b67066927304be7cce3348c3f3e5a3cccdce3ad5 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sun, 23 Feb 2014 01:04:29 -0500
Subject: [PATCH] Add rake task for loading MaxMind data.

---
 lib/geocoder/railtie.rb |  1 +
 lib/tasks/maxmind.rake  | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 lib/tasks/maxmind.rake

diff --git a/lib/geocoder/railtie.rb b/lib/geocoder/railtie.rb
index bd7cfb29..ec111317 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 00000000..bb2c6d7c
--- /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
-- 
GitLab