|
1 | 1 | CREATE TABLE notification_preferences
|
2 | 2 | (
|
3 |
| - user_id uuid REFERENCES users ON DELETE CASCADE NOT NULL, |
4 |
| - notification_template_id uuid REFERENCES notification_templates ON DELETE CASCADE NOT NULL, |
5 |
| - disabled bool NOT NULL DEFAULT FALSE, |
6 |
| - created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, |
7 |
| - updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP |
| 3 | + user_id uuid REFERENCES users ON DELETE CASCADE NOT NULL, |
| 4 | + notification_template_id uuid REFERENCES notification_templates ON DELETE CASCADE NOT NULL, |
| 5 | + disabled bool NOT NULL DEFAULT FALSE, |
| 6 | + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 7 | + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 8 | + PRIMARY KEY (user_id, notification_template_id) |
8 | 9 | );
|
9 | 10 |
|
10 | 11 | -- Ensure we cannot insert multiple entries for the same user/template combination.
|
11 | 12 | ALTER TABLE notification_preferences
|
12 |
| - ADD CONSTRAINT unique_user_notification_template UNIQUE (user_id, notification_template_id); |
| 13 | + ADD CONSTRAINT unique_user_notification_template UNIQUE (user_id, notification_template_id); |
13 | 14 |
|
14 | 15 | -- Add a new type (to be expanded upon later) which specifies the kind of notification template.
|
15 | 16 | CREATE TYPE notification_template_kind AS ENUM (
|
16 |
| - 'system' |
17 |
| - ); |
| 17 | + 'system' |
| 18 | + ); |
18 | 19 |
|
19 | 20 | ALTER TABLE notification_templates
|
20 |
| - -- Allow per-template notification method (enterprise only). |
21 |
| - ADD COLUMN method notification_method, |
22 |
| - -- Update all existing notification templates to be system templates. |
23 |
| - ADD COLUMN kind notification_template_kind DEFAULT 'system'::notification_template_kind NOT NULL; |
| 21 | + -- Allow per-template notification method (enterprise only). |
| 22 | + ADD COLUMN method notification_method, |
| 23 | + -- Update all existing notification templates to be system templates. |
| 24 | + ADD COLUMN kind notification_template_kind DEFAULT 'system'::notification_template_kind NOT NULL; |
24 | 25 | COMMENT ON COLUMN notification_templates.method IS 'NULL defers to the deployment-level method';
|
25 | 26 |
|
26 | 27 | -- No equivalent in down migration because ENUM values cannot be deleted.
|
27 | 28 | ALTER TYPE notification_message_status ADD VALUE IF NOT EXISTS 'inhibited';
|
28 | 29 |
|
29 | 30 | -- Function to prevent enqueuing notifications unnecessarily.
|
30 | 31 | CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
|
31 |
| - RETURNS TRIGGER AS |
| 32 | + RETURNS TRIGGER AS |
32 | 33 | $$
|
33 | 34 | BEGIN
|
34 |
| - -- Fail the insertion if the user has disabled this notification. |
35 |
| - IF EXISTS (SELECT 1 |
36 |
| - FROM notification_preferences |
37 |
| - WHERE disabled = TRUE |
38 |
| - AND user_id = NEW.user_id |
39 |
| - AND notification_template_id = NEW.notification_template_id) THEN |
40 |
| - RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification'; |
41 |
| - END IF; |
42 |
| - |
43 |
| - RETURN NEW; |
| 35 | + -- Fail the insertion if the user has disabled this notification. |
| 36 | + IF EXISTS (SELECT 1 |
| 37 | + FROM notification_preferences |
| 38 | + WHERE disabled = TRUE |
| 39 | + AND user_id = NEW.user_id |
| 40 | + AND notification_template_id = NEW.notification_template_id) THEN |
| 41 | + RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification'; |
| 42 | + END IF; |
| 43 | + |
| 44 | + RETURN NEW; |
44 | 45 | END;
|
45 | 46 | $$ LANGUAGE plpgsql;
|
46 | 47 |
|
47 | 48 | -- Trigger to execute above function on insertion.
|
48 | 49 | CREATE TRIGGER inhibit_enqueue_if_disabled
|
49 |
| - BEFORE INSERT |
50 |
| - ON notification_messages |
51 |
| - FOR EACH ROW |
| 50 | + BEFORE INSERT |
| 51 | + ON notification_messages |
| 52 | + FOR EACH ROW |
52 | 53 | EXECUTE FUNCTION inhibit_enqueue_if_disabled();
|
53 | 54 |
|
54 | 55 | -- Allow modifications to notification templates to be audited.
|
|
0 commit comments