Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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