summaryrefslogtreecommitdiff
path: root/hosts/surtr/postgresql.nix
diff options
context:
space:
mode:
Diffstat (limited to 'hosts/surtr/postgresql.nix')
-rw-r--r--hosts/surtr/postgresql.nix12
1 files changed, 9 insertions, 3 deletions
diff --git a/hosts/surtr/postgresql.nix b/hosts/surtr/postgresql.nix
index 7b3b8c74..d8f66fcc 100644
--- a/hosts/surtr/postgresql.nix
+++ b/hosts/surtr/postgresql.nix
@@ -30,17 +30,23 @@ in {
30 BEGIN; 30 BEGIN;
31 SELECT _v.register_patch('000-base', null, null); 31 SELECT _v.register_patch('000-base', null, null);
32 32
33 CREATE TABLE virtual_mailbox_mapping ( 33 CREATE TABLE mailbox (
34 id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
35 mailbox text NOT NULL CONSTRAINT mailbox_non_empty CHECK (mailbox <> '''),
36 quota_bytes bigint CONSTRAINT quota_bytes_positive CHECK (CASE WHEN quota_bytes IS NOT NULL THEN quota_bytes > 0 ELSE true),
37 quota_rule text GENERATED ALWAYS AS (CASE WHEN quota_bytes IS NULL THEN '*:ignore' ELSE '*:bytes=' || quota_bytes) STORED
38 )
39 CREATE TABLE mailbox_mapping (
34 id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(), 40 id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
35 local text CONSTRAINT local_non_empty CHECK (local IS DISTINCT FROM '''), 41 local text CONSTRAINT local_non_empty CHECK (local IS DISTINCT FROM '''),
36 domain text NOT NULL CONSTRAINT domain_non_empty CHECK (domain <> '''), 42 domain text NOT NULL CONSTRAINT domain_non_empty CHECK (domain <> '''),
37 mailbox text NOT NULL CONSTRAINT mailbox_non_empty CHECK (mailbox <> '''), 43 mailbox uuid REFERENCES virtual_mailbox(id)
38 CONSTRAINT local_domain_unique UNIQUE (local, domain) 44 CONSTRAINT local_domain_unique UNIQUE (local, domain)
39 ); 45 );
40 CREATE UNIQUE INDEX domain_unique ON virtual_mailbox_mapping (domain) WHERE local IS NULL; 46 CREATE UNIQUE INDEX domain_unique ON virtual_mailbox_mapping (domain) WHERE local IS NULL;
41 47
42 CREATE VIEW virtual_mailbox_domain (domain) AS SELECT DISTINCT domain FROM virtual_mailbox_mapping; 48 CREATE VIEW virtual_mailbox_domain (domain) AS SELECT DISTINCT domain FROM virtual_mailbox_mapping;
43 CREATE VIEW virtual_mailbox (mailbox) AS SELECT DISTINCT mailbox FROM virtual_mailbox_mapping; 49 CREATE VIEW virtual_mailbox_mapping (mailbox, lookup) AS SELECT mailbox.mailbox as mailbox, (CASE WHEN local IS NULL THEN ''' ELSE local) || '@' || domain AS lookup FROM mailbox_mapping INNER JOIN mailbox on mailbox.id = mailbox_mapping.mailbox;
44 COMMIT; 50 COMMIT;
45 ''} 51 ''}
46 ''; 52 '';