fix: Preserve author_username and retired_username during MongoDB to … #240
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the issue where posts from retired users cause the discussions MFE to crash with
Missing :learnerUsername paramerror after migrating from MongoDB to MySQL.Problem
After migrating forum data from MongoDB to MySQL using the
forum_migrate_course_from_mongodb_to_mysqlmanagement command, the discussions MFE fails to display posts when clicking on content from retired users.Error:
This occurs in
AuthorLabel.jsxwhen trying to generate a route to the learner's posts page.Root Cause
V1 forum (MongoDB) stores usernames as fields: The original Ruby forum (
cs_comments_service) storesauthor_usernameandretired_usernameas separate database fields to preserve historical usernames (see content.rb)V2 migration script ignores these fields: The migration script in
migration_helpers.pywas not readingauthor_usernameorretired_usernamefrom MongoDB documentsMySQL 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.usernameinto_dict()methodsUsernames 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 postedSolution
This PR adds proper username preservation to match the V1 forum behavior:
1. Added Database Fields
author_usernamefield to Content model (stores username at posting time)retired_usernamefield to Content model (stores name to display for retired users)2. Updated Migration Script
create_or_update_thread()to read and preserveauthor_usernamefrom MongoDBcreate_or_update_comment()to read and preserveauthor_usernamefrom MongoDB3. Updated Models
save()hook to setauthor_usernameon content creation for new poststo_dict()methods to use stored username with fallback chain:author_username→retired_username→author.username4. Database Migration
0004_add_author_username_fields.pyto add the new columnsTesting
Migration Path for Existing Deployments
For sites that have already migrated courses to MySQL:
python manage.py migrate forumThe 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_usernamedata; we just needed to preserve it during migration.Checklist