-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor SlackMessage.channel_id
(CHAR
field) to SlackMessage.channel
(foreign key relationship)
#5292
base: dev
Are you sure you want to change the base?
refactor SlackMessage.channel_id
(CHAR
field) to SlackMessage.channel
(foreign key relationship)
#5292
Conversation
# Don't send request for permalink if there is no slack_team_identity or slack token has been revoked | ||
if self.cached_permalink or not self.slack_team_identity or self.slack_team_identity.detected_token_revoked: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I verified (in production) and don't see any records where slack_team_identity
is null for SlackMessage
(fwiw, I think this field should eventually be dropped from this model and we should use slack_message.channel.slack_team_identity
instead; see comments above)
logger.warning( | ||
f"SlackMessage (pk: {self.pk}) fields _slack_team_identity and organization is None. " | ||
f"It is strange!" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to find any mentions of this log message for the last 90 days + there's no git history available unfortunately for why this is here. As I am proposing to (eventually) drop slack_team_identity
from this model, I propose to remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main change
elif db_type == settings.DATABASE_TYPES.MYSQL: | ||
sql = f""" | ||
UPDATE | ||
{SlackMessage._meta.db_table} AS sm | ||
JOIN {SlackChannel._meta.db_table} AS sc | ||
ON sc.slack_id = sm._channel_id | ||
AND sc.slack_team_identity_id = sm.slack_team_identity | ||
SET | ||
sm.channel_id = sc.id | ||
WHERE | ||
sm._channel_id IS NOT NULL | ||
AND sm.slack_team_identity IS NOT NULL; | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main change. Added queries for sqlite/postgres here to avoid #5244 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: I'm planning on running this SQL statement against a prod clone just to get an idea of performance before merging this PR
|
||
def send_slack_notification(self, user, alert_group, notification_policy): | ||
def send_slack_notification(self, user: "User", notification_policy: "UserNotificationPolicy") -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given that this is an instance method (only used here), self.alert_group
already is a thing, so I don't think we need to re-pass in alert_group
?
def slack_channel(self) -> typing.Optional["SlackChannel"]: | ||
""" | ||
This is only set if the schedule associated with the shift swap request | ||
has a Slack channel configured for it. | ||
""" | ||
return self.schedule.slack_channel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added for this
channel = models.ForeignKey( | ||
"slack.SlackChannel", on_delete=models.CASCADE, null=True, default=None, related_name="slack_messages" | ||
) | ||
""" | ||
TODO: once we've migrated the data in `_channel_id` to this field, set `null=False` | ||
as we should always have a `channel` associated with a message | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if Slack channel gets deleted (for example here)? will it still be possible to use SlackMessage.permalink
to redirect users to Slack?
on_delete=models.CASCADE
this means if alert group was posted to #alerts
and then #alerts
is deleted, Slack messages will be deleted and we will not be able to reference them (for things like Slack permalink, resolution notes, etc.). I think we don't want that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good question! I was also questioning what the right on_delete
would be here. My thought was that if you delete a Slack channel.. everything inside the channel vanishes. So permalinks/resolution notes/etc are no longer relevant for those SlackMessage
s (but maybe I'm overlooking something?)
What this PR does
Related to https://github.com/grafana/oncall-private/issues/2947
I’m tackling this work now because ultimately I want to move
AlertReceiveChannel.rate_limited_in_slack_at
toSlackChannel.rate_limited_at
, but first I sorta need to refactorSlackMessage.channel_id
from aCHAR
field to a foreign key relationship (because in the spots where we touch Slack rate limiting, like here for example, we only haveslack_message.channel_id
, which means I need to do extra queries to fetch the appropriateSlackChannel
to then be able to get/setSlackChannel.rate_limited_at
Checklist
pr:no public docs
PR label added if not required)release:
). These labels dictate how your PR willshow up in the autogenerated release notes.