2009-03-15 02:42:34 +01:00
|
|
|
module CouchRest
|
|
|
|
class Upgrade
|
|
|
|
attr_accessor :olddb, :newdb, :dbname
|
|
|
|
def initialize dbname, old_couch, new_couch
|
|
|
|
@dbname = dbname
|
|
|
|
@olddb = old_couch.database dbname
|
|
|
|
@newdb = new_couch.database!(dbname)
|
|
|
|
@bulk_docs = []
|
|
|
|
end
|
|
|
|
def clone!
|
|
|
|
puts "#{dbname} - #{olddb.info['doc_count']} docs"
|
|
|
|
streamer = CouchRest::Streamer.new(olddb)
|
|
|
|
streamer.view("_all_docs_by_seq") do |row|
|
2009-03-15 03:00:26 +01:00
|
|
|
load_row_docs(row) if row
|
2009-03-15 02:42:34 +01:00
|
|
|
maybe_flush_bulks
|
|
|
|
end
|
|
|
|
flush_bulks!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def maybe_flush_bulks
|
2009-03-15 03:00:26 +01:00
|
|
|
flush_bulks! if (@bulk_docs.length > 99)
|
2009-03-15 02:42:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def flush_bulks!
|
|
|
|
url = CouchRest.paramify_url "#{@newdb.uri}/_bulk_docs", {:all_or_nothing => true}
|
2009-03-15 03:00:26 +01:00
|
|
|
puts "posting #{@bulk_docs.length} bulk docs to #{url}"
|
2009-03-15 02:42:34 +01:00
|
|
|
begin
|
|
|
|
CouchRest.post url, {:docs => @bulk_docs}
|
2009-03-15 03:00:26 +01:00
|
|
|
@bulk_docs = []
|
2009-03-15 02:42:34 +01:00
|
|
|
rescue Exception => e
|
|
|
|
puts e.response
|
2009-03-15 03:00:26 +01:00
|
|
|
raise e
|
2009-03-15 02:42:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-15 03:00:26 +01:00
|
|
|
def load_row_docs(row)
|
2009-03-15 02:42:34 +01:00
|
|
|
results = @olddb.get(row["id"], {:open_revs => "all", :attachments => true})
|
|
|
|
results.select{|r|r["ok"]}.each do |r|
|
|
|
|
doc = r["ok"]
|
2009-03-15 03:00:26 +01:00
|
|
|
if /^_/.match(doc["_id"]) && !/^_design/.match(doc["_id"])
|
|
|
|
puts "invalid docid #{doc["_id"]} -- trimming"
|
|
|
|
doc["_id"] = doc["_id"].sub('_','')
|
|
|
|
end
|
2009-03-15 02:42:34 +01:00
|
|
|
doc.delete('_rev')
|
|
|
|
@bulk_docs << doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|