Skip to content

feat: add course authoring migration and rollback scripts#218

Draft
dwong2708 wants to merge 4 commits intoopenedx:mainfrom
WGU-Open-edX:dwong2708/migration-script-for-course-roles
Draft

feat: add course authoring migration and rollback scripts#218
dwong2708 wants to merge 4 commits intoopenedx:mainfrom
WGU-Open-edX:dwong2708/migration-script-for-course-roles

Conversation

@dwong2708
Copy link
Contributor

@dwong2708 dwong2708 commented Feb 18, 2026

Resolves: #179

Course Authoring Roles Migration to Authz

Overview

This PR introduces migration and rollback support for course authoring roles between the legacy CourseAccessRole model and the new Authz (Casbin-based) authorization system.

It provides:

  • Forward migration (legacy → Authz)
  • Rollback migration (Authz → legacy)
  • Optional cleanup of source permissions
  • Management commands for operational control
  • Automated data migration (The Django migration will be added once it is released)
  • Full test coverage

What’s Included

1️⃣ Management Commands

authz_migrate_course_authoring

Migrates legacy CourseAccessRole entries to the new Authz system.

./manage.py authz_migrate_course_authoring [--delete]

  • Maps legacy roles to their equivalent Authz roles
  • Logs unmapped/invalid roles
  • Optional --delete flag removes successfully migrated legacy entries

authz_rollback_course_authoring

Rolls back Authz course authoring roles into legacy CourseAccessRole.

./manage.py cms authz_rollback_course_authoring [--delete]

  • Recreates legacy entries from Authz assignments
  • Optional --delete flag removes Authz role assignments after rollback

2️⃣ Service Layer

Added two migration service functions:

  • migrate_legacy_course_roles_to_authz
  • migrate_authz_to_legacy_course_roles

These encapsulate the migration logic and are reusable by:

  • Management commands
  • Data migrations
  • Tests

3️⃣ Data Migration

A Django data migration has been added to automatically trigger the forward migration.

This ensures legacy course authoring roles are migrated during deployment without requiring manual intervention.

4️⃣ Test Coverage

Added comprehensive unit tests that verify:

  • Correct role mapping (legacy ↔ Authz)
  • Error handling for invalid roles
  • Behavior with and without --delete
  • Management command execution and flag handling
  • Idempotency (no duplicate legacy roles on rollback)

🧠 Design Considerations

  • Migration and rollback are symmetrical
  • Deletion is always explicit and controlled via --delete
  • Invalid roles are logged and skipped safely
  • get_or_create ensures rollback does not create duplicates
  • Commands wrap logic in transactions for safety

⚠️ Operational Notes

  • Recommended to run migration without --delete first for validation.
  • Rollback is fully supported in case of unexpected issues.
  • Data migration runs automatically during deployment.

✅ Result

This PR provides a safe, reversible, and production-ready migration path from legacy course authoring roles to the new Authz system.

Merge checklist:
Check off if complete or not applicable:

  • Version bumped
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

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

Thanks for the pull request, @dwong2708!

This repository is currently maintained by @openedx/committers-openedx-authz.

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.

🔘 Update the status of your PR

Your PR is currently marked as a draft. After completing the steps above, update its status by clicking "Ready for Review", or removing "WIP" from the title, as appropriate.


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 Feb 18, 2026
@mphilbrick211 mphilbrick211 added the mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). label Feb 18, 2026
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Waiting on Author in Contributions Feb 18, 2026
Copy link
Contributor

@rodmgwgu rodmgwgu left a comment

Choose a reason for hiding this comment

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

We should make sure to only apply the migrations on courses that are covered by the feature flag.

If we cannot access the feature flag from here (as it is defined in edx-platform), we should add a parameter to the migration commands and functions to specify a list of courses to work on, so edx-platform can specify which courses to migrate.

param CourseAccessRole: The CourseAccessRole model to use.
"""

legacy_permissions = CourseAccessRole.objects.select_related("user").all()
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to make sure to only do the migration on courses that have the flag enabled.

for assignment in role_assignments:
scope = assignment.scope.external_key

course_overview = assignment.scope.get_object()
Copy link
Contributor

Choose a reason for hiding this comment

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

We should only rollback courses that don't have the flag enabled

- Add `authz_migrate_course_authoring` command to migrate legacy
  CourseAccessRole data to the new Authz (Casbin-based) system
- Add `authz_rollback_course_authoring` command to rollback
  Authz roles back to legacy CourseAccessRole
- Support optional `--delete` flag for controlled cleanup of
  source permissions after successful migration
- Add `migrate_legacy_course_roles_to_authz` and
  `migrate_authz_to_legacy_course_roles` service functions
- Add unit tests to verify migration and command behavior
- Add Django data migration to automatically trigger the migration
@dwong2708 dwong2708 force-pushed the dwong2708/migration-script-for-course-roles branch from b605933 to 0b8547b Compare February 19, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Waiting on Author

Development

Successfully merging this pull request may close these issues.

Task - RBAC AuthZ - Implement migration script for turning legacy role assignations to openedx-authz assignations.

4 participants

Comments