Wrap all close() calls in "ensure" blocks and simplify DB connection-making.
[mailshears.git] / lib / rm / plugins / roundcube.rb
1 require 'pg'
2
3 require 'common/roundcube_plugin'
4 require 'rm/rm_plugin'
5
6 # Handle removal of Roundcube users from its database. Roundcube has
7 # no concept of domains.
8 #
9 class RoundcubeRm
10
11 include RoundcubePlugin
12 include RmPlugin
13
14 # Remove *user* from the Roundcube database. This should remove him
15 # from _every_ table in which he is referenced. Fortunately the
16 # Roundcube developers were nice enough to include DBMS-specific
17 # install and upgrade scripts, so Postgres can take advantage of ON
18 # DELETE triggers.
19 #
20 # @param user [User] the user to remove.
21 #
22 def remove_user(user)
23 raise NonexistentUserError.new(user.to_s()) if not user_exists(user)
24
25 # Get the primary key for this user in the "users" table.
26 user_id = self.get_user_id(user)
27
28 # Thanks to the ON DELETE triggers, this will remove all child
29 # records associated with user_id too.
30 sql_query = 'DELETE FROM users WHERE user_id = $1::int;'
31
32 connection = PG::Connection.connect(@db_hash)
33
34 begin
35 connection.query(sql_query, [user_id])
36 ensure
37 # Make sure the connection gets closed even if the query explodes.
38 connection.close()
39 end
40 end
41
42 end