7f98aa609e22bd170b16d4a395fb30bd5c8c2e1b
[couchdb_scripts.git] / ruby / destroy-all-test-users
1 #!/usr/bin/ruby
2
3 #
4 # Clean up cruft left over by bad tests.
5 #
6 # Removes all 'test_user_x' users from the users db, along with the
7 # corresponding indentities and storage db.
8 #
9
10 require 'couchrest'
11
12 users_db_name = 'users'
13 #user_db_name = 'tmp_users'
14 identities_db_name = 'identities'
15
16 netrc = File.read('/root/.netrc').split(' ')
17 auth = "%{username}:%{password}@" % {username: netrc[3], password: netrc[5]}
18 server = CouchRest::Server.new("http://#{auth}localhost:5984")
19 users_db = server.database(users_db_name)
20 identities_db = server.database(identities_db_name)
21
22 records = users_db.view('User/by_login', :reduce => false, :startkey => 'test_user_', :endkey => 'test_user_'+"\u{fff}")['rows']
23 records.each do |record|
24   user_id = record['id']
25   username = record['key']
26   begin
27     doc = users_db.get(user_id)
28     users_db.delete_doc(doc)
29     puts "deleted #{users_db.name}/#{user_id} (#{username})"
30   rescue RestClient::ResourceNotFound
31   end
32   begin
33     storage_db = server.database("user-" + user_id)
34     storage_db.delete!
35     puts "  deleted #{storage_db.name}"
36   rescue RestClient::ResourceNotFound
37   end
38   identities_db.view('Identity/by_user_id', :reduce => false, :startkey => user_id, :endkey => user_id)['rows'].each do |row|
39     begin
40       doc = identities_db.get(row['id'])
41       identities_db.delete_doc(doc)
42       puts "  deleted #{identities_db.name}/#{row['id']}"
43     rescue RestClient::ResourceNotFound
44     end
45   end
46 end
47
48 users_db.compact!
49 identities_db.compact!
50
51 puts "DONE"