From 31deebcba13e0dc8e62d3a1384d7ea13f551fd68 Mon Sep 17 00:00:00 2001 From: Julia Eskew Date: Thu, 24 Jun 2021 18:50:14 -0400 Subject: [PATCH] fix: Use a "through" model for the ManyToManyField fields in CourseSection and CourseSectionSequence to ensure that cascading deletes will occur to delete the relation upon deletion of a foreign-keyed object. This commit is phase 2 of 3 in order to ensure a smooth deploy. The phases: 1) Add separate through models for user partition groups, add fields to point to the separate models, and start writing to those fields as well. (COMPLETE) 2) After all models have been re-generated, switch code over to use separate through model fields backed by the separate through models *and* stop writing to the original ManyToManyField field. (This PR!) 3) After phase 2 is deployed smoothly, remove the original ManyToManyField fields and rename the new fields to have the same name as the old fields. This PR is designed to be able to easily be rolled-back if anything goes wrong. --- .../learning_sequences/api/outlines.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/openedx/core/djangoapps/content/learning_sequences/api/outlines.py b/openedx/core/djangoapps/content/learning_sequences/api/outlines.py index dd8358c621..fc7c3b3460 100644 --- a/openedx/core/djangoapps/content/learning_sequences/api/outlines.py +++ b/openedx/core/djangoapps/content/learning_sequences/api/outlines.py @@ -139,11 +139,11 @@ def get_course_outline(course_key: CourseKey) -> CourseOutlineData: # represented (so query CourseSection explicitly instead of relying only on # select_related from CourseSectionSequence). section_models = CourseSection.objects \ - .prefetch_related('user_partition_groups') \ + .prefetch_related('new_user_partition_groups') \ .filter(course_context=course_context) \ .order_by('ordering') section_sequence_models = CourseSectionSequence.objects \ - .prefetch_related('user_partition_groups') \ + .prefetch_related('new_user_partition_groups') \ .filter(course_context=course_context) \ .order_by('ordering') \ .select_related('sequence', 'exam') @@ -173,7 +173,7 @@ def get_course_outline(course_key: CourseKey) -> CourseOutlineData: ), exam=exam_data, user_partition_groups=_get_user_partition_groups_from_qset( - sec_seq_model.user_partition_groups.all() + sec_seq_model.new_user_partition_groups.all() ), ) sec_ids_to_sequence_list[sec_seq_model.section_id].append(sequence_data) @@ -188,7 +188,7 @@ def get_course_outline(course_key: CourseKey) -> CourseOutlineData: visible_to_staff_only=section_model.visible_to_staff_only, ), user_partition_groups=_get_user_partition_groups_from_qset( - section_model.user_partition_groups.all() + section_model.new_user_partition_groups.all() ), ) for section_model in section_models @@ -552,17 +552,6 @@ def _update_user_partition_groups(upg_data: Dict[int, FrozenSet[int]], """ Replace UserPartitionGroups associated with this content with `upg_data`. """ - model_obj.user_partition_groups.all().delete() - if upg_data: - for partition_id, group_ids in upg_data.items(): - for group_id in group_ids: - upg, _ = UserPartitionGroup.objects.get_or_create( - partition_id=partition_id, group_id=group_id - ) - model_obj.user_partition_groups.add(upg) - - # Temporarily fill-in the new and old user_partition_group fields. - # Switchover to the new field will occur after all models have been repopulated. model_obj.new_user_partition_groups.all().delete() if upg_data: for partition_id, group_ids in upg_data.items():