From e4f8ee7c4c69bba1a996c9af552fd8bd2f2faaa4 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sat, 22 Feb 2014 23:46:11 -0500
Subject: [PATCH] Add migration generator for geolite city database.

---
 lib/generators/geocoder/maxmind_generator.rb  | 26 +++++++++++++++++++
 .../migration/maxmind_geolite_city.rb         | 23 ++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 lib/generators/geocoder/maxmind_generator.rb
 create mode 100644 lib/generators/geocoder/templates/migration/maxmind_geolite_city.rb

diff --git a/lib/generators/geocoder/maxmind_generator.rb b/lib/generators/geocoder/maxmind_generator.rb
new file mode 100644
index 00000000..40921ab1
--- /dev/null
+++ b/lib/generators/geocoder/maxmind_generator.rb
@@ -0,0 +1,26 @@
+require 'rails/generators/migration'
+
+module Geocoder
+  module Generators
+    class MaxmindGenerator < Rails::Generators::Base
+      include Rails::Generators::Migration
+
+      source_root File.expand_path('../templates', __FILE__)
+
+      def copy_migration_files
+      	migration_template "migration/maxmind_geolite_city.rb", "db/migrate/geocoder_maxmind_geolite_city.rb"
+      end
+
+      # Define the next_migration_number method (necessary for the
+      # migration_template method to work)
+      def self.next_migration_number(dirname)
+        if ActiveRecord::Base.timestamped_migrations
+          sleep 1 # make sure each time we get a different timestamp
+          Time.new.utc.strftime("%Y%m%d%H%M%S")
+        else
+          "%.3d" % (current_migration_number(dirname) + 1)
+        end
+      end
+    end
+  end
+end
\ No newline at end of file
diff --git a/lib/generators/geocoder/templates/migration/maxmind_geolite_city.rb b/lib/generators/geocoder/templates/migration/maxmind_geolite_city.rb
new file mode 100644
index 00000000..ed6d421e
--- /dev/null
+++ b/lib/generators/geocoder/templates/migration/maxmind_geolite_city.rb
@@ -0,0 +1,23 @@
+class GeocoderMaxmindGeoliteCity < ActiveRecord::Migration
+  def change
+    create_table :maxmind_blocks, id: false do |t|
+      t.column :startIpNum, 'integer unsigned', null: false
+      t.column :endIpNum, 'integer unsigned', null: false
+      t.column :locId, 'integer unsigned', null: false
+    end
+    add_index :maxmind_blocks, :startIpNum, unique: true
+
+    create_table :maxmind_location, id: false do |t|
+  	  t.column :locId, 'integer unsigned', null: false
+      t.string :country, null: false
+      t.string :region, null: false
+      t.string :city
+      t.string :postalCode, null: false
+      t.float :latitude
+      t.float :longitude
+      t.integer :dmaCode
+      t.integer :areaCode
+    end
+    add_index :maxmind_location, :locId, unique: true
+  end
+end
\ No newline at end of file
-- 
GitLab