summaryrefslogtreecommitdiff
path: root/destroy-all-test-users
blob: b578f83518ce9245339142311aedc654ecf32cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/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"