Commits

Zhang Huangbin  committed 8ed8354

Fixed: Dovecot cannot store realtime quota usage in PostgreSQL.

  • Participants
  • Parent commits 0f61562

Comments (0)

Files changed (2)

File iRedMail/ChangeLog

-iRedMail-0.8.0:
+iRedMail-0.8.0-beta2:
+    * Fixed:
+        + Dovecot cannot store realtime quota usage in PostgreSQL.
+
+iRedMail-0.8.0-beta1:
+    * It now supports PostgreSQL as backend.
     * Package update:
         + Roundcube webmail -> 0.7.1
 

File iRedMail/samples/iredmail.pgsql

     messages INT8 NOT NULL DEFAULT 0,
     PRIMARY KEY (username)
 );
+
+-- Trigger required by quota dict
+CREATE OR REPLACE FUNCTION merge_quota() RETURNS TRIGGER AS $$
+BEGIN
+  IF NEW.messages < 0 OR NEW.messages IS NULL THEN
+    -- ugly kludge: we came here from this function, really do try to insert
+    IF NEW.messages IS NULL THEN
+      NEW.messages = 0;
+    ELSE
+      NEW.messages = -NEW.messages;
+    END IF;
+    return NEW;
+  END IF;
+
+  LOOP
+    UPDATE used_quota SET bytes = bytes + NEW.bytes,
+      messages = messages + NEW.messages
+      WHERE username = NEW.username;
+    IF found THEN
+      RETURN NULL;
+    END IF;
+
+    BEGIN
+      IF NEW.messages = 0 THEN
+        INSERT INTO used_quota (bytes, messages, username)
+          VALUES (NEW.bytes, NULL, NEW.username);
+      ELSE
+        INSERT INTO used_quota (bytes, messages, username)
+          VALUES (NEW.bytes, -NEW.messages, NEW.username);
+      END IF;
+      return NULL;
+    EXCEPTION WHEN unique_violation THEN
+      -- someone just inserted the record, update it
+    END;
+  END LOOP;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER mergequota BEFORE INSERT ON used_quota
+   FOR EACH ROW EXECUTE PROCEDURE merge_quota();