Overhaul everything to get consistent error reports.
[mailshears.git] / lib / mv / plugins / davical.rb
1 require 'pg'
2
3 require 'common/davical_plugin'
4 require 'rm/rm_plugin'
5
6 class DavicalMv
7 #
8 # DAViCal only supports Postgres, so even if we ever are
9 # database-agnostic, this plugin can't be.
10 #
11 include DavicalPlugin
12 include MvPlugin
13
14
15 def mv_user(src, dst)
16 # Switch the given usernames. DAViCal uses foreign keys properly
17 # and only supports postgres, so we let the ON UPDATE CASCADE
18 # trigger handle most of the work.
19
20 # It's obviously an error if the source user does not exist. It
21 # would also be an error if the destination domain didn't exist;
22 # however, DAViCal doesn't know about domains, so we let that slide.
23 raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
24
25 # And it's an error if the destination user exists already.
26 raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
27
28 sql_queries = ['UPDATE usr SET username = $1 WHERE username = $2']
29
30 begin
31 connection = PGconn.connect(@db_host,
32 @db_port,
33 @db_opts,
34 @db_tty,
35 @db_name,
36 @db_user,
37 @db_pass)
38
39 sql_queries.each do |sql_query|
40 connection.query(sql_query, [dst.to_s(), src.to_s()])
41 end
42
43 connection.close()
44
45 rescue PGError => e
46 # Pretend like we're database-agnostic in case we ever are.
47 raise DatabaseError.new(e)
48 end
49
50 end
51
52 end