Document everything with YARD and fix some bugs along the way.
[mailshears.git] / lib / mv / plugins / davical.rb
1 require 'pg'
2
3 require 'common/davical_plugin'
4 require 'rm/rm_plugin'
5
6 # Handle moving (renaming) DAViCal users in its database. DAViCal has
7 # no concept of domains.
8 #
9 class DavicalMv
10 include DavicalPlugin
11 include MvPlugin
12
13
14 # Move the user *src* to *dst* within the DAViCal database. This
15 # should "rename" him in _every_ table where he is referenced.
16 # DAViCal uses foreign keys properly, so we let the ON UPDATE
17 # CASCADE trigger handle most of the work.
18 #
19 # This can fail is *src* does not exist, or if *dst* already exists
20 # before the move. It should also be an error if the destination
21 # domain doesn't exist. But DAViCal doesn't know about domains, so
22 # we let that slide.
23 #
24 # @param src [User] the source user to be moved.
25 #
26 # @param dst [User] the destination user being moved to.
27 #
28 def mv_user(src, dst)
29 raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
30 raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
31
32 sql_queries = ['UPDATE usr SET username = $1 WHERE username = $2']
33
34 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
35 @db_name, @db_user, @db_pass)
36
37 sql_queries.each do |sql_query|
38 connection.query(sql_query, [dst.to_s(), src.to_s()])
39 end
40
41 connection.close()
42 end
43
44 end