]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/rm/plugins/postfixadmin_db.rb
Move domain removal into the plugins.
[mailshears.git] / lib / rm / plugins / postfixadmin_db.rb
index cd59928007448a3bd77c400cecad60eb717f6365..1928202123e1b0316157f67552513ef796b325f4 100644 (file)
@@ -51,12 +51,15 @@ class PostfixadminDbRm
 
 
   def delete_domain(domain)
 
 
   def delete_domain(domain)
+    raise NonexistentDomainError.new(domain) if not domain_exists(domain)
+
     sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
     sql_queries << 'DELETE FROM alias WHERE domain = $1;'
     sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
     sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
     sql_queries << 'DELETE FROM log WHERE domain = $1;'
     sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
     sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
     sql_queries << 'DELETE FROM alias WHERE domain = $1;'
     sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
     sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
     sql_queries << 'DELETE FROM log WHERE domain = $1;'
     sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
+    sql_queries << 'DELETE FROM domain WHERE domain = $1;'
 
     begin
       connection = PGconn.connect(@db_host,
 
     begin
       connection = PGconn.connect(@db_host,
@@ -81,9 +84,10 @@ class PostfixadminDbRm
   end
 
 
   end
 
 
+  protected;
 
 
-  def get_domain_usernames(domain)
-    usernames = []
+  def domain_exists(domain)
+    count = 0
 
     # Just assume PostgreSQL for now.
     begin
 
     # Just assume PostgreSQL for now.
     begin
@@ -95,19 +99,23 @@ class PostfixadminDbRm
                                   @db_user,
                                   @db_pass)
 
                                   @db_user,
                                   @db_pass)
 
-      sql_query  = 'SELECT username FROM mailbox WHERE domain = $1;'
-
+      sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
       connection.query(sql_query, [domain]) do |result|
       connection.query(sql_query, [domain]) do |result|
-        usernames = result.field_values('username')
+        return false if result.ntuples() < 1
+        begin
+          count = result.getvalue(0,0).to_i()
+          return false if count.nil?
+        rescue StandardError
+          return false
+        end
       end
       end
-
       connection.close()
     rescue PGError => e
       connection.close()
     rescue PGError => e
-      # Pretend like we're database-agnostic in case we ever are.
+      # But pretend like we're database-agnostic in case we ever are.
       raise DatabaseError.new(e)
     end
 
       raise DatabaseError.new(e)
     end
 
-    return usernames
+    return (count > 0)
   end
 
 end
   end
 
 end