Skip to content
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

Fix Grafana teams sync #1652

Merged
merged 3 commits into from
Mar 28, 2023
Merged

Fix Grafana teams sync #1652

merged 3 commits into from
Mar 28, 2023

Conversation

vstpme
Copy link
Member

@vstpme vstpme commented Mar 28, 2023

What this PR does

Sometimes plugin sync fails with the following exception:

Cannot delete or update a parent row: a foreign key constraint fails (`schedules_oncallschedule`, CONSTRAINT `alerts_oncallschedul_team_id_4e633f4b_fk_user_mana` FOREIGN KEY (`team_id`) REFERENCES `user_management_team` (`id`))'

How to reproduce:

  1. Create a new Grafana team
  2. Create two schedules with different types (e.g. ICal and Web) and assign both schedules to the new team
  3. Delete the team in Grafana
  4. Trigger plugin sync, the sync will fail with the exception above

This happens because the OnCallSchedule Django model is a polymorphic model and there's a known bug in django-polymorphic with deleting related objects when using on_delete=SET_NULL and on_delete=CASCADE. This PR adds non-polymorphic versions of SET_NULL and CASCADE to use in schedule FKs as per this comment.

This also applies to two other schedule FKs: organization and user_group, which are not working properly as well.

Checklist

  • Unit, integration, and e2e (if applicable) tests updated
  • Documentation added (or pr:no public docs PR label added if not required)
  • CHANGELOG.md updated (or pr:no changelog PR label added if not required)

@vstpme vstpme added the pr:no public docs Added to a PR that does not require public documentation updates label Mar 28, 2023
@vstpme vstpme changed the title Fix teams sync Fix Grafana teams sync Mar 28, 2023
)

team = models.ForeignKey(
"user_management.Team",
on_delete=models.SET_NULL,
on_delete=NON_POLYMORPHIC_SET_NULL,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change of the PR

@@ -15,3 +16,23 @@ def get_random_readonly_database_key_if_present_otherwise_default() -> str:
if hasattr(settings, "READONLY_DATABASES") and len(settings.READONLY_DATABASES) > 0:
using_db = random.choice(list(settings.READONLY_DATABASES.keys()))
return using_db


def NON_POLYMORPHIC_SET_NULL(collector, field, sub_objs, using):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change of the PR

@vstpme vstpme marked this pull request as ready for review March 28, 2023 18:13
@vstpme vstpme requested a review from a team March 28, 2023 18:13
Copy link
Member

@Ferril Ferril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@vstpme vstpme added this pull request to the merge queue Mar 28, 2023
Merged via the queue into dev with commit 3c00345 Mar 28, 2023
@vstpme vstpme deleted the vadimkerr/fix-polymorphic-on-delete branch March 28, 2023 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:no public docs Added to a PR that does not require public documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants