Skip to content

feat: add data migration to sync DiscussionsConfiguration with modulestore tab.is_hidden#38294

Open
Anas12091101 wants to merge 2 commits intoopenedx:masterfrom
mitodl:anas/data-migration-to-sync-discussion
Open

feat: add data migration to sync DiscussionsConfiguration with modulestore tab.is_hidden#38294
Anas12091101 wants to merge 2 commits intoopenedx:masterfrom
mitodl:anas/data-migration-to-sync-discussion

Conversation

@Anas12091101
Copy link
Copy Markdown
Contributor

@Anas12091101 Anas12091101 commented Apr 7, 2026

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 both DiscussionsConfiguration.enabled and CourseAppStatus.enabled to match (enabled = NOT is_hidden). Only out-of-sync rows are updated. The migration uses CourseOverviewTab instead of the modulestore directly to avoid performance issues at scale.

Useful information to include:

  • Which edX user roles will this change impact? Course Author"
  • Include screenshots for changes to the UI (ideally, both "before" and "after" screenshots, if applicable).
  • Provide links to the description of corresponding configuration changes. Remember to correctly annotate these
    changes.

Supporting information

#38084 (comment)

Testing instructions

  1. Rollback 0019 if already applied:
python manage.py migrate discussions 0018
  1. Create unsynced data in the shell:
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview, CourseOverviewTab
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration
from openedx.core.djangoapps.course_apps.models import CourseAppStatus
from opaque_keys.edx.keys import CourseKey

course_key = CourseKey.from_string("course-v1:U0X12+CS11013+UAI_22")

overview, _ = CourseOverview.objects.get_or_create(id=course_key, defaults={"version": 1})

tab, _ = CourseOverviewTab.objects.update_or_create(
    course_overview=overview,
    tab_id="discussion",
    defaults={"is_hidden": True},
)

config, _ = DiscussionsConfiguration.objects.update_or_create(
    context_key=course_key,
    defaults={"enabled": True},
)

app_status, _ = CourseAppStatus.objects.update_or_create(
    course_key=course_key,
    app_id="discussion",
    defaults={"enabled": True},
)

# Verify the desync
print(f"Tab is_hidden: {tab.is_hidden}")            # True
print(f"Config enabled: {config.enabled}")           # True  (should be False)
print(f"AppStatus enabled: {app_status.enabled}")    # True  (should be False)
  1. Visit the Pages and Resources page in the authoring MFE and verify that the discussion configuration is unsynced

  2. Run the migration:

python manage.py migrate discussions 0019
  1. Verify the fix:
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration
from opaque_keys.edx.keys import CourseKey

6. Repeat step 3 and verify that the configuration is now synced

config = DiscussionsConfiguration.objects.get(context_key=CourseKey.from_string("course-v1:TestOrg+Test101+2026"))
print(f"Config enabled: {config.enabled}")  # Should now be False

Deadline

"None"

Other information

Include anything else that will help reviewers and consumers understand the change.

  • Does this change depend on other changes elsewhere?
  • Any special concerns or limitations? For example: deprecations, migrations, security, or accessibility.
  • If your database migration can't be rolled back easily.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Apr 7, 2026
@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @Anas12091101!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform.

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 approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To 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:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where 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:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

Comment on lines +33 to +38
CourseAppStatus.objects.filter(
course_key=course_key,
app_id="discussion",
).exclude(
enabled=expected_enabled,
).update(enabled=expected_enabled)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?)

@Anas12091101 Anas12091101 force-pushed the anas/data-migration-to-sync-discussion branch from ff22569 to f07de7f Compare April 7, 2026 12:24


def sync_enabled_from_course_overview_tab(apps, schema_editor):
CourseOverviewTab = apps.get_model("course_overviews", "CourseOverviewTab")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Glad that the CourseOverviewTab model works! Thanks for the suggestion, Dave.

Comment on lines +33 to +38
CourseAppStatus.objects.filter(
course_key=course_key,
app_id="discussion",
).exclude(
enabled=expected_enabled,
).update(enabled=expected_enabled)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?)

Comment on lines +112 to +120
# 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,
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: In Eng Review

Development

Successfully merging this pull request may close these issues.

4 participants