Skip to content

Commit

Permalink
perf: reduce db calls for ComponentVersion after lookup by UUID
Browse files Browse the repository at this point in the history
When you fetch a single ComponentVersion, you are often going to want to
pull data related to its Component, and possibly the LearningPackage it
belongs to. This commit adds a select_related call to eliminate the
extra roundtrips for this information.
  • Loading branch information
ormsbee committed Sep 27, 2024
1 parent da9adf8 commit e0baf65
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions openedx_learning/apps/authoring/components/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,15 @@ def get_component_by_uuid(uuid: UUID) -> Component:


def get_component_version_by_uuid(uuid: UUID) -> ComponentVersion:
return ComponentVersion.objects.get(publishable_entity_version__uuid=uuid)
return (
ComponentVersion
.objects
.select_related(
"component",
"component__learning_package",
)
.get(publishable_entity_version__uuid=uuid)
)


def component_exists_by_key(
Expand Down Expand Up @@ -510,7 +518,12 @@ def _error_header(error: AssetError) -> dict[str, str]:

# Check: Does the ComponentVersion exist?
try:
component_version = get_component_version_by_uuid(component_version_uuid)
component_version = (
ComponentVersion
.objects
.select_related("component", "component__learning_package")
.get(publishable_entity_version__uuid=component_version_uuid)
)
except ComponentVersion.DoesNotExist:
# No need to add headers here, because no ComponentVersion was found.
logger.error(f"Asset Not Found: No ComponentVersion with UUID {component_version_uuid}")
Expand Down

0 comments on commit e0baf65

Please sign in to comment.