class Mongo::Util

Constants

STANDARD_TO_SETTINGS
VERSION

Attributes

dump_dir[RW]
from[R]
to[R]

Public Class Methods

authentication(db_config) click to toggle source

Enable auth if we set db_config contains user & password

# File lib/mongo/util.rb, line 102
def self.authentication(db_config)
  if (db_config[:user] && db_config[:password])
    " -u#{db_config[:user]} -p#{db_config[:password]}"
  else
    ""
  end
end
new(from={}, to={}, dump_dir=nil) click to toggle source
# File lib/mongo/util.rb, line 12
def initialize(from={}, to={}, dump_dir=nil)
  @from = from
  @to = STANDARD_TO_SETTINGS.merge(to)
  @dump_dir = dump_dir || 'dump'
end

Public Instance Methods

clean!() click to toggle source

Deletes @dump_dir

# File lib/mongo/util.rb, line 89
def clean!
  self.exec("rm -rf #{@dump_dir}")
end
collections() click to toggle source

Returns Array of all collection-names of @from{database}

# File lib/mongo/util.rb, line 74
def collections
  unless @from[:host] && @from[:port] && @from[:db]
    raise 'Cannot fetch collections: needs @to[:host], @to[:port], @to[:db]'
  end

  cmd = "mongo #{@from[:db]} --host #{@from[:host]} --port #{@from[:port]} --quiet --eval 'db.getCollectionNames()'"
  # Append auth, if neccessary
  cmd += Mongo::Util.authentication(@from)

  collections = self.exec(cmd, return_output: true).rstrip.split(',')
  # If we have a '{' in the output, Mongo has thrown an error
  collections.each {|col| raise "Error while fetching collections: '#{collections.join()}'" if col.include?('{')}
end
dump(options={}) click to toggle source

Dump from @from{database}

# File lib/mongo/util.rb, line 27
def dump(options={})
  unless @from[:host] && @from[:port] && @from[:db]
    raise 'Cannot dump: needs @to[:host], @port & @db'
  end

  cmd = "mongodump --host #{@from[:host]} --port #{@from[:port]} -d #{@from[:db]}"
  # Append collection, if neccessary
  cmd += " -c #{options[:collection]}" if options[:collection]
  # Append query, if neccessary
  cmd += " -q '#{options[:query].to_json}'" if options[:query]
  # Append auth, if neccessary
  cmd += Mongo::Util.authentication(@from)

  self.exec(cmd)
end
exec(cmd, options={}) click to toggle source
# File lib/mongo/util.rb, line 93
def exec(cmd, options={})
  # Print commands for debugging
  print "\n=======================================\n"
  print "| Executing: '#{cmd}'\n"
  print "=======================================\n\n"
  options[:return_output] ? `#{cmd}` : system(cmd)
end
from=(from) click to toggle source
# File lib/mongo/util.rb, line 18
def from=(from)
  @from = @from.merge(from)
end
remove!(collection, options={}) click to toggle source

Removes all items / items which match options from collection of @from{database} NOTE: Changes @from{database}

# File lib/mongo/util.rb, line 60
def remove!(collection, options={})
  unless @to[:host] && @to[:port] && @to[:db]
    raise "Cannot remove #{collection}: needs @to[:host], @to[:port], @to[:db]"
  end

  cmd = "mongo #{@to[:db]} --host #{@to[:host]} --port #{@to[:port]}"
  # Append auth, if neccessary
  cmd += Mongo::Util.authentication(@to)
  cmd += " --eval 'db.#{collection}.remove(#{options[:query] ? options[:query].to_json : "{}"});'"

  self.exec(cmd)
end
restore!() click to toggle source

Restore contents of @dump_dr to @to{database} NOTE: Changes @to{database}

# File lib/mongo/util.rb, line 45
def restore!
  unless @to[:host] && @to[:port] && @to[:db] && @from[:db]
    raise 'Cannot restore: needs @to[:host], @to[:port], @to[:db] & @from[:db]'
  end

  cmd = "mongorestore --host #{@to[:host]} --port #{@to[:port]} -d#{@to[:db]} dump/#{@from[:db]}"
  # Append auth, if neccessary
  cmd += Mongo::Util.authentication(@to)

  self.exec(cmd)
end
to=(to) click to toggle source
# File lib/mongo/util.rb, line 22
def to=(to)
  @to = @to.merge(to)
end