-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge alert group static labels to integration labels (#5262)
# What this PR does - Adds migration to merge static labels to integration labels. On creating new static label in UI saves it as integration label. - Removes "inheritable" option for integration labels. All integration labels will be inheritable. This PR should be merged together with frontend changes. ## Which issue(s) this PR closes Related to grafana/oncall-private#2973 <!-- *Note*: If you want the issue to be auto-closed once the PR is merged, change "Related to" to "Closes" in the line above. If you have more than one GitHub issue that this PR closes, be sure to preface each issue link with a [closing keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue). This ensures that the issue(s) are auto-closed once the PR has been merged. --> ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes.
- Loading branch information
Showing
8 changed files
with
169 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Generated by Django 4.2.15 on 2024-11-12 09:33 | ||
import logging | ||
|
||
from django.db import migrations | ||
import django_migration_linter as linter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def migrate_static_labels(apps, schema_editor): | ||
AlertReceiveChannelAssociatedLabel = apps.get_model("labels", "AlertReceiveChannelAssociatedLabel") | ||
AlertReceiveChannel = apps.get_model("alerts", "AlertReceiveChannel") | ||
|
||
logging.info("Start migrating alert group static labels to integration labels") | ||
|
||
labels_associations_to_create = [] | ||
alert_receive_channels_to_update = [] | ||
|
||
alert_receive_channels = AlertReceiveChannel.objects.filter(alert_group_labels_custom__isnull=False) | ||
logging.info(f"Found {alert_receive_channels.count()} integrations with custom alert groups labels") | ||
for alert_receive_channel in alert_receive_channels: | ||
update_labels = False | ||
labels = alert_receive_channel.alert_group_labels_custom[:] | ||
for label in labels: | ||
if label[1] is not None: | ||
labels_associations_to_create.append( | ||
AlertReceiveChannelAssociatedLabel( | ||
key_id=label[0], | ||
value_id=label[1], | ||
organization=alert_receive_channel.organization, | ||
alert_receive_channel=alert_receive_channel | ||
) | ||
) | ||
alert_receive_channel.alert_group_labels_custom.remove(label) | ||
update_labels = True | ||
if update_labels: | ||
alert_receive_channels_to_update.append(alert_receive_channel) | ||
|
||
AlertReceiveChannelAssociatedLabel.objects.bulk_create( | ||
labels_associations_to_create, ignore_conflicts=True, batch_size=5000 | ||
) | ||
logging.info("Bulk created label associations") | ||
AlertReceiveChannel.objects.bulk_update(alert_receive_channels_to_update, fields=["alert_group_labels_custom"], batch_size=5000) | ||
logging.info("Bulk updated integrations") | ||
logging.info("Finished migrating static labels to integration labels") | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('alerts', '0070_remove_resolutionnoteslackmessage__slack_channel_id_db'), | ||
('labels', '0005_labelkeycache_prescribed_labelvaluecache_prescribed'), | ||
] | ||
|
||
operations = [ | ||
# migrate static alert group labels to integration labels | ||
linter.IgnoreMigration(), | ||
migrations.RunPython(migrate_static_labels, migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
engine/apps/labels/0007_remove_alertreceivechannelassociatedlabel_inheritable_db.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# TODO: MOVE IT TO /migrations DIRECTORY IN FUTURE RELEASE | ||
|
||
# Generated by Django 4.2.15 on 2024-11-26 13:37 | ||
|
||
from django.db import migrations | ||
|
||
import common.migrations.remove_field | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("labels", "0006_remove_alertreceivechannelassociatedlabel_inheritable_state"), | ||
] | ||
|
||
operations = [ | ||
common.migrations.remove_field.RemoveFieldDB( | ||
model_name="AlertReceiveChannelAssociatedLabel", | ||
name="inheritable", | ||
remove_state_migration=("labels", "0007_remove_alertreceivechannelassociatedlabel_inheritable_state"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...pps/labels/migrations/0006_remove_alertreceivechannelassociatedlabel_inheritable_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.15 on 2024-11-26 13:37 | ||
|
||
import common.migrations.remove_field | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('labels', '0005_labelkeycache_prescribed_labelvaluecache_prescribed'), | ||
] | ||
|
||
operations = [ | ||
common.migrations.remove_field.RemoveFieldState( | ||
model_name='AlertReceiveChannelAssociatedLabel', | ||
name='inheritable', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters