Created
July 18, 2016 20:10
-
-
Save alwalker/04c3a696ad78476046ddea220ce05449 to your computer and use it in GitHub Desktop.
Rebuild your CouchDB Views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
require 'couchrest' | |
require 'net/http' | |
require 'uri' | |
require 'json/ext' | |
require 'rest-client' | |
if ARGV.length == 1 | |
puts 'Run on all database (y/n)?' | |
if STDIN.gets.chomp != 'y' | |
exit | |
end | |
couch = "http://#{ARGV[0]}:5984" | |
elsif ARGV.length > 1 | |
couch = "http://#{ARGV[0]}:5984" | |
targets = ARGV[1..-1] | |
else | |
puts "Usage: #{__FILE__} source_server [target_db...]" | |
exit 1 | |
end | |
log = open('log', 'w') | |
backup = open('design_doc_backup', 'w') | |
if(targets.nil?) | |
puts "Getting list of dbs" | |
log.puts "Getting list of dbs" | |
targets = JSON.parse(RestClient.get "#{couch}/_all_dbs").select {|db| !db.start_with?('_') and !db.end_with?("_r") and !db.start_with?("log-") and !db.start_with?("test_suite") and !db.end_with?("_backup") and !db.end_with?('_p')} | |
puts "Target dbs: #{targets}" | |
log.puts "Target dbs: #{targets}" | |
end | |
targets.each do |db| | |
puts "############CUSTOMER - #{db}############" | |
log.puts "############CUSTOMER - #{db}############" | |
couchdb = CouchRest.database("#{couch}/#{db}") | |
puts "Getting design docs" | |
log.puts "Getting design docs" | |
ddocs = JSON.parse(RestClient.get "#{couch}/#{db}/_all_docs", {:params => {:startkey => "\"_design/\"", :endkey => "\"_design0\"", :include_docs => true}})['rows'].collect {|r| r['doc']} | |
ddocs.each do |ddoc| | |
puts "---------Design Doc: #{ddoc['_id']}---------" | |
log.puts "---------Design Doc: #{ddoc['_id']}---------" | |
puts "Backuping up ddoc" | |
log.puts "Backuping up ddoc" | |
backup.puts ddoc | |
backup.puts | |
puts "Deleting" | |
log.puts "Deleting" | |
couchdb.delete_doc(ddoc) | |
puts "Cleaning up views" | |
log.puts "Cleaning up views" | |
RestClient.post "#{couch}/#{db}/_view_cleanup", nil, :content_type => :json | |
puts "Replacing design doc" | |
log.puts "Replacing design doc" | |
ddoc.delete '_rev' | |
couchdb.save_doc(ddoc) | |
view = ddoc['_id'].split("/")[1] + '/' + ddoc['views'].keys[0] | |
puts "Querying #{view}" | |
log.puts "Querying #{view}" | |
couchdb.view(view) | |
end | |
puts "########################" | |
log.puts "########################" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment