forked from pedro/heroku-mongo-sync
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo.rb
74 lines (64 loc) · 2.22 KB
/
mongo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Heroku::Command
class Mongo < BaseWithApp
def initialize(*args)
super
require 'mongo'
rescue LoadError
error "Install the Mongo gem to use mongo commands:\nsudo gem install mongo"
end
def push
display "THIS WILL REPLACE ALL DATA for #{app} ON #{heroku_mongo_uri.host} WITH #{local_mongo_uri.host}"
display "Are you sure? (y/n) ", false
return unless ask.downcase == 'y'
transfer(local_mongo_uri, heroku_mongo_uri)
end
def pull
display "Replacing the #{app} db at #{local_mongo_uri.host} with #{heroku_mongo_uri.host}"
transfer(heroku_mongo_uri, local_mongo_uri)
end
protected
def transfer(from, to)
origin = make_connection(from)
dest = make_connection(to)
origin.collections.each do |col|
display "Syncing #{col.name} (#{col.size})...", false
dest.drop_collection(col.name)
dest_col = dest.create_collection(col.name)
col.find().each do |record|
dest_col.insert record
end
display " done"
end
end
def heroku_mongo_uri
config = heroku.config_vars(app)
url = config['MONGO_URL'] || config['MONGOHQ_URL']
error("Could not find the MONGO_URL for #{app}") unless url
make_uri(url)
end
def local_mongo_uri
url = ENV['MONGO_URL'] || "mongo://localhost:27017/#{app}"
make_uri(url)
end
def make_uri(url)
url.gsub!('local.mongohq.com', 'mongohq.com')
uri = URI.parse(url)
raise URI::InvalidURIError unless uri.host
uri
rescue URI::InvalidURIError
error("Invalid mongo url: #{url}")
end
def make_connection(uri)
connection = ::Mongo::Connection.new(uri.host, uri.port)
db = connection.db(uri.path.gsub(/^\//, ''))
db.authenticate(uri.user, uri.password) if uri.user
db
rescue ::Mongo::ConnectionFailure
error("Could not connect to the mongo server at #{uri}")
end
Help.group 'Mongo' do |group|
group.command 'mongo:push', 'push the local mongo database'
group.command 'mongo:pull', 'pull from the production mongo database'
end
end
end