Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(learning): read related projections in one snapshot

dajiaohuang80881c250069
diff --git a/deeptutor/learning/storage.py b/deeptutor/learning/storage.py
index f7f000f79..807903272 100644
--- a/deeptutor/learning/storage.py
+++ b/deeptutor/learning/storage.py
@@ -956,6 +956,7 @@ class LearningStore:
self._import_legacy_if_needed(path_id)
bounded_limit = max(1, min(int(limit), 1000))
with self._connect() as conn:
+ conn.execute("BEGIN")
row = conn.execute(
"SELECT * FROM mastery_paths WHERE path_id = ?", (path_id,)
).fetchone()
@@ -1547,6 +1548,7 @@ class LearningStore:
"""Read the atlas in a constant number of bounded SQLite queries."""
with self._connect() as conn:
+ conn.execute("BEGIN")
progress_rows = conn.execute(
"""
SELECT p.* FROM mastery_paths p
diff --git a/deeptutor/learning/tests/test_read_snapshots.py b/deeptutor/learning/tests/test_read_snapshots.py
new file mode 100644
index 000000000..6c58f0a08
--- /dev/null
+++ b/deeptutor/learning/tests/test_read_snapshots.py
@@ -0,0 +1,63 @@
+"""Read aggregates and their projections from one committed database revision."""
+
+from contextlib import contextmanager
+from pathlib import Path
+import sqlite3
+from typing import Iterator
+
+import pytest
+
+from deeptutor.learning.models import LearningEvidence, LearningProgress, TopicMetadata
+from deeptutor.learning.storage import LearningStore
+
+
[email protected]("view", ["evidence", "atlas"])
+def test_read_view_survives_interleaved_commit(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, view: str
+) -> None:
+ """A real second connection commits between the reader's SELECT statements."""
+ reader = LearningStore(root=tmp_path)
+ writer = LearningStore(root=tmp_path)
+ progress = LearningProgress(book_id="snapshot", name="before")
+ reader.save(progress)
+ reader.put_topic(TopicMetadata(path_id="snapshot", goal="before"), [])
+ original_connect = reader._connect
+ wrote = False
+
+ def commit_between_queries(statement: str) -> None:
+ nonlocal wrote
+ boundary = (
+ "SELECT evidence_json FROM mastery_learning_evidence"
+ if view == "evidence"
+ else "SELECT * FROM mastery_topic_meta WHERE status"
+ )
+ if wrote or boundary not in statement:
+ return
+ wrote = True
+ with writer.transaction("snapshot") as tx:
+ tx.progress.name = "after"
+ tx.progress.learning_evidence.append(
+ LearningEvidence(knowledge_point_id="kp1", result="correct")
+ )
+ tx.put_topic(TopicMetadata(path_id="snapshot", goal="after"), [])
+ tx.touch()
+
+ @contextmanager
+ def interleaved_connect(*, initialize: bool = True) -> Iterator[sqlite3.Connection]:
+ with original_connect(initialize=initialize) as conn:
+ conn.set_trace_callback(commit_between_queries)
+ yield conn
+
+ monkeypatch.setattr(reader, "_connect", interleaved_connect)
+ if view == "evidence":
+ loaded, evidence, count = reader.load_with_learning_evidence("snapshot", "kp1")
+ assert loaded is not None and loaded.name == "before"
+ assert evidence == loaded.learning_evidence == []
+ assert count == 0
+ else:
+ [(loaded, topic, _sessions, _interaction)] = reader.list_topic_snapshots()
+ assert loaded.name == topic.metadata.goal == "before"
+ assert wrote
+ current = writer.load("snapshot")
+ assert current is not None and current.name == "after"
+ assert len(current.learning_evidence) == 1