|
1 | 1 | CREATETABLEnotification_preferences
|
2 | 2 | (
|
3 |
| - user_id uuidREFERENCES usersON DELETE CASCADENOT NULL, |
4 |
| - notification_template_id uuidREFERENCES notification_templatesON DELETE CASCADENOT NULL, |
5 |
| - disabled boolNOT NULL DEFAULT FALSE, |
6 |
| - created_atTIMESTAMP WITH TIME ZONENOT NULL DEFAULTCURRENT_TIMESTAMP, |
7 |
| - updated_atTIMESTAMP WITH TIME ZONENOT NULL DEFAULTCURRENT_TIMESTAMP |
| 3 | +user_id uuidREFERENCES usersON DELETE CASCADENOT NULL, |
| 4 | +notification_template_id uuidREFERENCES notification_templatesON DELETE CASCADENOT NULL, |
| 5 | +disabled boolNOT NULL DEFAULT FALSE, |
| 6 | +created_atTIMESTAMP WITH TIME ZONENOT NULL DEFAULTCURRENT_TIMESTAMP, |
| 7 | +updated_atTIMESTAMP WITH TIME ZONENOT NULL DEFAULTCURRENT_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 | ALTERTABLE notification_preferences
|
12 |
| -ADDCONSTRAINT unique_user_notification_template UNIQUE (user_id, notification_template_id); |
| 13 | +ADDCONSTRAINT 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 | CREATETYPEnotification_template_kindAS ENUM (
|
16 |
| -'system' |
17 |
| -); |
| 17 | +'system' |
| 18 | +); |
18 | 19 |
|
19 | 20 | ALTERTABLE 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_kindNOT 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_kindNOT 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 | ALTERTYPE notification_message_status ADD VALUE IF NOT EXISTS'inhibited';
|
28 | 29 |
|
29 | 30 | -- Function to prevent enqueuing notifications unnecessarily.
|
30 | 31 | CREATE OR REPLACEFUNCTIONinhibit_enqueue_if_disabled()
|
31 |
| -RETURNS TRIGGERAS |
| 32 | +RETURNS TRIGGERAS |
32 | 33 | $$
|
33 | 34 | BEGIN
|
34 |
| --- Fail the insertion if the user has disabled this notification. |
35 |
| -IF EXISTS (SELECT1 |
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 (SELECT1 |
| 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 | CREATETRIGGERinhibit_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.
|
|