Repository navigation
fix: send post_save when a learner competency status is raised #932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,8 @@ | |
| from datetime import datetime | ||
| from typing import TypeVar | ||
|
|
||
| from django.db import models | ||
| from django.db import models, transaction | ||
| from django.db.models.signals import post_save | ||
|
|
||
| from .models import ( | ||
| MasteryStatus, | ||
|
|
@@ -32,13 +33,36 @@ def raise_status( | |
| Create or raise the learner status row identified by ``lookup``; never lowers a stored status. | ||
|
|
||
| Returns 1 if a row was created or raised, 0 if the stored status was already at least ``status``. | ||
|
|
||
| ``post_save`` fires for a created row (from ``get_or_create``) and for a raised one (sent here, because the raise | ||
| is a bulk ``UPDATE``), so listeners such as analytics sinks see every change. It does not fire when nothing changed. | ||
| In both cases it is sent before the transaction around the write commits, so a receiver that queues a task should | ||
| do it from ``transaction.on_commit()``. | ||
|
|
||
| Other code that changes a stored status, such as the staff override and recalculation planned in ADR-0004, must | ||
| send ``post_save`` too. | ||
| """ | ||
| row, created = manager.get_or_create(**lookup, defaults={"status_id": status, "created": now, "modified": now}) | ||
| if created: | ||
| log.info("Created %s %s: status %s -> %s", manager.model.__name__, lookup, None, status) | ||
| return 1 | ||
| # A single conditional UPDATE, never read-then-write: a stale read can only compute a value that is too low. | ||
| changed = manager.filter(pk=row.pk, status_id__lt=status).update(status_id=status, modified=now) | ||
| if changed: | ||
| log.info("Raised %s %s: status %s -> %s", manager.model.__name__, lookup, row.status_id, status) | ||
| if row.status_id >= status: | ||
| # Automatic writes only raise a status, so even a stale read means there is nothing to raise. | ||
| return 0 | ||
| # Commit the raise with its post_save, so a failing receiver rolls it back and a retry resends. | ||
| with transaction.atomic(savepoint=False): | ||
|
ormsbee marked this conversation as resolved.
Resolved
|
||
| # A single conditional UPDATE, never read-then-write: a stale read can only compute a value that is too low. | ||
| raised = manager.filter(pk=row.pk, status_id__lt=status) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a test where the row that That filter is what keeps a writer from overwriting a raise that landed after its read (ADR-0004, Decision 4), so I'd like a test holding it in place. A stale read is the only way to get here with nothing to raise now, so the test has to fake one. Maybe something like this in def test_a_raise_that_loses_a_race_changes_nothing(user, tag, saves, monkeypatch) -> None:
"""The conditional UPDATE decides, not the row that was read, so a raise behind a stale read is a no-op."""
_write(user, tag, MasteryStatus.PARTIALLY_ATTEMPTED, FIRST)
stale = StudentCompetencyStatus.objects.get(user=user, tag=tag)
_write(user, tag, MasteryStatus.DEMONSTRATED, FIRST)
monkeypatch.setattr(StudentCompetencyStatus.objects, "get_or_create", lambda **kwargs: (stale, False))
assert _write(user, tag, MasteryStatus.DEMONSTRATED, SECOND) == 0
assert StudentCompetencyStatus.objects.get(user=user, tag=tag).modified == FIRST
assert len(saves) == 2Claude ran this on the branch. It passes as-is and fails with |
||
| changed = raised.update(status_id=status, modified=now) | ||
| if changed: | ||
| log.info("Raised %s %s: status %s -> %s", manager.model.__name__, lookup, row.status_id, status) | ||
| row.status_id, row.modified = status, now | ||
| post_save.send( | ||
| sender=manager.model, | ||
| instance=row, | ||
| created=False, | ||
| update_fields=frozenset({"status_id", "modified"}), | ||
| raw=False, | ||
| using=raised.db, # The alias that update() wrote to; manager.db is the router's read alias. | ||
| ) | ||
| return changed | ||
Uh oh!
There was an error while loading. Please reload this page.