20c8a32af50d668fe8ccba1f690091cbddcafe9a
[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 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
31 @db_name, @db_user, @db_pass)
32
33 sql_queries.each do |sql_query|
34 connection.query(sql_query, [dst.to_s(), src.to_s()])
35 end
36
37 connection.close()
38 end
39
40 end