pgsql/create_tables.pgsql
branchv0.6.x
changeset 512 821d3ffaaaca
parent 511 0244f1344b04
child 514 d863a44a6353
--- a/pgsql/create_tables.pgsql	Tue Apr 10 00:33:53 2012 +0200
+++ b/pgsql/create_tables.pgsql	Tue Apr 10 00:50:55 2012 +0200
@@ -576,13 +576,30 @@
                 RETURN NEXT record;
             END LOOP;
         IF NOT FOUND THEN
+            -- First, we have to check existing mailboxes and
+            -- return identity if one exists
             FOR record IN
-                SELECT recipient, destination
-                  FROM catchall
+                SELECT recipient, recipient as destination
+                  FROM users
                  WHERE gid = did
+                   AND local_part = localpart
+                UNION SELECT recipient, recipient as destination
+                  FROM relocated
+                 WHERE gid = did
+                   AND address = localpart
                 LOOP
                     RETURN NEXT record;
                 END LOOP;
+            -- Only now can we think about catchalls...
+            IF NOT FOUND THEN
+                FOR record IN
+                    SELECT recipient, destination
+                      FROM catchall
+                    WHERE gid = did
+                    LOOP
+                        RETURN NEXT record;
+                    END LOOP;
+            END IF;
         END IF;
         RETURN;
     END;