Skip to content

Commit

Permalink
Added migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
bwilk committed Feb 10, 2017
1 parent f5bf888 commit 814c311
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions migrate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "couchrest"
require 'optparse'

class Options

def self.parse(args)
options = OpenStruct.new
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: migrate.rb [options]"
opts.on('-s', '--source HOST', 'Source HOST endpoint') { |v| options.source = v }
opts.on('-i', '--source-db NAME', 'Source database NAME') { |v| options.source_db = v }
opts.on('-t', '--target HOST', 'Target HOST endpoint') { |v| options.target = v }
opts.on('-o', '--target-db NAME', 'Target database NAME') { |v| options.target_db = v }
if options[:source].nil? then options[:source] = "http://localhost:5984" end
if options.target.nil? then options.target = "http://localhost:5984" end
end
opt_parser.parse!(args)
options
end

end

def migrate(options)
docs = CouchRest.get(options.source+"/"+ options.source_db+"/_all_docs?include_docs=true")
db_target = CouchRest.new(options.target).database(options.target_db)
docs["rows"].each do |row|
row["doc"].delete("_rev")
if (!row["id"].start_with?("_"))
begin
db_target.save_doc(row["doc"]);
rescue => exception
puts "Eror while saving #{row["doc"]}"
puts exception
end
end
end
end

options = Options.parse(ARGV)
puts options
migrate(options)

0 comments on commit 814c311

Please sign in to comment.