Skip to content

Commit dd51bd7

Browse files
chore: refactor sql
1 parent de41561 commit dd51bd7

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

coderd/database/dump.sql

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000284_allow_disabling_notification_templates_by_default.up.sql

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ BEGIN
88
-- * the user has disabled this notification.
99
-- * the notification template is disabled by default and hasn't
1010
-- been explicitly enabled by the user.
11-
IF EXISTS (SELECT 1
12-
FROM notification_preferences
13-
WHERE disabled = TRUE
14-
AND user_id = NEW.user_id
15-
AND notification_template_id = NEW.notification_template_id)
16-
OR (NOT EXISTS (SELECT 1
17-
FROM notification_preferences
18-
WHERE disabled = FALSE
19-
AND user_id = NEW.user_id
20-
AND notification_template_id = NEW.notification_template_id))
21-
AND (EXISTS (SELECT 1
22-
FROM notification_templates
23-
WHERE id = NEW.notification_template_id
24-
AND enabled_by_default = FALSE) ) THEN
11+
IF EXISTS (
12+
SELECT 1 FROM notification_templates
13+
LEFT JOIN notification_preferences
14+
ON notification_preferences.notification_template_id = notification_templates.id
15+
AND notification_preferences.user_id = NEW.user_id
16+
WHERE notification_templates.id = NEW.notification_template_id AND (
17+
-- Case 1: The user has explicitly disabled this template
18+
notification_preferences.disabled = TRUE
19+
OR
20+
-- Case 2: The template is disabled by default AND the user hasn't enabled it
21+
(notification_templates.enabled_by_default = FALSE AND notification_preferences.notification_template_id IS NULL)
22+
)
23+
) THEN
2524
RAISE EXCEPTION 'cannot enqueue message: notification is not enabled';
2625
END IF;
2726

0 commit comments

Comments
 (0)