]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/mv/plugins/roundcube.rb
Wrap all close() calls in "ensure" blocks and simplify DB connection-making.
[mailshears.git] / lib / mv / plugins / roundcube.rb
index 4d8196461ca570a8e51a5aa0880365dda8d950f5..5cd20c77064532988bff689bcf52f43c49b67692 100644 (file)
@@ -30,16 +30,15 @@ class RoundcubeMv
     raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
     raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
 
-    sql_queries = ['UPDATE users SET username = $1 WHERE username = $2;']
+    sql_query = 'UPDATE users SET username = $1 WHERE username = $2;'
 
-    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
-                                @db_name, @db_user, @db_pass)
-
-    sql_queries.each do |sql_query|
+    connection = PG::Connection.new(@db_hash)
+    begin
       connection.query(sql_query, [dst.to_s(), src.to_s()])
+    ensure
+      # Make sure the connection gets closed even if the query explodes.
+      connection.close()
     end
-
-    connection.close()
   end
 
 end