@@ -75,15 +75,15 @@ def backfill_openedx_catalog(apps, schema_editor):
7575 language = settings .LANGUAGE_CODE
7676
7777 # Ensure that the CatalogCourse exists.
78- cc , created = CatalogCourse .objects .get_or_create (
78+ cc , cc_created = CatalogCourse .objects .get_or_create (
7979 org_id = org_data ["id" ],
8080 course_code = course_code ,
8181 defaults = {
8282 "display_name" : display_name ,
8383 "language" : language ,
8484 },
8585 )
86- if created :
86+ if cc_created :
8787 created_catalog_course_ids .add (cc .pk )
8888 elif cc .pk in created_catalog_course_ids :
8989 # This CatalogCourse was previously created during this same migration
@@ -106,13 +106,23 @@ def backfill_openedx_catalog(apps, schema_editor):
106106 )
107107
108108 # Create the CourseRun
109- CourseRun .objects .get_or_create (
109+ new_run , run_created = CourseRun .objects .get_or_create (
110110 catalog_course = cc ,
111111 run = run_code ,
112112 course_id = course_run .course_id ,
113113 defaults = {"display_name" : display_name },
114114 )
115115
116+ # Correct the "created" timestamp. Since it has auto_now_add=True, we can't set its value except using update()
117+ # The CourseOverview should have the "created" date unless it's missing or the course was created before
118+ # the CourseOverview model existed. In any case, it should be good enough. Otherwise use the default (now).
119+ if course_overview :
120+ if course_overview .created < cc .created and cc .pk in created_catalog_course_ids :
121+ # Use the 'created' date from the oldest course run that we process.
122+ CatalogCourse .objects .filter (pk = cc .pk ).update (created = course_overview .created )
123+ if run_created :
124+ CourseRun .objects .filter (pk = new_run .pk ).update (created = course_overview .created )
125+
116126
117127class Migration (migrations .Migration ):
118128 dependencies = [
0 commit comments