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.nix37
1 files changed, 34 insertions, 3 deletions
diff --git a/hosts/surtr/postgresql.nix b/hosts/surtr/postgresql.nix
index abd2cb26..a5e93ecf 100644
--- a/hosts/surtr/postgresql.nix
+++ b/hosts/surtr/postgresql.nix
@@ -1,4 +1,4 @@
1{ pkgs, sources, ... }: 1{ pkgs, sources, config, ... }:
2let 2let
3 versioning = sources.psql-versioning.src; 3 versioning = sources.psql-versioning.src;
4in { 4in {
@@ -22,8 +22,19 @@ in {
22 ''; 22 '';
23 }; 23 };
24 24
25 systemd.services.postgresql = { 25 systemd.services.migrate-postgresql = {
26 postStart = '' 26 after = [ "postgresql.service" ];
27 bindsTo = [ "postgresql.service" ];
28 wantedBy = [ "postgresql.service" ];
29
30 serviceConfig = {
31 Type = "oneshot";
32 inherit (config.systemd.services.postgresql.serviceConfig) User Group;
33 RemainAfterExit = true;
34 };
35
36 path = [ config.services.postgresql.package ];
37 script = ''
27 psql email postgres -eXf ${pkgs.writeText "email.sql" '' 38 psql email postgres -eXf ${pkgs.writeText "email.sql" ''
28 \i ${versioning + "/install.versioning.sql"} 39 \i ${versioning + "/install.versioning.sql"}
29 40
@@ -78,6 +89,26 @@ in {
78 CREATE VIEW imap_user ("user", quota_rule) AS SELECT mailbox AS "user", quota_rule FROM mailbox_quota_rule; 89 CREATE VIEW imap_user ("user", quota_rule) AS SELECT mailbox AS "user", quota_rule FROM mailbox_quota_rule;
79 CREATE VIEW lmtp_mapping ("user", quota_rule, local, domain) AS SELECT mailbox_quota_rule.mailbox AS "user", quota_rule, local, domain FROM mailbox_quota_rule INNER JOIN mailbox_mapping ON mailbox_quota_rule.id = mailbox_mapping.mailbox; 90 CREATE VIEW lmtp_mapping ("user", quota_rule, local, domain) AS SELECT mailbox_quota_rule.mailbox AS "user", quota_rule, local, domain FROM mailbox_quota_rule INNER JOIN mailbox_mapping ON mailbox_quota_rule.id = mailbox_mapping.mailbox;
80 COMMIT; 91 COMMIT;
92
93 BEGIN;
94 SELECT _v.register_patch('003-extensions', ARRAY['000-base', '002-citext'], null);
95
96 ALTER TABLE mailbox_mapping ADD COLUMN extension citext CHECK (CASE WHEN extension IS NOT NULL THEN extension NOT LIKE '%+%' ELSE true END);
97
98 DROP VIEW virtual_mailbox_mapping;
99 DROP VIEW lmtp_mapping;
100
101 CREATE VIEW virtual_mailbox_mapping (lookup) AS SELECT (CASE WHEN local IS NULL THEN ''' ELSE local END) || (CASE WHEN extension IS NULL THEN ''' ELSE '+' || extension END) || '@' || domain AS lookup FROM mailbox_mapping WHERE mailbox IS NOT NULL;
102 CREATE VIEW virtual_mailbox_access (lookup, action) AS SELECT (CASE WHEN local IS NULL THEN ''' ELSE local END) || (CASE WHEN extension IS NULL THEN ''' ELSE '+' || extension END) || '@' || domain AS lookup, CASE WHEN mailbox IS NULL THEN 'REJECT' ELSE 'DUNNO' END AS action FROM mailbox_mapping;
103 CREATE VIEW lmtp_mapping ("user", quota_rule, local, extension, domain) AS SELECT mailbox_quota_rule.mailbox AS "user", quota_rule, local, extension, domain FROM mailbox_quota_rule INNER JOIN mailbox_mapping ON mailbox_quota_rule.id = mailbox_mapping.mailbox;
104 COMMIT;
105
106 BEGIN;
107 SELECT _v.register_patch('004-cascade', ARRAY['000-base'], null);
108
109 ALTER TABLE mailbox_mapping DROP CONSTRAINT mailbox_mapping_mailbox_fkey;
110 ALTER TABLE mailbox_mapping ADD CONSTRAINT mailbox_mapping_mailbox_fkey FOREIGN KEY (mailbox) REFERENCES mailbox(id) ON DELETE CASCADE ON UPDATE RESTRICT;
111 COMMIT;
81 ''} 112 ''}
82 ''; 113 '';
83 }; 114 };