#!/usr/bin/ruby # # Clean up cruft left over by bad tests. # # Removes all 'test_user_x' users from the users db, along with the # corresponding indentities and storage db. # require 'couchrest' users_db_name = 'users' #user_db_name = 'tmp_users' identities_db_name = 'identities' netrc = File.read('/root/.netrc').split(' ') auth = "%{username}:%{password}@" % {username: netrc[3], password: netrc[5]} server = CouchRest::Server.new("http://#{auth}localhost:5984") users_db = server.database(users_db_name) identities_db = server.database(identities_db_name) records = users_db.view('User/by_login', :reduce => false, :startkey => 'test_user_', :endkey => 'test_user_'+"\u{fff}")['rows'] records.each do |record| user_id = record['id'] username = record['key'] tries = 0 begin begin doc = users_db.get(user_id) users_db.delete_doc(doc) puts "deleted #{users_db.name}/#{user_id} (#{username})" rescue RestClient::ResourceNotFound end begin storage_db = server.database("user-" + user_id) storage_db.delete! puts " deleted #{storage_db.name}" rescue RestClient::ResourceNotFound end identities_db.view('Identity/by_user_id', :reduce => false, :startkey => user_id, :endkey => user_id)['rows'].each do |row| begin doc = identities_db.get(row['id']) identities_db.delete_doc(doc) puts " deleted #{identities_db.name}/#{row['id']}" rescue RestClient::ResourceNotFound end end rescue RestClient::RequestTimeout tries += 1 if tries < 10 puts "Got timeout, retrying" sleep 10 retry else puts "Retry limit reached" exit(1) end end end users_db.compact! identities_db.compact! puts "DONE"