-
-
Save kzhangworks/4036677 to your computer and use it in GitHub Desktop.
Rake tasks to list, export & import Mongo DB collections
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
namespace :mongo do | |
def db_name | |
Mongoid.database.name | |
end | |
def db_connection_options | |
host, port = Mongoid.database.connection.host_to_try | |
auths = Mongoid.database.connection.auths | |
auth_string = auths.length > 0 ? "-u #{auths[0]["username"]} -p #{auths[0]["password"]}" : "" | |
conn_string = "#{auth_string} --host #{host} --port #{port} -d #{db_name}" | |
conn_string | |
end | |
desc "Exports all or the specified Mongo collection to JSON files" | |
task :export, [:collection] => :environment do |task, args| | |
coll_string = args[:collection] ? "-c #{args[:collection]}" : "" | |
data_directory = File.join("db", "data", Time.now.strftime("%Y%m%d-%H%m%S")) | |
cmd = "mongodump #{db_connection_options} #{coll_string} -o #{data_directory}" | |
puts "Executing: #{cmd}#{File::SEPARATOR}#{db_name}" | |
`#{cmd}` | |
Dir.glob("#{File.join(data_directory, db_name)}#{File::SEPARATOR}*.bson").each do |file_name| | |
collection_name = file_name.split(".")[0] | |
cmd = "bsondump --type json #{file_name} > #{collection_name}.json" | |
puts "Executing: #{cmd}" | |
`#{cmd}` | |
end | |
puts "Ok" | |
end | |
desc "Imports all or the specified JSON file to the Mongo collection(s)" | |
task :import, [:directory, :collection] => :environment do |task, args| | |
if args[:collection] | |
cmd = "mongoimport #{db_connection_options} -c #{args[:collection]} --type json --file #{File.join(args[:directory], args[:collection])}.json --upsert" | |
puts "Executing: #{cmd}" | |
`#{cmd}` | |
else | |
Dir.glob("#{args[:directory]}#{File::SEPARATOR}*.json").each do |file_name| | |
collection_name = file_name.split("\/").last.split(".")[0] | |
cmd = "mongoimport #{db_connection_options} -c #{collection_name} --type json --file #{file_name} --upsert" | |
puts "Executing: #{cmd}" | |
`#{cmd}` | |
end | |
end | |
end | |
def mongoid_models | |
documents = [] | |
Dir.glob("app/models/**/*.rb").sort.each do |file| | |
model_path = file[0..-4].split('/')[2..-1] | |
begin | |
klass = model_path.map { |path| path.camelize }.join('::').constantize | |
if klass.ancestors.include?(Mongoid::Document) && !klass.embedded | |
documents << klass | |
end | |
rescue => e | |
# Just for non-mongoid objects that dont have the embedded | |
# attribute at the class level. | |
end | |
end | |
documents | |
end | |
desc "List all Mongo DB collections in the database that have a corresponding model class in the application" | |
task :list_collections => :environment do | |
documents = [] | |
Dir.glob("app/models/**/*.rb").sort.each do |file| | |
model_path = file[0..-4].split('/')[2..-1] | |
begin | |
klass = model_path.map { |path| path.camelize }.join('::').constantize | |
documents << klass if klass.ancestors.include?(Mongoid::Document) && !klass.embedded | |
rescue | |
# Just for non-mongoid objects that dont have the embedded attribute at the class level. | |
end | |
end | |
collections = Mongoid.master.collection_names.reject {|cn| cn == "system.indexes" } | |
collections.each do |collection| | |
puts collection if documents.include?(collection.classify.constantize) rescue NameError # ignore collection if no corresponding model exists | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment