|
| 1 | +""" |
| 2 | +Data migration to populate the new CourseRun and CatalogCourse models. |
| 3 | +""" |
| 4 | + |
| 5 | +# Generated by Django 5.2.11 on 2026-02-13 21:47 |
| 6 | +import logging |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from django.db import migrations |
| 10 | +from organizations.api import ensure_organization, exceptions as org_exceptions |
| 11 | + |
| 12 | +log = logging.getLogger(__name__) |
| 13 | + |
| 14 | +# https://github.com/openedx/openedx-platform/issues/38036 |
| 15 | +NORMALIZE_LANGUAGE_CODES = { |
| 16 | + "zh-hans": "zh-cn", |
| 17 | + "zh-hant": "zh-hk", |
| 18 | + "ca@valencia": "ca-es-valencia", |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def backfill_openedx_catalog(apps, schema_editor) -> None: |
| 23 | + """ |
| 24 | + Populate the new CourseRun and CatalogCourse models. |
| 25 | + """ |
| 26 | + CourseOverview = apps.get_model("course_overviews", "CourseOverview") |
| 27 | + CatalogCourse = apps.get_model("openedx_catalog", "CatalogCourse") |
| 28 | + CourseRun = apps.get_model("openedx_catalog", "CourseRun") |
| 29 | + |
| 30 | + created_catalog_course_ids: set[int] = set() |
| 31 | + all_course_runs = CourseOverview.objects.order_by("-created") |
| 32 | + for course_overview in all_course_runs: |
| 33 | + course_key = course_overview.id |
| 34 | + org_code: str = course_key.org |
| 35 | + course_code: str = course_key.course |
| 36 | + run_code: str = course_key.run |
| 37 | + |
| 38 | + # Ensure that the Organization exists. |
| 39 | + try: |
| 40 | + org_data = ensure_organization(org_code) |
| 41 | + except org_exceptions.InvalidOrganizationException as exc: |
| 42 | + # Note: IFF the org exists among the modulestore courses but not in the Organizations database table, |
| 43 | + # and if auto-create is disabled (it's enabled by default), this will raise InvalidOrganizationException. It |
| 44 | + # would be up to the operator to decide how they want to resolve that. |
| 45 | + raise ValueError( |
| 46 | + f'The organization short code "{org_code}" exists in modulestore ({course_key}) but ' |
| 47 | + "not the Organizations table, and auto-creating organizations is disabled. You can resolve this by " |
| 48 | + "creating the Organization manually (e.g. from the Django admin) or turning on auto-creation. " |
| 49 | + "You can set active=False to prevent this Organization from being used other than for historical data. " |
| 50 | + ) from exc |
| 51 | + if org_data["short_name"] != org_code: |
| 52 | + # On most installations, the 'short_name' database column is case insensitive (unfortunately) |
| 53 | + log.warning( |
| 54 | + 'The course with ID "%s" does not match its Organization.short_name "%s"', |
| 55 | + course_key, |
| 56 | + org_data["short_name"], |
| 57 | + ) |
| 58 | + |
| 59 | + # Fetch the CourseOverview if it exists |
| 60 | + try: |
| 61 | + course_overview = CourseOverview.objects.get(id=course_key) |
| 62 | + except CourseOverview.DoesNotExist: |
| 63 | + course_overview = None # Course exists in modulestore but details aren't cached into CourseOverview yet |
| 64 | + title: str = (course_overview.display_name if course_overview else None) or course_code |
| 65 | + |
| 66 | + # Determine the course language. |
| 67 | + # Note that in Studio, the options for course language generally came from the ALL_LANGUAGES setting, which is |
| 68 | + # mostly two-letter language codes with no locale, except it uses "zh_HANS" for Mandarin and "zh_HANT" for |
| 69 | + # Cantonese. We normalize those to "zh-cn" and "zh-hk" for consistency with our platform UI languages / |
| 70 | + # Transifex, but you can still access the "old" version using the CatalogCourse.language_short |
| 71 | + # getter/setter for backwards compatbility. See https://github.com/openedx/openedx-platform/issues/38036 |
| 72 | + language = settings.LANGUAGE_CODE |
| 73 | + if course_overview and course_overview.language: |
| 74 | + language = course_overview.language.lower() |
| 75 | + language = language.replace("_", "-") # Ensure we use hyphens for consistency (`en-us` not `en_us`) |
| 76 | + # Normalize this language code. The previous/non-normalized code will still be available via the |
| 77 | + # "language_short" property for backwards compatibility. |
| 78 | + language = NORMALIZE_LANGUAGE_CODES.get(language, language) |
| 79 | + if len(language) > 2 and language[2] != "-": |
| 80 | + # This seems like an invalid value; revert to the default: |
| 81 | + log.warning( |
| 82 | + 'The course with ID "%s" has invalid language "%s" - using default language "%s" instead.', |
| 83 | + course_key, |
| 84 | + language, |
| 85 | + settings.LANGUAGE_CODE, |
| 86 | + ) |
| 87 | + language = settings.LANGUAGE_CODE |
| 88 | + |
| 89 | + # Ensure that the CatalogCourse exists. |
| 90 | + cc, cc_created = CatalogCourse.objects.get_or_create( |
| 91 | + org_id=org_data["id"], |
| 92 | + course_code=course_code, |
| 93 | + defaults={ |
| 94 | + # The default title for the catalog course will be the same name as the newest run, since we iterate |
| 95 | + # over "all_course_runs" in "-created" order. |
| 96 | + "title": title, |
| 97 | + "language": language, |
| 98 | + }, |
| 99 | + ) |
| 100 | + if cc_created: |
| 101 | + created_catalog_course_ids.add(cc.pk) |
| 102 | + |
| 103 | + if cc.course_code != course_code: |
| 104 | + raise ValueError( |
| 105 | + f"The course {course_key} exists in modulestore with a different capitalization of its " |
| 106 | + f'course code compared to other instances of the same run ("{course_code}" vs "{cc.course_code}"). ' |
| 107 | + "This really should not happen. To fix it, delete the inconsistent course runs (!). " |
| 108 | + ) |
| 109 | + |
| 110 | + # Create the CourseRun |
| 111 | + new_run, run_created = CourseRun.objects.get_or_create( |
| 112 | + catalog_course=cc, |
| 113 | + run_code=run_code, |
| 114 | + course_key=course_key, |
| 115 | + defaults={"title": title}, |
| 116 | + ) |
| 117 | + |
| 118 | + # Correct the "created" timestamp. Since it has auto_now_add=True, we can't set its value except using update() |
| 119 | + # The CourseOverview should have the "created" date unless it's missing or the course was created before |
| 120 | + # the CourseOverview model existed. In any case, it should be good enough. Otherwise use the default (now). |
| 121 | + if course_overview: |
| 122 | + if course_overview.created < cc.created and cc.pk in created_catalog_course_ids: |
| 123 | + # Use the 'created' date from the oldest course run that we process. |
| 124 | + CatalogCourse.objects.filter(pk=cc.pk).update(created=course_overview.created) |
| 125 | + if run_created: |
| 126 | + CourseRun.objects.filter(pk=new_run.pk).update(created=course_overview.created) |
| 127 | + |
| 128 | + |
| 129 | +class Migration(migrations.Migration): |
| 130 | + dependencies = [ |
| 131 | + ("openedx_catalog", "0001_initial"), |
| 132 | + ("course_overviews", "0029_alter_historicalcourseoverview_options"), |
| 133 | + ("split_modulestore_django", "0003_alter_historicalsplitmodulestorecourseindex_options"), |
| 134 | + ] |
| 135 | + |
| 136 | + operations = [ |
| 137 | + migrations.RunPython(backfill_openedx_catalog, reverse_code=migrations.RunPython.noop), |
| 138 | + ] |
0 commit comments