Skip to content

Commit f19a3d7

Browse files
vbrovolokluev
authored andcommitted
chore(hc): add check constraint on NotificationSetting team or user (#48820)
1 parent b6d6bd7 commit f19a3d7

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

migrations_lockfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ To resolve this, rebase against latest master and regenerate your migration. Thi
66
will then be regenerated, and you should be able to merge without conflicts.
77

88
nodestore: 0002_nodestore_no_dictfield
9-
sentry: 0442_remove_use_case_id_default_perf_indexer
9+
sentry: 0443_check_notification_team_or_user
1010
social_auth: 0001_initial
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Generated by Django 2.2.28 on 2023-05-15 20:44
2+
3+
from django.db import migrations, models
4+
5+
from sentry.new_migrations.migrations import CheckedMigration
6+
7+
8+
class Migration(CheckedMigration):
9+
# This flag is used to mark that a migration shouldn't be automatically run in production. For
10+
# the most part, this should only be used for operations where it's safe to run the migration
11+
# after your code has deployed. So this should not be used for most operations that alter the
12+
# schema of a table.
13+
# Here are some things that make sense to mark as dangerous:
14+
# - Large data migrations. Typically we want these to be run manually by ops so that they can
15+
# be monitored and not block the deploy for a long period of time while they run.
16+
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
17+
# have ops run this and not block the deploy. Note that while adding an index is a schema
18+
# change, it's completely safe to run the operation after the code has deployed.
19+
is_dangerous = True
20+
21+
dependencies = [
22+
("sentry", "0442_remove_use_case_id_default_perf_indexer"),
23+
]
24+
25+
operations = [
26+
migrations.AddConstraint(
27+
model_name="notificationsetting",
28+
constraint=models.CheckConstraint(
29+
check=models.Q(
30+
models.Q(("team_id__isnull", False), ("user_id__isnull", True)),
31+
models.Q(("team_id__isnull", True), ("user_id__isnull", False)),
32+
_connector="OR",
33+
),
34+
name="notification_team_or_user_check",
35+
),
36+
),
37+
]

src/sentry/models/notificationsetting.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ class Meta:
118118
"type",
119119
),
120120
)
121+
constraints = [
122+
models.CheckConstraint(
123+
check=models.Q(team_id__isnull=False, user_id__isnull=True)
124+
| models.Q(team_id__isnull=True, user_id__isnull=False),
125+
name="notification_team_or_user_check",
126+
)
127+
]
121128

122129
__repr__ = sane_repr(
123130
"scope_str",

tests/sentry/migrations/test_0439_backfill_notificationsetting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from sentry.models.actor import ACTOR_TYPES, Actor
24
from sentry.models.notificationsetting import NotificationSetting
35
from sentry.notifications.types import (
@@ -9,6 +11,7 @@
911
from sentry.types.integrations import ExternalProviders
1012

1113

14+
@pytest.mark.skip("Test setup no longer valid after adding notification_team_or_user_check")
1215
class BackfillNotificationSettingTest(TestMigrations):
1316
migrate_from = "0438_break_inviter_fk_organizationmember"
1417
migrate_to = "0439_backfill_notificationsetting"

0 commit comments

Comments
 (0)