feat: add data migration to sync DiscussionsConfiguration with modulestore tab.is_hidden#38294
feat: add data migration to sync DiscussionsConfiguration with modulestore tab.is_hidden#38294Anas12091101 wants to merge 2 commits intoopenedx:masterfrom
Conversation
|
Thanks for the pull request, @Anas12091101! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
| CourseAppStatus.objects.filter( | ||
| course_key=course_key, | ||
| app_id="discussion", | ||
| ).exclude( | ||
| enabled=expected_enabled, | ||
| ).update(enabled=expected_enabled) |
There was a problem hiding this comment.
The CourseAppStatus model is what the Pages & Resources MFE actually reads to determine whether the Discussion app shows as "Enabled" or "Disabled". When the API endpoint /api/course_apps/v1/apps/{course_id} is called, it first checks CourseAppStatus.enabled for the "discussion" app and only falls back to DiscussionsConfiguration.is_enabled()( if no row exists. So updating DiscussionsConfiguration.enabled alone is not enough; if a CourseAppStatus row already exists with enabled=True, the MFE will still show the discussion tab as enabled regardless of what DiscussionsConfiguration says. This is why we update both models in the migration to ensure the UI reflects the actual tab visibility from the course structure.
There was a problem hiding this comment.
Wow, there are so many layers 😵
@Anas12091101 When a course is imported, does CourseAppStatus.enabled get updated from the modulestore tab's is_hidden state, like you did for DiscussionConfiguration.enabled? (If not, then am I correct to understand that this migration would only be a one-time fix for existing CourseAppStatuses, and the issue would continue for future imports?)
…store tab.is_hidden
ff22569 to
f07de7f
Compare
|
|
||
|
|
||
| def sync_enabled_from_course_overview_tab(apps, schema_editor): | ||
| CourseOverviewTab = apps.get_model("course_overviews", "CourseOverviewTab") |
There was a problem hiding this comment.
Glad that the CourseOverviewTab model works! Thanks for the suggestion, Dave.
| CourseAppStatus.objects.filter( | ||
| course_key=course_key, | ||
| app_id="discussion", | ||
| ).exclude( | ||
| enabled=expected_enabled, | ||
| ).update(enabled=expected_enabled) |
There was a problem hiding this comment.
Wow, there are so many layers 😵
@Anas12091101 When a course is imported, does CourseAppStatus.enabled get updated from the modulestore tab's is_hidden state, like you did for DiscussionConfiguration.enabled? (If not, then am I correct to understand that this migration would only be a one-time fix for existing CourseAppStatuses, and the issue would continue for future imports?)
| # Also update CourseAppStatus to keep the Pages & Resources UI in sync. | ||
| # The update_course_apps_status task may run before this handler due to | ||
| # the COURSE_PUBLISH_TASK_DELAY countdown, caching a stale enabled value. | ||
| CourseAppStatus.update_status_for_course_app( | ||
| course_key=course_key, | ||
| app_id="discussion", | ||
| enabled=configuration.enabled, | ||
| ) | ||
|
|
There was a problem hiding this comment.
@kdmccormick, you’re absolutely right. Here’s what’s actually happening:
When a course is published (including after an import), two independent Celery tasks are triggered by the course_published signal. The first is update_discussions_settings_from_course_task, which reads the course tabs from the modulestore, derives enabled = not tab.is_hidden for the discussion tab, and passes it through CourseDiscussionConfigurationData. The COURSE_DISCUSSIONS_CHANGED event fires, and the handler updates DiscussionsConfiguration.enabled in the database.
The second is update_course_apps_status, which iterates over all available course apps and calls each app's is_enabled() method. For the discussion app, DiscussionCourseApp.is_enabled() calls DiscussionsConfiguration.is_enabled(course_key), reading directly from the DiscussionsConfiguration model. It then writes the result into CourseAppStatus via update_status_for_course_app().
The Pages & Resources MFE calls GET /api/course_apps/v1/apps/{course_id}. The serializer first checks CourseAppStatus (bulk-loaded via get_all_app_status_data_for_course). If no row exists, it falls back to is_course_app_enabled(), which tries CourseAppStatus.objects.get(course_key, app_id) and returns its enabled value if found, otherwise calls DiscussionCourseApp.is_enabled() which reads from DiscussionsConfiguration.is_enabled().
Locally, this was working because CELERY_ALWAYS_EAGER=True. However, in production with Celery workers, update_discussions_settings_from_course_task is triggered after update_course_apps_status due to the COURSE_PUBLISH_TASK_DELAY. As a result, update_course_apps_status ends up using stale values from DiscussionsConfiguration, so the data doesn’t get synced as expected.
I have added the code here. We have 2 scenarios:
New course: No CourseAppStatus row exists yet. The handler creates one with the correct enabled value.
Old course: A CourseAppStatus row already exists (created by update_course_apps_status on a previous publish, or by initialize_course_app_status on first MFE page load). The handler updates it.
Description
We introduced a fix in #38084 to synchronize DiscussionsConfiguration.enabled (DB) with tab.is_hidden (modulestore) during course imports. While this resolves the issue for newly imported courses, existing courses that are currently out of sync still need to be updated so that course authors see the correct values.
This PR adds a data migration that reads each course's discussion tab from
CourseOverviewTab(a DB-level cache of modulestore tabs) and updates bothDiscussionsConfiguration.enabledandCourseAppStatus.enabledto match (enabled = NOT is_hidden). Only out-of-sync rows are updated. The migration usesCourseOverviewTabinstead of the modulestore directly to avoid performance issues at scale.Useful information to include:
changes.
Supporting information
#38084 (comment)
Testing instructions
Visit the
Pages and Resourcespage in the authoring MFE and verify that the discussion configuration is unsyncedRun the migration:
Deadline
"None"
Other information
Include anything else that will help reviewers and consumers understand the change.