Last active
September 29, 2017 08:57
-
-
Save draveness/6ae2e7703d82534b743c685caa1bcfe9 to your computer and use it in GitHub Desktop.
Export MongoDB to csv files and import them into MySQL with mysqldump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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