Skip to content

Conversation

@blarghmatey
Copy link
Contributor

Summary

This PR fixes the issue where posts from retired users cause the discussions MFE to crash with Missing :learnerUsername param error after migrating from MongoDB to MySQL.

Problem

After migrating forum data from MongoDB to MySQL using the forum_migrate_course_from_mongodb_to_mysql management command, the discussions MFE fails to display posts when clicking on content from retired users.

Error:

Error: Missing ":learnerUsername" param

This occurs in AuthorLabel.jsx when trying to generate a route to the learner's posts page.

Root Cause

  1. V1 forum (MongoDB) stores usernames as fields: The original Ruby forum (cs_comments_service) stores author_username and retired_username as separate database fields to preserve historical usernames (see content.rb)

  2. V2 migration script ignores these fields: The migration script in migration_helpers.py was not reading author_username or retired_username from MongoDB documents

  3. MySQL models derive username at runtime: The V2 MySQL models only stored a foreign key to the User table and derived the username from self.author.username in to_dict() methods

  4. Usernames change when users retire: When users are retired, their username is changed to retired__user_<hash>, losing the historical username that was used when content was originally posted

Solution

This PR adds proper username preservation to match the V1 forum behavior:

1. Added Database Fields

  • Added author_username field to Content model (stores username at posting time)
  • Added retired_username field to Content model (stores name to display for retired users)
  • These fields are inherited by CommentThread and Comment models

2. Updated Migration Script

  • Modified create_or_update_thread() to read and preserve author_username from MongoDB
  • Modified create_or_update_comment() to read and preserve author_username from MongoDB
  • Falls back to current username if MongoDB didn't have these fields

3. Updated Models

  • Added save() hook to set author_username on content creation for new posts
  • Updated to_dict() methods to use stored username with fallback chain:
    • author_usernameretired_usernameauthor.username

4. Database Migration

  • Created Django migration 0004_add_author_username_fields.py to add the new columns

Testing

  • ✅ Posts from active users work as before
  • ✅ Posts from retired users now display with correct historical username
  • ✅ Frontend properly handles retired user indicators
  • ✅ Re-running migration preserves original usernames from MongoDB
  • ✅ New posts created in V2 automatically store username

Migration Path for Existing Deployments

For sites that have already migrated courses to MySQL:

  1. Apply this PR's code changes
  2. Run Django migration: python manage.py migrate forum
  3. Re-run content migration for affected courses:
    python manage.py forum_migrate_course_from_mongodb_to_mysql <course_id>

The migration script is idempotent and will update existing records with the preserved usernames.

Related Issues

Additional Context

The V1 forum (Ruby/MongoDB) has always stored usernames as separate fields to avoid issues with username changes. This PR brings that behavior to the V2 forum (Python/MySQL). The investigation revealed that the MongoDB documents already contain the necessary author_username data; we just needed to preserve it during migration.

Checklist

  • Added database fields to Content model
  • Updated migration script to preserve MongoDB fields
  • Updated to_dict() methods with fallback logic
  • Created Django migration file
  • Documented migration path for existing deployments
  • Commit message follows conventional commits format

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Oct 24, 2025
@openedx-webhooks
Copy link

Thanks for the pull request, @blarghmatey!

This repository is currently maintained by @openedx/wg-maintainers-forums.

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.


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.

@github-project-automation github-project-automation bot moved this to Needs Triage in Contributions Oct 24, 2025
@blarghmatey blarghmatey force-pushed the fix/preserve-author-username-in-migration branch 2 times, most recently from 54abefe to d5b5c2c Compare October 24, 2025 15:09
…MySQL migration

This commit fixes the issue where posts from retired users cause the
discussions MFE to crash with 'Missing :learnerUsername param' error
after migrating from MongoDB to MySQL.

Root Cause:
- V1 forum (MongoDB) stores author_username and retired_username as
  separate fields to preserve historical usernames
- V2 migration script was not reading these fields from MongoDB
- MySQL models only derived username from User table at runtime
- When users are retired, their username changes, losing historical data

Changes Made:
1. Added author_username and retired_username fields to Content model
2. Added save() hook to set author_username on content creation
3. Updated to_dict() methods to use stored username with fallbacks
4. Updated migration script to preserve MongoDB username fields
5. Created Django migration to add database columns

Testing:
- Posts from active users work as before
- Posts from retired users now display with correct historical username
- Frontend properly handles retired user indicators
- Re-running migration preserves original usernames

Fixes: openedx/frontend-app-discussions#817
Related: openedx#236
@blarghmatey blarghmatey force-pushed the fix/preserve-author-username-in-migration branch from d5b5c2c to 3443a2c Compare October 24, 2025 15:21
@taimoor-ahmed-1 taimoor-ahmed-1 merged commit 084c58f into openedx:master Oct 27, 2025
14 checks passed
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in Contributions Oct 27, 2025
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: Done

Development

Successfully merging this pull request may close these issues.

Forums V2 with retired user results in a display error when viewing posts

3 participants