studysystem 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- studysystem/__init__.py +1 -0
- studysystem/assets/note-callouts.lua +100 -0
- studysystem/assets/note-no-html.lua +28 -0
- studysystem/assets/note.css +146 -0
- studysystem/cli.py +344 -0
- studysystem/dashboard.html +781 -0
- studysystem/dashboard.py +377 -0
- studysystem/db/__init__.py +1 -0
- studysystem/db/engine.py +158 -0
- studysystem/db/migrate.py +122 -0
- studysystem/db/migrations/env.py +59 -0
- studysystem/db/migrations/script.py.mako +29 -0
- studysystem/db/migrations/versions/0000_baseline.py +19 -0
- studysystem/db/migrations/versions/0001_the_whole_schema.py +1743 -0
- studysystem/db/migrations/versions/0002_semester_ignores_case.py +38 -0
- studysystem/db/migrations/versions/0003_course_vault_folder.py +26 -0
- studysystem/db/migrations/versions/0004_note_pdf_path.py +26 -0
- studysystem/db/migrations/versions/0005_tagging_kind.py +39 -0
- studysystem/db/migrations/versions/0006_practice_mode.py +36 -0
- studysystem/db/migrations/versions/0007_exam_prep_from.py +25 -0
- studysystem/db/migrations/versions/0008_more_papers_kind.py +37 -0
- studysystem/db/migrations/versions/0009_past_exam_month_date.py +28 -0
- studysystem/db/migrations/versions/0010_task_parts.py +70 -0
- studysystem/db/migrations/versions/0011_observation_kind.py +39 -0
- studysystem/db/migrations/versions/0012_profile_claim.py +52 -0
- studysystem/db/migrations/versions/0013_assumed_instructor_papers.py +40 -0
- studysystem/db/migrations/versions/0014_scan_confirmation.py +44 -0
- studysystem/db/migrations/versions/0015_practice_item_count.py +37 -0
- studysystem/db/migrations/versions/0016_timetable_patterns.py +102 -0
- studysystem/db/migrations/versions/0017_course_dropped.py +36 -0
- studysystem/db/migrations/versions/0018_syllabus_topics.py +82 -0
- studysystem/db/migrations/versions/0019_course_lab_group.py +36 -0
- studysystem/db/migrations/versions/0020_chapter_scope.py +61 -0
- studysystem/db/migrations/versions/0021_note_retired_at.py +28 -0
- studysystem/db/migrations/versions/0022_rescope.py +48 -0
- studysystem/db/migrations/versions/0023_backlog_reason.py +42 -0
- studysystem/db/migrations/versions/0024_guess_sensitivity.py +35 -0
- studysystem/db/migrations/versions/0025_shortfall_inputs.py +83 -0
- studysystem/db/migrations/versions/0026_trials_switch.py +36 -0
- studysystem/db/tables.py +1096 -0
- studysystem/demo.py +722 -0
- studysystem/errors.py +39 -0
- studysystem/server/__init__.py +83 -0
- studysystem/server/__main__.py +5 -0
- studysystem/services/__init__.py +1 -0
- studysystem/services/adherence.py +147 -0
- studysystem/services/assessments.py +171 -0
- studysystem/services/capacity.py +164 -0
- studysystem/services/claims.py +405 -0
- studysystem/services/course_folder.py +82 -0
- studysystem/services/courses.py +176 -0
- studysystem/services/coverage.py +121 -0
- studysystem/services/dashboard_key.py +102 -0
- studysystem/services/feedback.py +412 -0
- studysystem/services/forward_eval.py +65 -0
- studysystem/services/gate.py +77 -0
- studysystem/services/generation.py +2864 -0
- studysystem/services/ids.py +15 -0
- studysystem/services/intake.py +278 -0
- studysystem/services/launcher.py +455 -0
- studysystem/services/lookups.py +91 -0
- studysystem/services/materials.py +97 -0
- studysystem/services/note_markdown.py +241 -0
- studysystem/services/notes.py +396 -0
- studysystem/services/parts.py +65 -0
- studysystem/services/past_exams.py +115 -0
- studysystem/services/pdf_render.py +148 -0
- studysystem/services/pdf_worker.py +184 -0
- studysystem/services/plan.py +1300 -0
- studysystem/services/private_view.py +38 -0
- studysystem/services/profiles.py +544 -0
- studysystem/services/programs.py +16 -0
- studysystem/services/replacements.py +107 -0
- studysystem/services/rescope.py +277 -0
- studysystem/services/settings.py +50 -0
- studysystem/services/setup.py +235 -0
- studysystem/services/shortfall.py +443 -0
- studysystem/services/state.py +846 -0
- studysystem/services/statements.py +222 -0
- studysystem/services/study_loop.py +998 -0
- studysystem/services/summaries.py +369 -0
- studysystem/services/syllabus.py +138 -0
- studysystem/services/system_work.py +113 -0
- studysystem/services/taxonomy.py +459 -0
- studysystem/services/topics.py +477 -0
- studysystem/services/trials.py +201 -0
- studysystem/services/unconfirmed.py +95 -0
- studysystem/services/units.py +34 -0
- studysystem/services/users.py +53 -0
- studysystem/services/values.py +168 -0
- studysystem/startup.py +147 -0
- studysystem/tools/__init__.py +4 -0
- studysystem/tools/course_setup.py +185 -0
- studysystem/tools/dashboard.py +27 -0
- studysystem/tools/feedback.py +66 -0
- studysystem/tools/gate.py +24 -0
- studysystem/tools/generation.py +156 -0
- studysystem/tools/materials.py +32 -0
- studysystem/tools/notes.py +29 -0
- studysystem/tools/past_exams.py +51 -0
- studysystem/tools/ping.py +9 -0
- studysystem/tools/plan.py +40 -0
- studysystem/tools/profiles.py +94 -0
- studysystem/tools/results.py +77 -0
- studysystem/tools/study_loop.py +145 -0
- studysystem/tools/summaries.py +52 -0
- studysystem/tools/topics.py +143 -0
- studysystem-1.0.0.dist-info/METADATA +254 -0
- studysystem-1.0.0.dist-info/RECORD +112 -0
- studysystem-1.0.0.dist-info/WHEEL +4 -0
- studysystem-1.0.0.dist-info/entry_points.txt +3 -0
- studysystem-1.0.0.dist-info/licenses/LICENSE +21 -0
studysystem/db/tables.py
ADDED
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
"""The 33 tables of physical-schema.md as SQLAlchemy Core metadata.
|
|
2
|
+
|
|
3
|
+
This is what the code believes the database looks like. Migration 0001 is the frozen copy of it;
|
|
4
|
+
`alembic revision --autogenerate` diffs the two. Every construct here must run on SQLite and
|
|
5
|
+
Postgres (D-08), so CHECKs use only portable SQL.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import (
|
|
9
|
+
CheckConstraint,
|
|
10
|
+
Column,
|
|
11
|
+
Float,
|
|
12
|
+
ForeignKey,
|
|
13
|
+
Index,
|
|
14
|
+
Integer,
|
|
15
|
+
MetaData,
|
|
16
|
+
Table,
|
|
17
|
+
Text,
|
|
18
|
+
UniqueConstraint,
|
|
19
|
+
func,
|
|
20
|
+
text,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Every constraint gets a name, so a later batch migration can drop it by name (D-08). CHECKs
|
|
24
|
+
# supply their own short name; the convention prefixes the table.
|
|
25
|
+
metadata = MetaData(
|
|
26
|
+
naming_convention={
|
|
27
|
+
"ix": "ix_%(table_name)s_%(column_0_N_name)s",
|
|
28
|
+
"pk": "pk_%(table_name)s",
|
|
29
|
+
"fk": "fk_%(table_name)s_%(column_0_name)s",
|
|
30
|
+
"uq": "uq_%(table_name)s_%(column_0_N_name)s",
|
|
31
|
+
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
|
32
|
+
}
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
TIERS = ["declared", "inferred", "unknown"]
|
|
36
|
+
STUDY_TIERS = ["E1", "E2", "E3"]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# --- column helpers -------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def ulid_pk() -> Column:
|
|
43
|
+
"""`id TEXT PRIMARY KEY` holding a 26-char ULID."""
|
|
44
|
+
return Column("id", Text, CheckConstraint("length(id) = 26", name="id_ulid"), primary_key=True)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def fk(name: str, target: str, *, nullable: bool = False, index: bool = False) -> Column:
|
|
48
|
+
"""A TEXT column referencing `<target>.id`, ON DELETE RESTRICT (nothing deletes; rows retire
|
|
49
|
+
by status)."""
|
|
50
|
+
return Column(
|
|
51
|
+
name, Text, ForeignKey(f"{target}.id", ondelete="RESTRICT"), nullable=nullable, index=index
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def owner(name: str = "user_id") -> Column:
|
|
56
|
+
"""The denormalised owner column, indexed. `user_id` on user-owned rows, `owner_id` on the
|
|
57
|
+
evidence cluster (NN in v1, NULL-means-shared in v2)."""
|
|
58
|
+
|
|
59
|
+
return fk(name, "user", index=True)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def bool_col(name: str, *, default: int = 0, nullable: bool = False) -> Column:
|
|
63
|
+
"""`INTEGER NOT NULL DEFAULT 0, CHECK (x IN (0, 1))`. A nullable bool has no default."""
|
|
64
|
+
return Column(
|
|
65
|
+
name,
|
|
66
|
+
Integer,
|
|
67
|
+
CheckConstraint(f"{name} IN (0,1)", name=f"{name}_bool"),
|
|
68
|
+
server_default=None if nullable else text(str(default)),
|
|
69
|
+
nullable=nullable,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def enum_col(
|
|
74
|
+
name: str, values: list[str], *, nullable: bool = False, default: str | None = None
|
|
75
|
+
) -> Column:
|
|
76
|
+
"""TEXT + CHECK IN (...). Never a lookup table."""
|
|
77
|
+
quoted = ", ".join(f"'{v}'" for v in values)
|
|
78
|
+
return Column(
|
|
79
|
+
name,
|
|
80
|
+
Text,
|
|
81
|
+
CheckConstraint(f"{name} IN ({quoted})", name=f"{name}_enum"),
|
|
82
|
+
server_default=text(f"'{default}'") if default is not None else None,
|
|
83
|
+
nullable=nullable,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def ts(name: str = "created_at", *, nullable: bool = False) -> Column:
|
|
88
|
+
"""ISO-8601 UTC text timestamp."""
|
|
89
|
+
return Column(name, Text, nullable=nullable)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# --- constraint helpers ---------------------------------------------------
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def implies(name: str, a: str, b: str) -> CheckConstraint:
|
|
96
|
+
"""a => b. Both sides must never evaluate to NULL, or the CHECK is silently skipped -
|
|
97
|
+
use `x IS NOT NULL`, not `x`."""
|
|
98
|
+
return CheckConstraint(f"NOT ({a}) OR ({b})", name=name)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def iff(name: str, a: str, b: str) -> CheckConstraint:
|
|
102
|
+
"""a <=> b. Same NULL warning as `implies`."""
|
|
103
|
+
return CheckConstraint(f"(({a}) AND ({b})) OR (NOT ({a}) AND NOT({b}))", name=name)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def tier_pair(field: str, type_, rule: str | None = None, *, default: str | None = None) -> list:
|
|
107
|
+
"""The value column, its `<field>_tier` column, and the tier rule: unknown <=> NULL.
|
|
108
|
+
`rule` is an extra CHECK on the value (e.g. `credits > 0`); NULL passes it, as CHECKs do.
|
|
109
|
+
`default` is the tier's server default - a pair added to a table with rows needs `unknown`.
|
|
110
|
+
Splat into the Table: `*tier_pair("credits", Float, "credits > 0")`."""
|
|
111
|
+
checks = [CheckConstraint(rule, name=f"{field}_range")] if rule is not None else []
|
|
112
|
+
return [
|
|
113
|
+
Column(field, type_, *checks, nullable=True),
|
|
114
|
+
enum_col(f"{field}_tier", TIERS, default=default),
|
|
115
|
+
iff(f"{field}_tier_rule", f"{field}_tier = 'unknown'", f"{field} is NULL"),
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def exactly_one(*cols: str) -> CheckConstraint:
|
|
120
|
+
"""The portable exactly-one form (D-20): a sum of CASE terms equals 1."""
|
|
121
|
+
terms = " + ".join(f"CASE WHEN {c} IS NULL THEN 0 ELSE 1 END" for c in cols)
|
|
122
|
+
return CheckConstraint(f"{terms} = 1", name="exactly_one")
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def partial_unique(name: str, *cols, where: str) -> Index:
|
|
126
|
+
"""A unique index over the rows matching `where`, on both engines."""
|
|
127
|
+
return Index(name, *cols, sqlite_where=text(where), postgresql_where=text(where), unique=True)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# --- identity -------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
user = Table(
|
|
133
|
+
"user",
|
|
134
|
+
metadata,
|
|
135
|
+
ulid_pk(),
|
|
136
|
+
ts(),
|
|
137
|
+
bool_col("trials_on"), # 1 = walkthroughs hide a generated item and ask for a guess (D-313)
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# --- course structure -----------------------------------------------------
|
|
142
|
+
|
|
143
|
+
course = Table(
|
|
144
|
+
"course",
|
|
145
|
+
metadata,
|
|
146
|
+
ulid_pk(),
|
|
147
|
+
owner(),
|
|
148
|
+
Column("code", Text, nullable=False),
|
|
149
|
+
Column("name", Text, nullable=False),
|
|
150
|
+
*tier_pair("instructor", Text),
|
|
151
|
+
*tier_pair("credits", Float, "credits > 0"),
|
|
152
|
+
Column("target_grade", Float, nullable=True),
|
|
153
|
+
CheckConstraint("target_grade >= 0 AND target_grade <= 100", name="target_grade_range"),
|
|
154
|
+
enum_col("strategy", ["default", "catch-up"], default="default"),
|
|
155
|
+
ts("strategy_changed_at", nullable=True),
|
|
156
|
+
bool_col("conceded"),
|
|
157
|
+
bool_col("planned", default=1), # 0 = on his timetable, never planned (D-216)
|
|
158
|
+
ts("dropped_at", nullable=True), # set = not taken this semester, kept as history (D-217)
|
|
159
|
+
bool_col("no_past_papers"), # 1 = declared: no papers exist, so none is asked for (D-223)
|
|
160
|
+
Column("lab_group", Text, nullable=True), # his group in its labs, e.g. G1; NULL = all (D-227)
|
|
161
|
+
ts("conceded_at", nullable=True),
|
|
162
|
+
implies("conceded_at_set", "conceded = 1", "conceded_at IS NOT NULL"),
|
|
163
|
+
Column("source_course_id", Text, nullable=True),
|
|
164
|
+
CheckConstraint("source_course_id IS NULL", name="source_course_v1"),
|
|
165
|
+
Column("semester_name", Text, nullable=False),
|
|
166
|
+
Column("semester_start", Text, nullable=True),
|
|
167
|
+
Column("semester_end", Text, nullable=True),
|
|
168
|
+
CheckConstraint("semester_end > semester_start", name="semester_order"),
|
|
169
|
+
bool_col("semester_end_approx"),
|
|
170
|
+
Column("vault_folder", Text, nullable=True), # under the output folder (D-72, D-347)
|
|
171
|
+
# the shortfall's H for this course; NULL / unknown reads the default (D-294). Migration 0025.
|
|
172
|
+
*tier_pair("hours_per_credit", Float, "hours_per_credit > 0", default="unknown"),
|
|
173
|
+
bool_col("elective"), # 1 = declared elective: listed last by the shortfall alert (D-300)
|
|
174
|
+
ts(),
|
|
175
|
+
)
|
|
176
|
+
Index(
|
|
177
|
+
"ux_course_code",
|
|
178
|
+
course.c.user_id,
|
|
179
|
+
func.lower(course.c.code),
|
|
180
|
+
func.lower(course.c.semester_name),
|
|
181
|
+
unique=True,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
assessment_slot = Table(
|
|
185
|
+
"assessment_slot",
|
|
186
|
+
metadata,
|
|
187
|
+
ulid_pk(),
|
|
188
|
+
owner("owner_id"),
|
|
189
|
+
fk("course_id", "course"),
|
|
190
|
+
Column("name", Text, nullable=False),
|
|
191
|
+
enum_col("kind", ["exam", "project", "lab"]),
|
|
192
|
+
ts("created_at", nullable=False),
|
|
193
|
+
)
|
|
194
|
+
Index(
|
|
195
|
+
"ux_assessment_slot_name",
|
|
196
|
+
assessment_slot.c.course_id,
|
|
197
|
+
func.lower(assessment_slot.c.name),
|
|
198
|
+
unique=True,
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
assessment = Table(
|
|
202
|
+
"assessment",
|
|
203
|
+
metadata,
|
|
204
|
+
ulid_pk(),
|
|
205
|
+
owner(),
|
|
206
|
+
fk("slot_id", "assessment_slot"),
|
|
207
|
+
*tier_pair("weight", Float, "weight >= 0 AND weight <= 100"),
|
|
208
|
+
Column("date", Text, nullable=True),
|
|
209
|
+
bool_col("date_approx"),
|
|
210
|
+
implies("approx_needs_date", "date_approx = 1", "date IS NOT NULL"),
|
|
211
|
+
Column("prep_from", Text, nullable=True), # exam prep from this day; NULL = term (D-119)
|
|
212
|
+
Column("time", Text, nullable=True),
|
|
213
|
+
Column("room", Text, nullable=True),
|
|
214
|
+
enum_col("session_type", ["first", "second"], nullable=True),
|
|
215
|
+
enum_col("status", ["upcoming", "sat", "graded"], default="upcoming"),
|
|
216
|
+
Column("mark", Float, nullable=True),
|
|
217
|
+
CheckConstraint("mark >= 0 AND mark <= 100", name="mark_range_check"),
|
|
218
|
+
iff("check_graded", "mark IS NOT NULL", "status = 'graded'"),
|
|
219
|
+
ts("created_at", nullable=False),
|
|
220
|
+
UniqueConstraint("slot_id", "session_type"),
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
Index("ix_assessment_user_date", assessment.c.user_id, assessment.c.date)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class_meeting = Table(
|
|
227
|
+
"class_meeting",
|
|
228
|
+
metadata,
|
|
229
|
+
ulid_pk(),
|
|
230
|
+
owner(),
|
|
231
|
+
fk("course_id", "course"),
|
|
232
|
+
Column(
|
|
233
|
+
"weekday",
|
|
234
|
+
Integer,
|
|
235
|
+
CheckConstraint("weekday >= 1 AND weekday <= 7", name="weekday_iso"),
|
|
236
|
+
nullable=False,
|
|
237
|
+
),
|
|
238
|
+
Column("start_time", Text, nullable=False),
|
|
239
|
+
Column("end_time", Text, nullable=False),
|
|
240
|
+
CheckConstraint("end_time > start_time", name="time_order"),
|
|
241
|
+
Column("room", Text, nullable=True),
|
|
242
|
+
enum_col("kind", ["lecture", "lab", "tutorial"]),
|
|
243
|
+
# NULL = every week / the whole class. Odd or even is the ISO week number's (D-216).
|
|
244
|
+
enum_col("week_parity", ["odd", "even"], nullable=True),
|
|
245
|
+
Column(
|
|
246
|
+
"group_label",
|
|
247
|
+
Text,
|
|
248
|
+
CheckConstraint("group_label IS NULL OR length(group_label) > 0", name="group_not_empty"),
|
|
249
|
+
nullable=True,
|
|
250
|
+
),
|
|
251
|
+
enum_col("tier", ["declared", "inferred"]),
|
|
252
|
+
fk("source_task_id", "generation_task", nullable=True),
|
|
253
|
+
ts(),
|
|
254
|
+
)
|
|
255
|
+
# A unique key over nullable columns lets two NULLs differ, so the pattern columns are folded
|
|
256
|
+
# to '' (D-216); the CHECK above keeps a real label from being '' itself.
|
|
257
|
+
Index(
|
|
258
|
+
"ux_class_meeting_slot",
|
|
259
|
+
class_meeting.c.course_id,
|
|
260
|
+
class_meeting.c.weekday,
|
|
261
|
+
class_meeting.c.start_time,
|
|
262
|
+
class_meeting.c.kind,
|
|
263
|
+
func.coalesce(class_meeting.c.week_parity, ""),
|
|
264
|
+
func.coalesce(class_meeting.c.group_label, ""),
|
|
265
|
+
unique=True,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
capacity = Table(
|
|
269
|
+
"capacity",
|
|
270
|
+
metadata,
|
|
271
|
+
ulid_pk(),
|
|
272
|
+
owner(),
|
|
273
|
+
Column(
|
|
274
|
+
"hours_per_week",
|
|
275
|
+
Float,
|
|
276
|
+
CheckConstraint("hours_per_week > 0 AND hours_per_week <= 112", name="hours_range"),
|
|
277
|
+
nullable=False,
|
|
278
|
+
),
|
|
279
|
+
Column("from_date", Text, nullable=False),
|
|
280
|
+
ts(),
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
capacity_suggestion = Table(
|
|
284
|
+
"capacity_suggestion",
|
|
285
|
+
metadata,
|
|
286
|
+
ulid_pk(),
|
|
287
|
+
owner(),
|
|
288
|
+
Column(
|
|
289
|
+
"hours_per_week",
|
|
290
|
+
Float,
|
|
291
|
+
CheckConstraint("hours_per_week > 0", name="hours_positive"),
|
|
292
|
+
nullable=False,
|
|
293
|
+
),
|
|
294
|
+
fk("source_task_id", "generation_task"),
|
|
295
|
+
enum_col("status", ["pending", "accepted", "dismissed"], default="pending"),
|
|
296
|
+
ts("resolved_at", nullable=True),
|
|
297
|
+
iff("resolved_when_done", "resolved_at IS NOT NULL", "status <> 'pending'"),
|
|
298
|
+
ts(),
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
topic = Table(
|
|
303
|
+
"topic",
|
|
304
|
+
metadata,
|
|
305
|
+
ulid_pk(),
|
|
306
|
+
owner(),
|
|
307
|
+
fk("course_id", "course"),
|
|
308
|
+
Column("name", Text, nullable=False),
|
|
309
|
+
enum_col(
|
|
310
|
+
"status", ["proposed", "active", "unexamined", "superseded", "declined"], nullable=False
|
|
311
|
+
),
|
|
312
|
+
fk("superseded_by", "topic", nullable=True),
|
|
313
|
+
implies("superseded_by_status", "superseded_by IS NOT NULL", "status = 'superseded'"),
|
|
314
|
+
enum_col("proposed_by", ["material", "profile", "split", "syllabus"]), # syllabus: D-224
|
|
315
|
+
ts("status_changed_at"),
|
|
316
|
+
ts("created_at"),
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
partial_unique(
|
|
320
|
+
"ux_topic_live",
|
|
321
|
+
topic.c.course_id,
|
|
322
|
+
func.lower(topic.c.name),
|
|
323
|
+
where="status IN ('proposed', 'active', 'unexamined')",
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
material = Table(
|
|
328
|
+
"material",
|
|
329
|
+
metadata,
|
|
330
|
+
ulid_pk(),
|
|
331
|
+
owner(),
|
|
332
|
+
fk("course_id", "course"),
|
|
333
|
+
Column("filename", Text, nullable=False),
|
|
334
|
+
Column("file_ref", Text, nullable=False),
|
|
335
|
+
Column(
|
|
336
|
+
"content_sha256",
|
|
337
|
+
Text,
|
|
338
|
+
CheckConstraint("length(content_sha256) = 64", name="sha256_len"),
|
|
339
|
+
nullable=False,
|
|
340
|
+
),
|
|
341
|
+
Column("media_type", Text, nullable=False),
|
|
342
|
+
bool_col("has_text_layer"),
|
|
343
|
+
enum_col(
|
|
344
|
+
"kind",
|
|
345
|
+
["slides", "chapter", "lecture-notes", "textbook", "exercises", "syllabus", "other"],
|
|
346
|
+
),
|
|
347
|
+
ts("added_at"),
|
|
348
|
+
UniqueConstraint("course_id", "content_sha256"),
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
topic_split = Table(
|
|
352
|
+
"topic_split",
|
|
353
|
+
metadata,
|
|
354
|
+
ulid_pk(),
|
|
355
|
+
owner(),
|
|
356
|
+
fk("parent_topic_id", "topic"),
|
|
357
|
+
fk("child_topic_id", "topic"),
|
|
358
|
+
CheckConstraint("child_topic_id <> parent_topic_id", name="not_self"),
|
|
359
|
+
ts(),
|
|
360
|
+
UniqueConstraint("parent_topic_id", "child_topic_id"),
|
|
361
|
+
)
|
|
362
|
+
|
|
363
|
+
# No owner column: its owner is reachable through both FKs (an explicit ownership test case).
|
|
364
|
+
topic_weight = Table(
|
|
365
|
+
"topic_weight",
|
|
366
|
+
metadata,
|
|
367
|
+
ulid_pk(),
|
|
368
|
+
fk("exam_profile_id", "exam_profile"),
|
|
369
|
+
fk("topic_id", "topic"),
|
|
370
|
+
Column(
|
|
371
|
+
"weight",
|
|
372
|
+
Float,
|
|
373
|
+
CheckConstraint("weight >= 0 AND weight <= 100", name="weight_range"),
|
|
374
|
+
nullable=False,
|
|
375
|
+
),
|
|
376
|
+
ts(),
|
|
377
|
+
UniqueConstraint("exam_profile_id", "topic_id"),
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
coverage = Table(
|
|
382
|
+
"coverage",
|
|
383
|
+
metadata,
|
|
384
|
+
ulid_pk(),
|
|
385
|
+
owner(),
|
|
386
|
+
fk("topic_id", "topic"),
|
|
387
|
+
fk("assessment_id", "assessment", nullable=True),
|
|
388
|
+
fk("material_id", "material", nullable=True),
|
|
389
|
+
exactly_one("material_id", "assessment_id"),
|
|
390
|
+
enum_col("tier", ["declared", "inferred"]),
|
|
391
|
+
ts("confirmed_at", nullable=True),
|
|
392
|
+
ts("created_at"),
|
|
393
|
+
# set when a rescope leaves an inferred exam edge with no live question behind it; cleared
|
|
394
|
+
# when one lands again - the same row comes back (D-261)
|
|
395
|
+
ts("retired_at", nullable=True),
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
partial_unique(
|
|
400
|
+
"ux_coverage_material",
|
|
401
|
+
coverage.c.topic_id,
|
|
402
|
+
coverage.c.material_id,
|
|
403
|
+
where="material_id IS NOT NULL",
|
|
404
|
+
)
|
|
405
|
+
partial_unique(
|
|
406
|
+
"ux_coverage_assessment",
|
|
407
|
+
coverage.c.topic_id,
|
|
408
|
+
coverage.c.assessment_id,
|
|
409
|
+
where="assessment_id IS NOT NULL",
|
|
410
|
+
)
|
|
411
|
+
Index("ix_coverage_topic", coverage.c.topic_id)
|
|
412
|
+
Index("ix_coverage_material", coverage.c.material_id)
|
|
413
|
+
Index("ix_coverage_user_confirmed", coverage.c.user_id, coverage.c.confirmed_at)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
practice_item = Table(
|
|
417
|
+
"practice_item",
|
|
418
|
+
metadata,
|
|
419
|
+
ulid_pk(),
|
|
420
|
+
owner(),
|
|
421
|
+
fk("topic_id", "topic"),
|
|
422
|
+
fk("task_id", "generation_task"),
|
|
423
|
+
Column(
|
|
424
|
+
"item_ordinal",
|
|
425
|
+
Integer,
|
|
426
|
+
CheckConstraint("item_ordinal >= 1", name="item_ordinal_min"),
|
|
427
|
+
nullable=False,
|
|
428
|
+
),
|
|
429
|
+
enum_col("origin", ["generated", "past-exam", "textbook", "instructor-set"]),
|
|
430
|
+
fk("past_exam_id", "past_exam", nullable=True),
|
|
431
|
+
iff("past_exam_origin", "past_exam_id IS NOT NULL", "origin = 'past-exam'"),
|
|
432
|
+
Column(
|
|
433
|
+
"position", Integer, CheckConstraint("position >= 1", name="position_min"), nullable=True
|
|
434
|
+
),
|
|
435
|
+
iff("position_past_exam", "position IS NOT NULL", "past_exam_id IS NOT NULL"),
|
|
436
|
+
Column("page", Integer, CheckConstraint("page >= 1", name="page_min"), nullable=True),
|
|
437
|
+
Column("question", Text, nullable=False),
|
|
438
|
+
Column("answer_key", Text, nullable=True),
|
|
439
|
+
enum_col("answer_provenance", ["verified-source", "unverified", "none"]),
|
|
440
|
+
iff("answer_exists", "answer_provenance = 'none'", "answer_key IS NULL"),
|
|
441
|
+
Column("marks", Float, CheckConstraint("marks >= 0", name="marks_min"), nullable=True),
|
|
442
|
+
enum_col("state", ["live", "needs-review", "rejected", "superseded"], default="live"),
|
|
443
|
+
enum_col("source_marker", ["from-material", "model-knowledge"]),
|
|
444
|
+
implies("past_exam_marker", "origin = 'past-exam'", "source_marker = 'from-material'"),
|
|
445
|
+
Column(
|
|
446
|
+
"novelty_score",
|
|
447
|
+
Float,
|
|
448
|
+
CheckConstraint("novelty_score >= 0 AND novelty_score <= 1", name="novelty_range"),
|
|
449
|
+
nullable=True,
|
|
450
|
+
),
|
|
451
|
+
iff("novelty_generated", "novelty_score IS NOT NULL", "origin = 'generated'"),
|
|
452
|
+
fk("nearest_item_id", "practice_item", nullable=True),
|
|
453
|
+
ts(),
|
|
454
|
+
UniqueConstraint("task_id", "item_ordinal"),
|
|
455
|
+
)
|
|
456
|
+
partial_unique(
|
|
457
|
+
"ux_practice_item_position",
|
|
458
|
+
practice_item.c.past_exam_id,
|
|
459
|
+
practice_item.c.position,
|
|
460
|
+
# a rescoped scan's superseded items free their positions for the redo (D-264)
|
|
461
|
+
where="past_exam_id IS NOT NULL AND state <> 'superseded'",
|
|
462
|
+
)
|
|
463
|
+
Index("ix_practice_item_topic_state", practice_item.c.topic_id, practice_item.c.state)
|
|
464
|
+
Index("ix_practice_item_origin_topic", practice_item.c.origin, practice_item.c.topic_id)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
evidence = Table(
|
|
468
|
+
"evidence",
|
|
469
|
+
metadata,
|
|
470
|
+
ulid_pk(),
|
|
471
|
+
owner(),
|
|
472
|
+
fk("practice_item_id", "practice_item", nullable=True),
|
|
473
|
+
fk("memory_item_id", "memory_item", nullable=True),
|
|
474
|
+
fk("observation_id", "observation", nullable=True),
|
|
475
|
+
exactly_one("practice_item_id", "memory_item_id", "observation_id"),
|
|
476
|
+
enum_col("study_tier", STUDY_TIERS),
|
|
477
|
+
Column("locator", Text, nullable=True),
|
|
478
|
+
ts("created_at"),
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
hours_entry = Table(
|
|
482
|
+
"hours_entry",
|
|
483
|
+
metadata,
|
|
484
|
+
ulid_pk(),
|
|
485
|
+
owner(),
|
|
486
|
+
fk("course_id", "course"),
|
|
487
|
+
fk("session_id", "study_session", nullable=True),
|
|
488
|
+
fk("topic_id", "topic", nullable=True),
|
|
489
|
+
Column("setup_key", Text, nullable=True),
|
|
490
|
+
fk("material_id", "material", nullable=True), # a chapter's minutes (D-238)
|
|
491
|
+
# at most one scope; all NULL is the cold-start case and is legal
|
|
492
|
+
CheckConstraint(
|
|
493
|
+
"CASE WHEN topic_id IS NULL THEN 0 ELSE 1 END"
|
|
494
|
+
" + CASE WHEN setup_key IS NULL THEN 0 ELSE 1 END"
|
|
495
|
+
" + CASE WHEN material_id IS NULL THEN 0 ELSE 1 END <= 1",
|
|
496
|
+
name="one_scope",
|
|
497
|
+
),
|
|
498
|
+
Column(
|
|
499
|
+
"minutes",
|
|
500
|
+
Integer,
|
|
501
|
+
CheckConstraint("minutes >= 1 AND minutes <= 720", name="minutes_range"),
|
|
502
|
+
nullable=False,
|
|
503
|
+
),
|
|
504
|
+
ts("occurred_at"),
|
|
505
|
+
enum_col("source", ["tool", "timer", "sweep"]),
|
|
506
|
+
enum_col(
|
|
507
|
+
"deviation_reason",
|
|
508
|
+
["external-deadline", "disagreed", "too-tired", "topic-wrong", "backlog", "unexplained"],
|
|
509
|
+
nullable=True,
|
|
510
|
+
),
|
|
511
|
+
ts("voided_at", nullable=True),
|
|
512
|
+
ts(),
|
|
513
|
+
)
|
|
514
|
+
Index("ix_hours_entry_user_occurred", hours_entry.c.user_id, hours_entry.c.occurred_at)
|
|
515
|
+
Index("ix_hours_entry_user_setup", hours_entry.c.user_id, hours_entry.c.setup_key)
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
coverage_reading = Table(
|
|
519
|
+
"coverage_reading",
|
|
520
|
+
metadata,
|
|
521
|
+
ulid_pk(),
|
|
522
|
+
owner(),
|
|
523
|
+
fk("course_id", "course"),
|
|
524
|
+
Column(
|
|
525
|
+
"value",
|
|
526
|
+
Float,
|
|
527
|
+
CheckConstraint("value >= 0 AND value <= 1", name="value_range"),
|
|
528
|
+
nullable=False,
|
|
529
|
+
),
|
|
530
|
+
enum_col("denominator_tier", ["weighted", "inferred", "unweighted"]),
|
|
531
|
+
Column("tier_note", Text, nullable=False),
|
|
532
|
+
ts("computed_at"),
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
# --- evidence cluster: owner_id NN in v1 ------------------------------------
|
|
537
|
+
|
|
538
|
+
past_exam = Table(
|
|
539
|
+
"past_exam",
|
|
540
|
+
metadata,
|
|
541
|
+
ulid_pk(),
|
|
542
|
+
owner("owner_id"),
|
|
543
|
+
fk("slot_id", "assessment_slot"),
|
|
544
|
+
enum_col("session_type", ["first", "second"]),
|
|
545
|
+
Column("session_date", Text, nullable=False),
|
|
546
|
+
bool_col("session_date_approx"), # 1: the paper prints only its month, stored as the 1st
|
|
547
|
+
bool_col("session_delayed"),
|
|
548
|
+
*tier_pair("instructor", Text),
|
|
549
|
+
bool_col("has_text_layer"),
|
|
550
|
+
Column("file_ref", Text, nullable=False),
|
|
551
|
+
Column(
|
|
552
|
+
"content_sha256",
|
|
553
|
+
Text,
|
|
554
|
+
CheckConstraint("length(content_sha256) = 64", name="sha256_len"),
|
|
555
|
+
nullable=False,
|
|
556
|
+
),
|
|
557
|
+
fk("transcript_task_id", "generation_task", nullable=True),
|
|
558
|
+
ts("transcript_confirmed_at", nullable=True), # a scan counts only once he confirms (D-210)
|
|
559
|
+
ts(),
|
|
560
|
+
UniqueConstraint("slot_id", "content_sha256"),
|
|
561
|
+
)
|
|
562
|
+
Index("ix_past_exam_slot_date", past_exam.c.slot_id, past_exam.c.session_date)
|
|
563
|
+
|
|
564
|
+
CLAIM_KEYS = {
|
|
565
|
+
"syllabus-driven": ["topic-marks"],
|
|
566
|
+
"format": ["duration-minutes", "total-marks", "question-count", "has-mcq"],
|
|
567
|
+
"instructor-driven": ["archetype", "phrasing", "trap"],
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
def _claim_class_rule() -> str:
|
|
572
|
+
"""`claim_class` is fixed by `claim_key`: one OR-branch per class."""
|
|
573
|
+
branches = []
|
|
574
|
+
for cls, keys in CLAIM_KEYS.items():
|
|
575
|
+
quoted = ", ".join(f"'{k}'" for k in keys)
|
|
576
|
+
branches.append(f"(claim_key IN ({quoted}) AND claim_class = '{cls}')")
|
|
577
|
+
return " OR ".join(branches)
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
observation = Table(
|
|
581
|
+
"observation",
|
|
582
|
+
metadata,
|
|
583
|
+
ulid_pk(),
|
|
584
|
+
owner("owner_id"),
|
|
585
|
+
fk("slot_id", "assessment_slot"),
|
|
586
|
+
fk("past_exam_id", "past_exam", nullable=True),
|
|
587
|
+
enum_col("claim_key", [k for keys in CLAIM_KEYS.values() for k in keys]),
|
|
588
|
+
enum_col("claim_class", list(CLAIM_KEYS)),
|
|
589
|
+
CheckConstraint(_claim_class_rule(), name="claim_class_by_key"),
|
|
590
|
+
fk("topic_id", "topic", nullable=True),
|
|
591
|
+
iff("topic_for_marks", "topic_id IS NOT NULL", "claim_key = 'topic-marks'"),
|
|
592
|
+
Column("claim_value", Text, nullable=False),
|
|
593
|
+
enum_col("study_tier", STUDY_TIERS),
|
|
594
|
+
Column("citation", Text, nullable=True),
|
|
595
|
+
ts(),
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
exam_profile = Table(
|
|
599
|
+
"exam_profile",
|
|
600
|
+
metadata,
|
|
601
|
+
ulid_pk(),
|
|
602
|
+
owner("owner_id"),
|
|
603
|
+
fk("slot_id", "assessment_slot"),
|
|
604
|
+
Column("version", Integer, CheckConstraint("version >= 1", name="version_min"), nullable=False),
|
|
605
|
+
ts("derived_at"),
|
|
606
|
+
bool_col("provisional_syllabus"),
|
|
607
|
+
bool_col("provisional_instructor"),
|
|
608
|
+
bool_col("provisional_format"),
|
|
609
|
+
Column("recency_weight_newest", Integer, server_default=text("3"), nullable=False),
|
|
610
|
+
Column("recency_weight_next_two", Integer, server_default=text("2"), nullable=False),
|
|
611
|
+
Column("recency_weight_older", Integer, server_default=text("1"), nullable=False),
|
|
612
|
+
Column(
|
|
613
|
+
"evidence_count_syllabus",
|
|
614
|
+
Integer,
|
|
615
|
+
CheckConstraint("evidence_count_syllabus >= 0", name="count_syllabus_min"),
|
|
616
|
+
nullable=False,
|
|
617
|
+
),
|
|
618
|
+
Column(
|
|
619
|
+
"evidence_count_instructor",
|
|
620
|
+
Integer,
|
|
621
|
+
CheckConstraint("evidence_count_instructor >= 0", name="count_instructor_min"),
|
|
622
|
+
nullable=False,
|
|
623
|
+
),
|
|
624
|
+
Column(
|
|
625
|
+
"evidence_count_format",
|
|
626
|
+
Integer,
|
|
627
|
+
CheckConstraint("evidence_count_format >= 0", name="count_format_min"),
|
|
628
|
+
nullable=False,
|
|
629
|
+
),
|
|
630
|
+
# Papers in its set whose instructor was assumed (D-174, D-176); 0 on rows from before 0013,
|
|
631
|
+
# which assumed none.
|
|
632
|
+
Column(
|
|
633
|
+
"assumed_instructor_papers",
|
|
634
|
+
Integer,
|
|
635
|
+
CheckConstraint("assumed_instructor_papers >= 0", name="count_assumed_min"),
|
|
636
|
+
server_default=text("0"),
|
|
637
|
+
nullable=False,
|
|
638
|
+
),
|
|
639
|
+
UniqueConstraint("slot_id", "version"),
|
|
640
|
+
)
|
|
641
|
+
|
|
642
|
+
# One row per observation a profile version kept by its class's rule (D-156). No owner column,
|
|
643
|
+
# as `topic_weight`: an explicit ownership test case.
|
|
644
|
+
profile_claim = Table(
|
|
645
|
+
"profile_claim",
|
|
646
|
+
metadata,
|
|
647
|
+
ulid_pk(),
|
|
648
|
+
fk("exam_profile_id", "exam_profile"),
|
|
649
|
+
fk("observation_id", "observation"),
|
|
650
|
+
ts(),
|
|
651
|
+
UniqueConstraint("exam_profile_id", "observation_id"),
|
|
652
|
+
)
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
# --- generation -------------------------------------------------------------
|
|
656
|
+
|
|
657
|
+
generation_task = Table(
|
|
658
|
+
"generation_task",
|
|
659
|
+
metadata,
|
|
660
|
+
ulid_pk(),
|
|
661
|
+
owner(),
|
|
662
|
+
enum_col(
|
|
663
|
+
"kind",
|
|
664
|
+
["note", "practice", "memory", "transcription", "timetable", "tagging", "observation"],
|
|
665
|
+
),
|
|
666
|
+
# documented discriminator exception: target existence is checked by the service
|
|
667
|
+
enum_col("scope_type", ["course", "material", "topic", "past-exam", "user"]),
|
|
668
|
+
Column("scope_id", Text, nullable=False),
|
|
669
|
+
fk("exam_profile_id", "exam_profile", nullable=True),
|
|
670
|
+
enum_col("route", ["host", "sampling"]),
|
|
671
|
+
Column(
|
|
672
|
+
"bytes_sent",
|
|
673
|
+
Integer,
|
|
674
|
+
CheckConstraint("bytes_sent >= 0", name="bytes_sent_min"),
|
|
675
|
+
server_default=text("0"),
|
|
676
|
+
nullable=False,
|
|
677
|
+
),
|
|
678
|
+
Column(
|
|
679
|
+
"bytes_returned",
|
|
680
|
+
Integer,
|
|
681
|
+
CheckConstraint("bytes_returned >= 0", name="bytes_returned_min"),
|
|
682
|
+
server_default=text("0"),
|
|
683
|
+
nullable=False,
|
|
684
|
+
),
|
|
685
|
+
# A task too big for one reply goes out in parts, fetched in order (D-145, migration 0010):
|
|
686
|
+
# `parts` in the current layout, `parts_sent` the highest fetched, `layout_hash` the sha256
|
|
687
|
+
# of the content they were cut from - a later part cut from other content is refused.
|
|
688
|
+
Column(
|
|
689
|
+
"parts",
|
|
690
|
+
Integer,
|
|
691
|
+
CheckConstraint("parts >= 1", name="parts_min"),
|
|
692
|
+
server_default=text("1"),
|
|
693
|
+
nullable=False,
|
|
694
|
+
),
|
|
695
|
+
Column(
|
|
696
|
+
"parts_sent",
|
|
697
|
+
Integer,
|
|
698
|
+
CheckConstraint("parts_sent >= 0", name="parts_sent_min"),
|
|
699
|
+
server_default=text("0"),
|
|
700
|
+
nullable=False,
|
|
701
|
+
),
|
|
702
|
+
Column(
|
|
703
|
+
"layout_hash",
|
|
704
|
+
Text,
|
|
705
|
+
CheckConstraint("length(layout_hash) = 64", name="layout_hash_len"),
|
|
706
|
+
nullable=True,
|
|
707
|
+
),
|
|
708
|
+
CheckConstraint("parts_sent <= parts", name="parts_sent_max"),
|
|
709
|
+
# How many items a practice task asked for (D-211, migration 0015); NULL = the host chooses.
|
|
710
|
+
Column(
|
|
711
|
+
"item_count",
|
|
712
|
+
Integer,
|
|
713
|
+
CheckConstraint("item_count >= 1", name="item_count_min"),
|
|
714
|
+
nullable=True,
|
|
715
|
+
),
|
|
716
|
+
enum_col("status", ["open", "closed", "partial", "superseded", "invalidated"], default="open"),
|
|
717
|
+
fk("superseded_by", "generation_task", nullable=True),
|
|
718
|
+
iff("superseded_by_status", "superseded_by IS NOT NULL", "status = 'superseded'"),
|
|
719
|
+
ts(),
|
|
720
|
+
ts("closed_at", nullable=True),
|
|
721
|
+
iff("closed_when_not_open", "closed_at IS NOT NULL", "status <> 'open'"),
|
|
722
|
+
)
|
|
723
|
+
|
|
724
|
+
submission = Table(
|
|
725
|
+
"submission",
|
|
726
|
+
metadata,
|
|
727
|
+
ulid_pk(),
|
|
728
|
+
owner(),
|
|
729
|
+
fk("task_id", "generation_task"),
|
|
730
|
+
Column(
|
|
731
|
+
"ordinal",
|
|
732
|
+
Integer,
|
|
733
|
+
CheckConstraint("ordinal >= 1 AND ordinal <= 3", name="ordinal_range"),
|
|
734
|
+
nullable=False,
|
|
735
|
+
),
|
|
736
|
+
Column("payload", Text, nullable=False),
|
|
737
|
+
Column("validation_result", Text, nullable=False),
|
|
738
|
+
Column(
|
|
739
|
+
"accepted_count",
|
|
740
|
+
Integer,
|
|
741
|
+
CheckConstraint("accepted_count >= 0", name="accepted_min"),
|
|
742
|
+
nullable=False,
|
|
743
|
+
),
|
|
744
|
+
Column(
|
|
745
|
+
"rejected_count",
|
|
746
|
+
Integer,
|
|
747
|
+
CheckConstraint("rejected_count >= 0", name="rejected_min"),
|
|
748
|
+
nullable=False,
|
|
749
|
+
),
|
|
750
|
+
Column(
|
|
751
|
+
"bytes_sent",
|
|
752
|
+
Integer,
|
|
753
|
+
CheckConstraint("bytes_sent >= 0", name="bytes_sent_min"),
|
|
754
|
+
nullable=False,
|
|
755
|
+
),
|
|
756
|
+
Column(
|
|
757
|
+
"bytes_returned",
|
|
758
|
+
Integer,
|
|
759
|
+
CheckConstraint("bytes_returned >= 0", name="bytes_returned_min"),
|
|
760
|
+
nullable=False,
|
|
761
|
+
),
|
|
762
|
+
ts(),
|
|
763
|
+
UniqueConstraint("task_id", "ordinal"),
|
|
764
|
+
)
|
|
765
|
+
|
|
766
|
+
note = Table(
|
|
767
|
+
"note",
|
|
768
|
+
metadata,
|
|
769
|
+
ulid_pk(),
|
|
770
|
+
owner(),
|
|
771
|
+
fk("material_id", "material"),
|
|
772
|
+
fk("task_id", "generation_task"),
|
|
773
|
+
Column(
|
|
774
|
+
"section_ordinal",
|
|
775
|
+
Integer,
|
|
776
|
+
CheckConstraint("section_ordinal >= 1", name="section_ordinal_min"),
|
|
777
|
+
server_default=text("1"),
|
|
778
|
+
nullable=False,
|
|
779
|
+
),
|
|
780
|
+
fk("exam_profile_id", "exam_profile", nullable=True),
|
|
781
|
+
Column("body", Text, nullable=False),
|
|
782
|
+
Column("exported_path", Text, nullable=True),
|
|
783
|
+
Column("pdf_path", Text, nullable=True),
|
|
784
|
+
ts(),
|
|
785
|
+
ts("retired_at", nullable=True), # set when a redo's note replaces this one (D-250)
|
|
786
|
+
UniqueConstraint("task_id", "section_ordinal"),
|
|
787
|
+
)
|
|
788
|
+
|
|
789
|
+
memory_item = Table(
|
|
790
|
+
"memory_item",
|
|
791
|
+
metadata,
|
|
792
|
+
ulid_pk(),
|
|
793
|
+
owner(),
|
|
794
|
+
fk("topic_id", "topic"),
|
|
795
|
+
fk("task_id", "generation_task"),
|
|
796
|
+
Column(
|
|
797
|
+
"item_ordinal",
|
|
798
|
+
Integer,
|
|
799
|
+
CheckConstraint("item_ordinal >= 1", name="item_ordinal_min"),
|
|
800
|
+
nullable=False,
|
|
801
|
+
),
|
|
802
|
+
Column("term", Text, nullable=False),
|
|
803
|
+
Column("definition", Text, nullable=False),
|
|
804
|
+
Column("formula", Text, nullable=True),
|
|
805
|
+
enum_col("state", ["live", "needs-review", "rejected", "superseded"], default="live"),
|
|
806
|
+
enum_col("source_marker", ["from-material", "model-knowledge"]),
|
|
807
|
+
ts(),
|
|
808
|
+
UniqueConstraint("task_id", "item_ordinal"),
|
|
809
|
+
)
|
|
810
|
+
|
|
811
|
+
item_rejection = Table(
|
|
812
|
+
"item_rejection",
|
|
813
|
+
metadata,
|
|
814
|
+
ulid_pk(),
|
|
815
|
+
owner(),
|
|
816
|
+
fk("practice_item_id", "practice_item", nullable=True),
|
|
817
|
+
fk("memory_item_id", "memory_item", nullable=True),
|
|
818
|
+
exactly_one("practice_item_id", "memory_item_id"),
|
|
819
|
+
enum_col(
|
|
820
|
+
"reason",
|
|
821
|
+
[
|
|
822
|
+
"off-syllabus",
|
|
823
|
+
"wrong-answer-key",
|
|
824
|
+
"wrong-topic",
|
|
825
|
+
"duplicate",
|
|
826
|
+
"unclear",
|
|
827
|
+
"wrong-difficulty",
|
|
828
|
+
],
|
|
829
|
+
),
|
|
830
|
+
implies(
|
|
831
|
+
"answer_key_is_practice", "reason = 'wrong-answer-key'", "practice_item_id IS NOT NULL"
|
|
832
|
+
),
|
|
833
|
+
Column("note", Text, nullable=True),
|
|
834
|
+
ts(),
|
|
835
|
+
)
|
|
836
|
+
|
|
837
|
+
replacement_queue = Table(
|
|
838
|
+
"replacement_queue",
|
|
839
|
+
metadata,
|
|
840
|
+
ulid_pk(),
|
|
841
|
+
owner(),
|
|
842
|
+
fk("topic_id", "topic"),
|
|
843
|
+
fk("rejection_id", "item_rejection", nullable=True),
|
|
844
|
+
fk("closed_task_id", "generation_task", nullable=True),
|
|
845
|
+
exactly_one("rejection_id", "closed_task_id"),
|
|
846
|
+
enum_col("status", ["pending", "filled", "dropped"], default="pending"),
|
|
847
|
+
fk("filled_by_task_id", "generation_task", nullable=True),
|
|
848
|
+
iff("filled_by_when_filled", "filled_by_task_id IS NOT NULL", "status = 'filled'"),
|
|
849
|
+
ts(),
|
|
850
|
+
ts("resolved_at", nullable=True),
|
|
851
|
+
iff("resolved_when_done", "resolved_at IS NOT NULL", "status <> 'pending'"),
|
|
852
|
+
)
|
|
853
|
+
Index("ix_replacement_queue_user_status", replacement_queue.c.user_id, replacement_queue.c.status)
|
|
854
|
+
|
|
855
|
+
generation_directive = Table(
|
|
856
|
+
"generation_directive",
|
|
857
|
+
metadata,
|
|
858
|
+
ulid_pk(),
|
|
859
|
+
owner(),
|
|
860
|
+
fk("course_id", "course", nullable=True),
|
|
861
|
+
fk("topic_id", "topic", nullable=True),
|
|
862
|
+
enum_col("kind", ["format-preference", "generator-correction"]),
|
|
863
|
+
Column("text", Text, nullable=False),
|
|
864
|
+
enum_col("status", ["proposed", "active", "superseded"], default="proposed"),
|
|
865
|
+
fk("superseded_by", "generation_directive", nullable=True),
|
|
866
|
+
iff("superseded_by_status", "superseded_by IS NOT NULL", "status = 'superseded'"),
|
|
867
|
+
fk("source_rejection_id", "item_rejection", nullable=True),
|
|
868
|
+
ts(),
|
|
869
|
+
ts("confirmed_at", nullable=True),
|
|
870
|
+
iff("confirmed_when_not_proposed", "confirmed_at IS NOT NULL", "status <> 'proposed'"),
|
|
871
|
+
)
|
|
872
|
+
Index(
|
|
873
|
+
"ix_generation_directive_user_status",
|
|
874
|
+
generation_directive.c.user_id,
|
|
875
|
+
generation_directive.c.status,
|
|
876
|
+
)
|
|
877
|
+
Index(
|
|
878
|
+
"ix_generation_directive_course_status",
|
|
879
|
+
generation_directive.c.course_id,
|
|
880
|
+
generation_directive.c.status,
|
|
881
|
+
)
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
# --- study and performance ----------------------------------------------------
|
|
885
|
+
|
|
886
|
+
study_session = Table(
|
|
887
|
+
"study_session",
|
|
888
|
+
metadata,
|
|
889
|
+
ulid_pk(),
|
|
890
|
+
owner(),
|
|
891
|
+
fk("course_id", "course"),
|
|
892
|
+
enum_col("mode", ["explain-chapter", "exam-walkthrough", "mock", "review-quiz", "practice"]),
|
|
893
|
+
# documented discriminator exception: target existence is checked by the service
|
|
894
|
+
enum_col("subject_type", ["topic", "material", "past-exam"], nullable=True),
|
|
895
|
+
Column("subject_id", Text, nullable=True),
|
|
896
|
+
iff("subject_pair", "subject_id IS NULL", "subject_type IS NULL"),
|
|
897
|
+
Column(
|
|
898
|
+
"position", Integer, CheckConstraint("position >= 1", name="position_min"), nullable=True
|
|
899
|
+
),
|
|
900
|
+
implies("position_walkthrough_only", "position IS NOT NULL", "mode = 'exam-walkthrough'"),
|
|
901
|
+
enum_col("source", ["system", "manual"]),
|
|
902
|
+
ts("started_at"),
|
|
903
|
+
ts("expires_at"),
|
|
904
|
+
ts("ended_at", nullable=True),
|
|
905
|
+
CheckConstraint("ended_at >= started_at", name="ended_after_start"),
|
|
906
|
+
)
|
|
907
|
+
|
|
908
|
+
attempt = Table(
|
|
909
|
+
"attempt",
|
|
910
|
+
metadata,
|
|
911
|
+
ulid_pk(),
|
|
912
|
+
owner(),
|
|
913
|
+
fk("session_id", "study_session", nullable=True),
|
|
914
|
+
implies(
|
|
915
|
+
"no_session_only_offline",
|
|
916
|
+
"session_id IS NULL",
|
|
917
|
+
"source IN ('self-reported', 'exam-retro')",
|
|
918
|
+
),
|
|
919
|
+
fk("practice_item_id", "practice_item", nullable=True),
|
|
920
|
+
fk("topic_id", "topic"),
|
|
921
|
+
enum_col(
|
|
922
|
+
"source",
|
|
923
|
+
[
|
|
924
|
+
"comprehension-check",
|
|
925
|
+
"practice",
|
|
926
|
+
"review-quiz",
|
|
927
|
+
"exam-walkthrough",
|
|
928
|
+
"exam-retro",
|
|
929
|
+
"self-reported",
|
|
930
|
+
],
|
|
931
|
+
),
|
|
932
|
+
bool_col("assisted"),
|
|
933
|
+
implies("walkthrough_is_assisted", "source = 'exam-walkthrough'", "assisted = 1"),
|
|
934
|
+
bool_col("correct", nullable=True),
|
|
935
|
+
Column(
|
|
936
|
+
"score",
|
|
937
|
+
Float,
|
|
938
|
+
CheckConstraint("score >= 0 AND score <= 1", name="score_range"),
|
|
939
|
+
nullable=True,
|
|
940
|
+
),
|
|
941
|
+
CheckConstraint("correct IS NOT NULL OR score IS NOT NULL", name="some_result"),
|
|
942
|
+
enum_col(
|
|
943
|
+
"root_cause",
|
|
944
|
+
["concept", "recall", "procedure", "misread", "careless", "time-pressure"],
|
|
945
|
+
nullable=True,
|
|
946
|
+
),
|
|
947
|
+
Column("fix_rule", Text, nullable=True),
|
|
948
|
+
bool_col("correctness_voided"),
|
|
949
|
+
ts(),
|
|
950
|
+
)
|
|
951
|
+
Index("ix_attempt_user_topic", attempt.c.user_id, attempt.c.topic_id)
|
|
952
|
+
|
|
953
|
+
discrimination_trial = Table(
|
|
954
|
+
"discrimination_trial",
|
|
955
|
+
metadata,
|
|
956
|
+
ulid_pk(),
|
|
957
|
+
owner(),
|
|
958
|
+
fk("session_id", "study_session"),
|
|
959
|
+
fk("practice_item_id", "practice_item"),
|
|
960
|
+
fk("topic_id", "topic"),
|
|
961
|
+
bool_col("was_generated"),
|
|
962
|
+
bool_col("flagged"),
|
|
963
|
+
enum_col(
|
|
964
|
+
"tell", ["too-easy", "phrasing", "topic-scope", "formatting", "no-tell"], nullable=True
|
|
965
|
+
),
|
|
966
|
+
ts(),
|
|
967
|
+
UniqueConstraint("session_id", "practice_item_id"),
|
|
968
|
+
)
|
|
969
|
+
Index(
|
|
970
|
+
"ix_discrimination_trial_user_generated",
|
|
971
|
+
discrimination_trial.c.user_id,
|
|
972
|
+
discrimination_trial.c.was_generated,
|
|
973
|
+
)
|
|
974
|
+
|
|
975
|
+
# The one natural key: no ULID, PK (user_id, topic_id).
|
|
976
|
+
topic_state = Table(
|
|
977
|
+
"topic_state",
|
|
978
|
+
metadata,
|
|
979
|
+
Column("user_id", Text, ForeignKey("user.id", ondelete="RESTRICT"), primary_key=True),
|
|
980
|
+
Column("topic_id", Text, ForeignKey("topic.id", ondelete="RESTRICT"), primary_key=True),
|
|
981
|
+
Column("stability", Float, nullable=True),
|
|
982
|
+
Column("difficulty", Float, nullable=True),
|
|
983
|
+
ts("due_at", nullable=True),
|
|
984
|
+
ts("last_reviewed_at", nullable=True),
|
|
985
|
+
Column("reps", Integer, CheckConstraint("reps >= 0", name="reps_min"), nullable=False),
|
|
986
|
+
Column("lapses", Integer, CheckConstraint("lapses >= 0", name="lapses_min"), nullable=False),
|
|
987
|
+
bool_col("inferred"),
|
|
988
|
+
ts("updated_at"),
|
|
989
|
+
)
|
|
990
|
+
Index("ix_topic_state_user_due", topic_state.c.user_id, topic_state.c.due_at)
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
# --- planning -------------------------------------------------------------------
|
|
994
|
+
|
|
995
|
+
plan = Table(
|
|
996
|
+
"plan",
|
|
997
|
+
metadata,
|
|
998
|
+
ulid_pk(),
|
|
999
|
+
owner(),
|
|
1000
|
+
enum_col("scope", ["all-courses", "course"]),
|
|
1001
|
+
fk("scope_course_id", "course", nullable=True),
|
|
1002
|
+
iff("course_scope_has_course", "scope_course_id IS NOT NULL", "scope = 'course'"),
|
|
1003
|
+
Column("shown_n", Integer, CheckConstraint("shown_n >= 1", name="shown_n_min"), nullable=False),
|
|
1004
|
+
Column("strategy_snapshot", Text, nullable=False),
|
|
1005
|
+
# Shown study topics missing from a side ranking with every exam edge left out (D-280,
|
|
1006
|
+
# D-281). NULL: an older plan, or a plan that showed no study topic. Migration 0024.
|
|
1007
|
+
Column(
|
|
1008
|
+
"guess_changed",
|
|
1009
|
+
Integer,
|
|
1010
|
+
CheckConstraint("guess_changed >= 0", name="guess_changed_min"),
|
|
1011
|
+
nullable=True,
|
|
1012
|
+
),
|
|
1013
|
+
ts(),
|
|
1014
|
+
)
|
|
1015
|
+
|
|
1016
|
+
SETUP_KINDS = [
|
|
1017
|
+
"grading",
|
|
1018
|
+
"credits",
|
|
1019
|
+
"exam-date",
|
|
1020
|
+
"target-grade",
|
|
1021
|
+
"first-material",
|
|
1022
|
+
"past-papers",
|
|
1023
|
+
"more-papers", # 1.18, migration 0008 (D-128)
|
|
1024
|
+
]
|
|
1025
|
+
|
|
1026
|
+
plan_item = Table(
|
|
1027
|
+
"plan_item",
|
|
1028
|
+
metadata,
|
|
1029
|
+
ulid_pk(),
|
|
1030
|
+
owner(),
|
|
1031
|
+
fk("plan_id", "plan"),
|
|
1032
|
+
enum_col("lane", ["review", "study", "setup"]),
|
|
1033
|
+
fk("course_id", "course"),
|
|
1034
|
+
fk("topic_id", "topic", nullable=True),
|
|
1035
|
+
iff("topic_unless_setup", "topic_id IS NOT NULL", "lane <> 'setup'"),
|
|
1036
|
+
enum_col("setup_kind", SETUP_KINDS, nullable=True),
|
|
1037
|
+
iff("setup_kind_for_setup", "setup_kind IS NOT NULL", "lane = 'setup'"),
|
|
1038
|
+
Column("setup_key", Text, nullable=True),
|
|
1039
|
+
iff("setup_key_for_setup", "setup_key IS NOT NULL", "lane = 'setup'"),
|
|
1040
|
+
Column("rank", Integer, CheckConstraint("rank >= 1", name="rank_min"), nullable=False),
|
|
1041
|
+
bool_col("shown"),
|
|
1042
|
+
Column("score", Float, nullable=True),
|
|
1043
|
+
iff("score_unless_setup", "score IS NOT NULL", "lane <> 'setup'"),
|
|
1044
|
+
Column("reason", Text, nullable=False),
|
|
1045
|
+
Column(
|
|
1046
|
+
"estimated_minutes",
|
|
1047
|
+
Integer,
|
|
1048
|
+
CheckConstraint("estimated_minutes > 0", name="estimated_minutes_min"),
|
|
1049
|
+
nullable=True,
|
|
1050
|
+
),
|
|
1051
|
+
implies("setup_has_no_minutes", "lane = 'setup'", "estimated_minutes IS NULL"),
|
|
1052
|
+
Column(
|
|
1053
|
+
"marks_unlocked_estimate",
|
|
1054
|
+
Float,
|
|
1055
|
+
CheckConstraint("marks_unlocked_estimate >= 0", name="marks_unlocked_min"),
|
|
1056
|
+
nullable=True,
|
|
1057
|
+
),
|
|
1058
|
+
implies("marks_unlocked_setup_only", "marks_unlocked_estimate IS NOT NULL", "lane = 'setup'"),
|
|
1059
|
+
fk("exam_profile_id", "exam_profile", nullable=True),
|
|
1060
|
+
fk("material_id", "material", nullable=True), # the chapter a study topic shows under (D-240)
|
|
1061
|
+
implies("chapter_study_only", "material_id IS NOT NULL", "lane = 'study'"),
|
|
1062
|
+
UniqueConstraint("plan_id", "lane", "rank"),
|
|
1063
|
+
)
|
|
1064
|
+
Index("ix_plan_item_user_topic", plan_item.c.user_id, plan_item.c.topic_id)
|
|
1065
|
+
Index("ix_plan_item_user_setup", plan_item.c.user_id, plan_item.c.setup_key)
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
# --- one last pass over the metadata ----------------------------------------
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
def _lift_column_checks() -> None:
|
|
1072
|
+
"""Move every CHECK written on a Column onto its Table.
|
|
1073
|
+
|
|
1074
|
+
`alembic revision --autogenerate` renders table-level CHECKs and silently drops
|
|
1075
|
+
column-level ones, so without this, Migration 001 would build a database with 47 of the
|
|
1076
|
+
schema's 175 CHECKs - and the tests, which run on this metadata, would never notice.
|
|
1077
|
+
Moving them changes no constraint name and no emitted SQL; it only changes where
|
|
1078
|
+
autogenerate can see them. They are re-appended ahead of the table's own rules, keeping
|
|
1079
|
+
the order they had as column constraints - SQLite reports the *first* CHECK a row fails,
|
|
1080
|
+
and the tests name the one they expect.
|
|
1081
|
+
"""
|
|
1082
|
+
for table in metadata.tables.values():
|
|
1083
|
+
for col in table.c:
|
|
1084
|
+
for con in list(col.constraints):
|
|
1085
|
+
if not isinstance(con, CheckConstraint):
|
|
1086
|
+
continue
|
|
1087
|
+
col.constraints.discard(con)
|
|
1088
|
+
moved = CheckConstraint(con.sqltext, name=con.name)
|
|
1089
|
+
# CREATE TABLE emits constraints in creation order, and SQLite reports the
|
|
1090
|
+
# first CHECK a row fails. Inheriting the original's order keeps every
|
|
1091
|
+
# failure named the same as when the CHECK sat on the column.
|
|
1092
|
+
moved._creation_order = con._creation_order
|
|
1093
|
+
table.append_constraint(moved)
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
_lift_column_checks()
|