Skip to content

Instantly share code, notes, and snippets.

@draveness
Last active September 29, 2017 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draveness/6ae2e7703d82534b743c685caa1bcfe9 to your computer and use it in GitHub Desktop.
Save draveness/6ae2e7703d82534b743c685caa1bcfe9 to your computer and use it in GitHub Desktop.
Export MongoDB to csv files and import them into MySQL with mysqldump
module DatabaseTransformer
def self.import(collection, fields, columns)
mongo_export = <<-EOF
mongoexport --host #{hostname} --username #{username} \
--password #{password} \
--authenticationDatabase #{auth_source} \
--db #{database} --collection #{collection} \
--type=csv \
--fields #{fields.join(',')} \
--out #{collection}.csv
EOF
STDOUT.puts shell
Dir.mktmpdir do |tmpdir|
tmpdir = File.expand_path tmpdir
Dir.chdir(tmpdir) do
`#{mongo_export}`
csv_file = "#{collection}.csv"
connection = ActiveRecord::Base.connection
connection.execute "set @@sql_mode='no_engine_substitution';"
sql = "LOAD DATA LOCAL INFILE '#{tmpdir}/#{csv_file}' INTO TABLE #{collection} FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES (#{columns.join(',')});"
connection.execute sql
end
end
end
end
DatabaseTransformer.import(:posts, [:title, :content], [:title, :content])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment