Skip to content

Commit 5ce1ae0

Browse files
fix: update modulestore migrator to not publish in draft context
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 80dbbfa commit 5ce1ae0

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

cms/djangoapps/modulestore_migrator/tasks.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,23 @@ def _import_structure(
374374
context=migration_context,
375375
source_node=root_node,
376376
)
377-
change_log.save()
377+
# Publishing is not allowed inside bulk_draft_changes_for(), so publish
378+
# everything that was modified now that the context has exited. We use the
379+
# change log to identify which drafts to publish. If the context produced
380+
# no records, it deletes the change log on exit (clearing its PK), in which
381+
# case there's nothing to publish and we return None so callers don't try
382+
# to associate the deleted change log with the migration.
383+
if not change_log.pk:
384+
return None, root_migrated_node
385+
if change_log.records.exists():
386+
drafts_to_publish = content_api.get_all_drafts(migration.target.id).filter(
387+
entity_id__in=change_log.records.values_list("entity_id", flat=True),
388+
)
389+
content_api.publish_from_drafts(
390+
migration.target.id,
391+
draft_qset=drafts_to_publish,
392+
published_by=migration_context.created_by,
393+
)
378394
return change_log, root_migrated_node
379395

380396

@@ -894,11 +910,7 @@ def _migrate_container(
894910
created_by=context.created_by,
895911
).publishable_entity_version
896912

897-
# Publish the container
898-
libraries_api.publish_container_changes(
899-
container.container_key,
900-
context.created_by,
901-
)
913+
# Note: Publishing happens after bulk_draft_changes_for exits, in _import_structure.
902914
context.used_container_slugs.add(container.container_key.container_id)
903915
return container_publishable_entity_version, None
904916

@@ -972,8 +984,7 @@ def _migrate_component(
972984
target_key, new_olx_str=olx, paths_to_media=paths_to_media_ids,
973985
)
974986

975-
# Publish the component
976-
libraries_api.publish_component_changes(target_key, context.created_by)
987+
# Note: Publishing happens after bulk_draft_changes_for exits, in _import_structure.
977988
context.used_component_keys.add(target_key)
978989
return component_version.publishable_entity_version, None
979990

cms/djangoapps/modulestore_migrator/tests/test_tasks.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,9 @@ def test_migrate_component_success(self):
370370
"problem", result.componentversion.component.component_type.name
371371
)
372372

373-
# The component is published
374-
self.assertFalse(result.componentversion.component.versioning.has_unpublished_changes) # noqa: PT009
373+
# The component is left as a draft; publishing is the caller's responsibility
374+
# (handled in _import_structure after bulk_draft_changes_for exits).
375+
self.assertTrue(result.componentversion.component.versioning.has_unpublished_changes) # noqa: PT009
375376

376377
def test_migrate_component_failure(self):
377378
"""
@@ -802,8 +803,9 @@ def test_migrate_container_different_container_types(self):
802803

803804
container_version = result.containerversion
804805
self.assertEqual(container_version.title, f"Test {block_type.title()}") # noqa: PT009
805-
# The container is published
806-
self.assertFalse(content_api.contains_unpublished_changes(container_version.container.pk)) # noqa: PT009 # pylint: disable=line-too-long
806+
# The container is left as a draft; publishing is the caller's
807+
# responsibility (handled in _import_structure after bulk_draft_changes_for exits).
808+
self.assertTrue(content_api.contains_unpublished_changes(container_version.container.pk)) # noqa: PT009 # pylint: disable=line-too-long
807809

808810
def test_migrate_container_same_title(self):
809811
"""

0 commit comments

Comments
 (0)