Skip to content

Commit

Permalink
Refactoring MongodbReplication class
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagogodinho committed Dec 25, 2013
1 parent cf8b0d9 commit 8f6b107
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/mongodb_clone/mongodb_replication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ def initialize
end

def dump(environment = 'production', session = 'default')
config = base_config[environment.to_s]['sessions'][session.to_s]
config = get_config(environment, session)

params = {
h: config['hosts'][0],
d: config['database'],
u: config['username'],
p: config['password'],
o: "/tmp/#{ config['database'] }/#{ @id }"
}
params = default_params(config).merge({ o: "/tmp/#{ config['database'] }/#{ @id }" })

a = params.collect { |key, value| "-#{ key } \"#{ value.to_s.gsub("\"", "\\\"") }\"" if value }.compact.join(' ')
a = hash_to_inline_params(params)

command = "mongodump #{ a }"

Expand All @@ -31,23 +25,33 @@ def dump(environment = 'production', session = 'default')
end

def restore(environment = 'development', session = 'default')
config = base_config[environment.to_s]['sessions'][session.to_s]
config = get_config(environment, session)

a = hash_to_inline_params(default_params(config))

command = "mongorestore --drop #{ a } #{ @path }"

execute(command)
end

params = {
private

def default_params(config)
{
h: config['hosts'][0],
d: config['database'],
u: config['username'],
p: config['password']
}
end

a = params.collect { |key, value| "-#{ key } \"#{ value.to_s.gsub("\"", "\\\"") }\"" if value }.compact.join(' ')

command = "mongorestore --drop #{ a } #{ @path }"

execute(command)
def get_config(environment, session)
base_config[environment.to_s]['sessions'][session.to_s]
end

private
def hash_to_inline_params(params)
params.collect { |key, value| "-#{ key } \"#{ value.to_s.gsub("\"", "\\\"") }\"" if value }.compact.join(' ')
end

def base_config
YAML.load_file(Rails.root.join('config/mongoid.yml'))
Expand Down

0 comments on commit 8f6b107

Please sign in to comment.