Permalink
Browse files

Add files

  • Loading branch information...
1 parent a91fba8 commit bb5d0d8091702d9d2d54ab3b099dafeec54b9b5c @masterkain committed Jul 30, 2011
Showing with 60 additions and 0 deletions.
  1. +5 −0 Gemfile
  2. +16 −0 Gemfile.lock
  3. +39 −0 mysql2mongo.rb
View
@@ -0,0 +1,5 @@
+source 'http://rubygems.org'
+
+gem 'mongo'
+gem 'bson_ext'
+gem 'mysql2'
View
@@ -0,0 +1,16 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ bson (1.3.1)
+ bson_ext (1.3.1)
+ mongo (1.3.1)
+ bson (>= 1.3.1)
+ mysql2 (0.3.6)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bson_ext
+ mongo
+ mysql2
View
@@ -0,0 +1,39 @@
+require "rubygems"
+require "bundler/setup"
+require "mysql2"
+require "mongo"
+
+MYSQL_SETTINGS = {
+ host: "127.0.0.1",
+ username: "root",
+ password: "",
+ database: "myapp_production",
+ encoding: "utf8"
+}
+
+MONGO_SETTINGS = {
+ schema: "myapp_production"
+}
+
+mysql_client = Mysql2::Client.new(MYSQL_SETTINGS)
+mysql_client.query_options.merge!(symbolize_keys: true)
+
+mongo_client = Mongo::Connection.new.db(MONGO_SETTINGS[:schema])
+
+if MONGO_SETTINGS.has_key?(:username) && MONGO_SETTINGS.has_key?(:password)
+ raise SystemExit, "Cannot connect to mongo, provided keys are not valid" unless mongo_client.authenticate(MONGO_SETTINGS[:username], MONGO_SETTINGS[:password])
+end
+
+puts "Migrating from: #{MYSQL_SETTINGS[:database]}"
+
+mysql_query_string = "select * from information_schema.tables where table_schema='#{MYSQL_SETTINGS[:database]}'"
+mysql_client.query(mysql_query_string).each do |row|
+ current_table = row[:TABLE_NAME]
+ puts "Working on: #{row[:TABLE_NAME]}"
+ inner_qs = "select * from #{current_table}"
+
+ current_collection = mongo_client.collection(current_table)
+ mysql_client.query(inner_qs).each do |result_row|
+ current_collection.insert(result_row)
+ end
+end

0 comments on commit bb5d0d8

Please sign in to comment.