Skip to content

Commit 33f1bc3

Browse files
committed
refactor: always return instance
1 parent 1bf52d2 commit 33f1bc3

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

openedx_authz/engine/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def run_course_authoring_migration(
503503
course_id_list=None,
504504
org_id=None,
505505
delete_after_migration=True,
506-
) -> None:
506+
) -> AuthzCourseAuthoringMigrationRun:
507507
"""
508508
Orchestrate a course authoring role migration with concurrency protection and lifecycle tracking.
509509
@@ -538,8 +538,7 @@ def run_course_authoring_migration(
538538
logger.warning(
539539
"Skipping %s migration for %s:%s — an active run already exists.", migration_type, scope_type, scope_key
540540
)
541-
AuthzCourseAuthoringMigrationRun.create_skipped(migration_type, scope_type, scope_key)
542-
return
541+
return AuthzCourseAuthoringMigrationRun.create_skipped(migration_type, scope_type, scope_key)
543542

544543
logger.info("Started %s migration run [%s] for %s:%s", migration_type, run.id, scope_type, scope_key)
545544

@@ -559,8 +558,7 @@ def run_course_authoring_migration(
559558
logger.exception(
560559
"Unexpected error in migration run [%s] for %s:%s", run.id, scope_type, scope_key, exc_info=exc
561560
)
562-
run.mark_failed(exception=exc)
563-
return
561+
return run.mark_failed(exception=exc)
564562

565563
errors_by_reason: dict = defaultdict(list)
566564
for entry in errors:
@@ -586,6 +584,7 @@ def run_course_authoring_migration(
586584
len(successes),
587585
len(errors),
588586
)
587+
return run
589588
else:
590589
run.mark_completed(metadata_updates=metadata_updates)
591590
logger.info(
@@ -596,3 +595,4 @@ def run_course_authoring_migration(
596595
scope_key,
597596
len(successes),
598597
)
598+
return run

openedx_authz/models/authz_migration.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Meta:
9393
models.Index(fields=["-created_at"]),
9494
]
9595

96-
def save(self, *args, **kwargs) -> None:
96+
def save(self, *args, **kwargs) -> "AuthzCourseAuthoringMigrationRun":
9797
"""Enforce at most one RUNNING record per (scope_type, scope_key).
9898
9999
MySQL does not support partial unique indexes, so this check is done at
@@ -112,6 +112,7 @@ def save(self, *args, **kwargs) -> None:
112112
f"Duplicate RUNNING migration run for scope {self.scope_type}:{self.scope_key}"
113113
)
114114
super().save(*args, **kwargs)
115+
return self
115116

116117
# pylint: disable=too-many-positional-arguments
117118
@classmethod
@@ -149,25 +150,25 @@ def create_skipped(
149150
extra = {**(metadata or {}), "skip_reason": "A concurrent migration run is already active for this scope."}
150151
return cls._create(migration_type, scope_type, scope_key, Status.SKIPPED, extra)
151152

152-
def _finalize(self, status: str, metadata_updates: dict | None = None) -> None:
153+
def _finalize(self, status: str, metadata_updates: dict | None = None) -> "AuthzCourseAuthoringMigrationRun":
153154
"""Finalize the migration run."""
154155
self.status = status
155156
self.completed_at = timezone.now()
156157
if metadata_updates:
157158
self.metadata = {**(self.metadata or {}), **metadata_updates}
158-
self.save(update_fields=["status", "completed_at", "updated_at", "metadata"])
159+
return self.save(update_fields=["status", "completed_at", "updated_at", "metadata"])
159160

160-
def mark_partial_success(self, *, metadata_updates=None) -> None:
161+
def mark_partial_success(self, *, metadata_updates=None) -> "AuthzCourseAuthoringMigrationRun":
161162
"""Mark the migration run as partially successful."""
162-
self._finalize(Status.PARTIAL_SUCCESS, metadata_updates)
163+
return self._finalize(Status.PARTIAL_SUCCESS, metadata_updates)
163164

164-
def mark_completed(self, *, metadata_updates=None) -> None:
165+
def mark_completed(self, *, metadata_updates=None) -> "AuthzCourseAuthoringMigrationRun":
165166
"""Mark the migration run as completed."""
166-
self._finalize(Status.COMPLETED, metadata_updates)
167+
return self._finalize(Status.COMPLETED, metadata_updates)
167168

168-
def mark_failed(self, *, exception=None) -> None:
169+
def mark_failed(self, *, exception=None) -> "AuthzCourseAuthoringMigrationRun":
169170
"""Mark the migration run as failed."""
170-
self._finalize(Status.FAILED, {"error": str(exception)} if exception is not None else None)
171+
return self._finalize(Status.FAILED, {"error": str(exception)} if exception is not None else None)
171172

172173
def __str__(self) -> str:
173174
"""Return a string representation of the migration run."""

0 commit comments

Comments
 (0)