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.
Files changed (112) hide show
  1. studysystem/__init__.py +1 -0
  2. studysystem/assets/note-callouts.lua +100 -0
  3. studysystem/assets/note-no-html.lua +28 -0
  4. studysystem/assets/note.css +146 -0
  5. studysystem/cli.py +344 -0
  6. studysystem/dashboard.html +781 -0
  7. studysystem/dashboard.py +377 -0
  8. studysystem/db/__init__.py +1 -0
  9. studysystem/db/engine.py +158 -0
  10. studysystem/db/migrate.py +122 -0
  11. studysystem/db/migrations/env.py +59 -0
  12. studysystem/db/migrations/script.py.mako +29 -0
  13. studysystem/db/migrations/versions/0000_baseline.py +19 -0
  14. studysystem/db/migrations/versions/0001_the_whole_schema.py +1743 -0
  15. studysystem/db/migrations/versions/0002_semester_ignores_case.py +38 -0
  16. studysystem/db/migrations/versions/0003_course_vault_folder.py +26 -0
  17. studysystem/db/migrations/versions/0004_note_pdf_path.py +26 -0
  18. studysystem/db/migrations/versions/0005_tagging_kind.py +39 -0
  19. studysystem/db/migrations/versions/0006_practice_mode.py +36 -0
  20. studysystem/db/migrations/versions/0007_exam_prep_from.py +25 -0
  21. studysystem/db/migrations/versions/0008_more_papers_kind.py +37 -0
  22. studysystem/db/migrations/versions/0009_past_exam_month_date.py +28 -0
  23. studysystem/db/migrations/versions/0010_task_parts.py +70 -0
  24. studysystem/db/migrations/versions/0011_observation_kind.py +39 -0
  25. studysystem/db/migrations/versions/0012_profile_claim.py +52 -0
  26. studysystem/db/migrations/versions/0013_assumed_instructor_papers.py +40 -0
  27. studysystem/db/migrations/versions/0014_scan_confirmation.py +44 -0
  28. studysystem/db/migrations/versions/0015_practice_item_count.py +37 -0
  29. studysystem/db/migrations/versions/0016_timetable_patterns.py +102 -0
  30. studysystem/db/migrations/versions/0017_course_dropped.py +36 -0
  31. studysystem/db/migrations/versions/0018_syllabus_topics.py +82 -0
  32. studysystem/db/migrations/versions/0019_course_lab_group.py +36 -0
  33. studysystem/db/migrations/versions/0020_chapter_scope.py +61 -0
  34. studysystem/db/migrations/versions/0021_note_retired_at.py +28 -0
  35. studysystem/db/migrations/versions/0022_rescope.py +48 -0
  36. studysystem/db/migrations/versions/0023_backlog_reason.py +42 -0
  37. studysystem/db/migrations/versions/0024_guess_sensitivity.py +35 -0
  38. studysystem/db/migrations/versions/0025_shortfall_inputs.py +83 -0
  39. studysystem/db/migrations/versions/0026_trials_switch.py +36 -0
  40. studysystem/db/tables.py +1096 -0
  41. studysystem/demo.py +722 -0
  42. studysystem/errors.py +39 -0
  43. studysystem/server/__init__.py +83 -0
  44. studysystem/server/__main__.py +5 -0
  45. studysystem/services/__init__.py +1 -0
  46. studysystem/services/adherence.py +147 -0
  47. studysystem/services/assessments.py +171 -0
  48. studysystem/services/capacity.py +164 -0
  49. studysystem/services/claims.py +405 -0
  50. studysystem/services/course_folder.py +82 -0
  51. studysystem/services/courses.py +176 -0
  52. studysystem/services/coverage.py +121 -0
  53. studysystem/services/dashboard_key.py +102 -0
  54. studysystem/services/feedback.py +412 -0
  55. studysystem/services/forward_eval.py +65 -0
  56. studysystem/services/gate.py +77 -0
  57. studysystem/services/generation.py +2864 -0
  58. studysystem/services/ids.py +15 -0
  59. studysystem/services/intake.py +278 -0
  60. studysystem/services/launcher.py +455 -0
  61. studysystem/services/lookups.py +91 -0
  62. studysystem/services/materials.py +97 -0
  63. studysystem/services/note_markdown.py +241 -0
  64. studysystem/services/notes.py +396 -0
  65. studysystem/services/parts.py +65 -0
  66. studysystem/services/past_exams.py +115 -0
  67. studysystem/services/pdf_render.py +148 -0
  68. studysystem/services/pdf_worker.py +184 -0
  69. studysystem/services/plan.py +1300 -0
  70. studysystem/services/private_view.py +38 -0
  71. studysystem/services/profiles.py +544 -0
  72. studysystem/services/programs.py +16 -0
  73. studysystem/services/replacements.py +107 -0
  74. studysystem/services/rescope.py +277 -0
  75. studysystem/services/settings.py +50 -0
  76. studysystem/services/setup.py +235 -0
  77. studysystem/services/shortfall.py +443 -0
  78. studysystem/services/state.py +846 -0
  79. studysystem/services/statements.py +222 -0
  80. studysystem/services/study_loop.py +998 -0
  81. studysystem/services/summaries.py +369 -0
  82. studysystem/services/syllabus.py +138 -0
  83. studysystem/services/system_work.py +113 -0
  84. studysystem/services/taxonomy.py +459 -0
  85. studysystem/services/topics.py +477 -0
  86. studysystem/services/trials.py +201 -0
  87. studysystem/services/unconfirmed.py +95 -0
  88. studysystem/services/units.py +34 -0
  89. studysystem/services/users.py +53 -0
  90. studysystem/services/values.py +168 -0
  91. studysystem/startup.py +147 -0
  92. studysystem/tools/__init__.py +4 -0
  93. studysystem/tools/course_setup.py +185 -0
  94. studysystem/tools/dashboard.py +27 -0
  95. studysystem/tools/feedback.py +66 -0
  96. studysystem/tools/gate.py +24 -0
  97. studysystem/tools/generation.py +156 -0
  98. studysystem/tools/materials.py +32 -0
  99. studysystem/tools/notes.py +29 -0
  100. studysystem/tools/past_exams.py +51 -0
  101. studysystem/tools/ping.py +9 -0
  102. studysystem/tools/plan.py +40 -0
  103. studysystem/tools/profiles.py +94 -0
  104. studysystem/tools/results.py +77 -0
  105. studysystem/tools/study_loop.py +145 -0
  106. studysystem/tools/summaries.py +52 -0
  107. studysystem/tools/topics.py +143 -0
  108. studysystem-1.0.0.dist-info/METADATA +254 -0
  109. studysystem-1.0.0.dist-info/RECORD +112 -0
  110. studysystem-1.0.0.dist-info/WHEEL +4 -0
  111. studysystem-1.0.0.dist-info/entry_points.txt +3 -0
  112. studysystem-1.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,1743 @@
1
+ """the whole schema
2
+
3
+ Revision ID: 0001
4
+ Revises: 0000
5
+ Create Date: 2026-09-22 19:43:29.757522
6
+ """
7
+
8
+ import sqlalchemy as sa
9
+ from alembic import op
10
+
11
+ revision = "0001"
12
+ down_revision = "0000"
13
+ branch_labels = None
14
+ depends_on = None
15
+
16
+
17
+ def upgrade() -> None:
18
+ # ### commands auto generated by Alembic - please adjust! ###
19
+ op.create_table(
20
+ "user",
21
+ sa.Column("id", sa.Text(), nullable=False),
22
+ sa.Column("created_at", sa.Text(), nullable=False),
23
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_user_id_ulid")),
24
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_user")),
25
+ )
26
+ op.create_table(
27
+ "capacity",
28
+ sa.Column("id", sa.Text(), nullable=False),
29
+ sa.Column("user_id", sa.Text(), nullable=False),
30
+ sa.Column("hours_per_week", sa.Float(), nullable=False),
31
+ sa.Column("from_date", sa.Text(), nullable=False),
32
+ sa.Column("created_at", sa.Text(), nullable=False),
33
+ sa.CheckConstraint(
34
+ "hours_per_week > 0 AND hours_per_week <= 112", name=op.f("ck_capacity_hours_range")
35
+ ),
36
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_capacity_id_ulid")),
37
+ sa.ForeignKeyConstraint(
38
+ ["user_id"], ["user.id"], name=op.f("fk_capacity_user_id"), ondelete="RESTRICT"
39
+ ),
40
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_capacity")),
41
+ )
42
+ with op.batch_alter_table("capacity", schema=None) as batch_op:
43
+ batch_op.create_index(batch_op.f("ix_capacity_user_id"), ["user_id"], unique=False)
44
+
45
+ op.create_table(
46
+ "course",
47
+ sa.Column("id", sa.Text(), nullable=False),
48
+ sa.Column("user_id", sa.Text(), nullable=False),
49
+ sa.Column("code", sa.Text(), nullable=False),
50
+ sa.Column("name", sa.Text(), nullable=False),
51
+ sa.Column("instructor", sa.Text(), nullable=True),
52
+ sa.Column("instructor_tier", sa.Text(), nullable=False),
53
+ sa.Column("credits", sa.Float(), nullable=True),
54
+ sa.Column("credits_tier", sa.Text(), nullable=False),
55
+ sa.Column("target_grade", sa.Float(), nullable=True),
56
+ sa.Column("strategy", sa.Text(), server_default=sa.text("'default'"), nullable=False),
57
+ sa.Column("strategy_changed_at", sa.Text(), nullable=True),
58
+ sa.Column("conceded", sa.Integer(), server_default=sa.text("0"), nullable=False),
59
+ sa.Column("conceded_at", sa.Text(), nullable=True),
60
+ sa.Column("source_course_id", sa.Text(), nullable=True),
61
+ sa.Column("semester_name", sa.Text(), nullable=False),
62
+ sa.Column("semester_start", sa.Text(), nullable=True),
63
+ sa.Column("semester_end", sa.Text(), nullable=True),
64
+ sa.Column("semester_end_approx", sa.Integer(), server_default=sa.text("0"), nullable=False),
65
+ sa.Column("created_at", sa.Text(), nullable=False),
66
+ sa.CheckConstraint(
67
+ "((credits_tier = 'unknown') AND (credits is NULL)) OR (NOT (credits_tier = 'unknown') AND NOT(credits is NULL))",
68
+ name=op.f("ck_course_credits_tier_rule"),
69
+ ),
70
+ sa.CheckConstraint(
71
+ "((instructor_tier = 'unknown') AND (instructor is NULL)) OR (NOT (instructor_tier = 'unknown') AND NOT(instructor is NULL))",
72
+ name=op.f("ck_course_instructor_tier_rule"),
73
+ ),
74
+ sa.CheckConstraint(
75
+ "credits_tier IN ('declared', 'inferred', 'unknown')",
76
+ name=op.f("ck_course_credits_tier_enum"),
77
+ ),
78
+ sa.CheckConstraint(
79
+ "instructor_tier IN ('declared', 'inferred', 'unknown')",
80
+ name=op.f("ck_course_instructor_tier_enum"),
81
+ ),
82
+ sa.CheckConstraint(
83
+ "strategy IN ('default', 'catch-up')", name=op.f("ck_course_strategy_enum")
84
+ ),
85
+ sa.CheckConstraint(
86
+ "NOT (conceded = 1) OR (conceded_at IS NOT NULL)",
87
+ name=op.f("ck_course_conceded_at_set"),
88
+ ),
89
+ sa.CheckConstraint("conceded IN (0,1)", name=op.f("ck_course_conceded_bool")),
90
+ sa.CheckConstraint("credits > 0", name=op.f("ck_course_credits_range")),
91
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_course_id_ulid")),
92
+ sa.CheckConstraint("semester_end > semester_start", name=op.f("ck_course_semester_order")),
93
+ sa.CheckConstraint(
94
+ "semester_end_approx IN (0,1)", name=op.f("ck_course_semester_end_approx_bool")
95
+ ),
96
+ sa.CheckConstraint("source_course_id IS NULL", name=op.f("ck_course_source_course_v1")),
97
+ sa.CheckConstraint(
98
+ "target_grade >= 0 AND target_grade <= 100", name=op.f("ck_course_target_grade_range")
99
+ ),
100
+ sa.ForeignKeyConstraint(
101
+ ["user_id"], ["user.id"], name=op.f("fk_course_user_id"), ondelete="RESTRICT"
102
+ ),
103
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_course")),
104
+ )
105
+ with op.batch_alter_table("course", schema=None) as batch_op:
106
+ batch_op.create_index(batch_op.f("ix_course_user_id"), ["user_id"], unique=False)
107
+
108
+ op.create_table(
109
+ "assessment_slot",
110
+ sa.Column("id", sa.Text(), nullable=False),
111
+ sa.Column("owner_id", sa.Text(), nullable=False),
112
+ sa.Column("course_id", sa.Text(), nullable=False),
113
+ sa.Column("name", sa.Text(), nullable=False),
114
+ sa.Column("kind", sa.Text(), nullable=False),
115
+ sa.Column("created_at", sa.Text(), nullable=False),
116
+ sa.CheckConstraint(
117
+ "kind IN ('exam', 'project', 'lab')", name=op.f("ck_assessment_slot_kind_enum")
118
+ ),
119
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_assessment_slot_id_ulid")),
120
+ sa.ForeignKeyConstraint(
121
+ ["course_id"],
122
+ ["course.id"],
123
+ name=op.f("fk_assessment_slot_course_id"),
124
+ ondelete="RESTRICT",
125
+ ),
126
+ sa.ForeignKeyConstraint(
127
+ ["owner_id"], ["user.id"], name=op.f("fk_assessment_slot_owner_id"), ondelete="RESTRICT"
128
+ ),
129
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_assessment_slot")),
130
+ )
131
+ with op.batch_alter_table("assessment_slot", schema=None) as batch_op:
132
+ batch_op.create_index(batch_op.f("ix_assessment_slot_owner_id"), ["owner_id"], unique=False)
133
+
134
+ op.create_table(
135
+ "coverage_reading",
136
+ sa.Column("id", sa.Text(), nullable=False),
137
+ sa.Column("user_id", sa.Text(), nullable=False),
138
+ sa.Column("course_id", sa.Text(), nullable=False),
139
+ sa.Column("value", sa.Float(), nullable=False),
140
+ sa.Column("denominator_tier", sa.Text(), nullable=False),
141
+ sa.Column("tier_note", sa.Text(), nullable=False),
142
+ sa.Column("computed_at", sa.Text(), nullable=False),
143
+ sa.CheckConstraint(
144
+ "denominator_tier IN ('weighted', 'inferred', 'unweighted')",
145
+ name=op.f("ck_coverage_reading_denominator_tier_enum"),
146
+ ),
147
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_coverage_reading_id_ulid")),
148
+ sa.CheckConstraint(
149
+ "value >= 0 AND value <= 1", name=op.f("ck_coverage_reading_value_range")
150
+ ),
151
+ sa.ForeignKeyConstraint(
152
+ ["course_id"],
153
+ ["course.id"],
154
+ name=op.f("fk_coverage_reading_course_id"),
155
+ ondelete="RESTRICT",
156
+ ),
157
+ sa.ForeignKeyConstraint(
158
+ ["user_id"], ["user.id"], name=op.f("fk_coverage_reading_user_id"), ondelete="RESTRICT"
159
+ ),
160
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_coverage_reading")),
161
+ )
162
+ with op.batch_alter_table("coverage_reading", schema=None) as batch_op:
163
+ batch_op.create_index(batch_op.f("ix_coverage_reading_user_id"), ["user_id"], unique=False)
164
+
165
+ op.create_table(
166
+ "material",
167
+ sa.Column("id", sa.Text(), nullable=False),
168
+ sa.Column("user_id", sa.Text(), nullable=False),
169
+ sa.Column("course_id", sa.Text(), nullable=False),
170
+ sa.Column("filename", sa.Text(), nullable=False),
171
+ sa.Column("file_ref", sa.Text(), nullable=False),
172
+ sa.Column("content_sha256", sa.Text(), nullable=False),
173
+ sa.Column("media_type", sa.Text(), nullable=False),
174
+ sa.Column("has_text_layer", sa.Integer(), server_default=sa.text("0"), nullable=False),
175
+ sa.Column("kind", sa.Text(), nullable=False),
176
+ sa.Column("added_at", sa.Text(), nullable=False),
177
+ sa.CheckConstraint(
178
+ "kind IN ('slides', 'chapter', 'lecture-notes', 'textbook', 'exercises', 'syllabus', 'other')",
179
+ name=op.f("ck_material_kind_enum"),
180
+ ),
181
+ sa.CheckConstraint("has_text_layer IN (0,1)", name=op.f("ck_material_has_text_layer_bool")),
182
+ sa.CheckConstraint("length(content_sha256) = 64", name=op.f("ck_material_sha256_len")),
183
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_material_id_ulid")),
184
+ sa.ForeignKeyConstraint(
185
+ ["course_id"], ["course.id"], name=op.f("fk_material_course_id"), ondelete="RESTRICT"
186
+ ),
187
+ sa.ForeignKeyConstraint(
188
+ ["user_id"], ["user.id"], name=op.f("fk_material_user_id"), ondelete="RESTRICT"
189
+ ),
190
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_material")),
191
+ sa.UniqueConstraint(
192
+ "course_id", "content_sha256", name=op.f("uq_material_course_id_content_sha256")
193
+ ),
194
+ )
195
+ with op.batch_alter_table("material", schema=None) as batch_op:
196
+ batch_op.create_index(batch_op.f("ix_material_user_id"), ["user_id"], unique=False)
197
+
198
+ op.create_table(
199
+ "plan",
200
+ sa.Column("id", sa.Text(), nullable=False),
201
+ sa.Column("user_id", sa.Text(), nullable=False),
202
+ sa.Column("scope", sa.Text(), nullable=False),
203
+ sa.Column("scope_course_id", sa.Text(), nullable=True),
204
+ sa.Column("shown_n", sa.Integer(), nullable=False),
205
+ sa.Column("strategy_snapshot", sa.Text(), nullable=False),
206
+ sa.Column("created_at", sa.Text(), nullable=False),
207
+ sa.CheckConstraint(
208
+ "((scope_course_id IS NOT NULL) AND (scope = 'course')) OR (NOT (scope_course_id IS NOT NULL) AND NOT(scope = 'course'))",
209
+ name=op.f("ck_plan_course_scope_has_course"),
210
+ ),
211
+ sa.CheckConstraint("scope IN ('all-courses', 'course')", name=op.f("ck_plan_scope_enum")),
212
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_plan_id_ulid")),
213
+ sa.CheckConstraint("shown_n >= 1", name=op.f("ck_plan_shown_n_min")),
214
+ sa.ForeignKeyConstraint(
215
+ ["scope_course_id"],
216
+ ["course.id"],
217
+ name=op.f("fk_plan_scope_course_id"),
218
+ ondelete="RESTRICT",
219
+ ),
220
+ sa.ForeignKeyConstraint(
221
+ ["user_id"], ["user.id"], name=op.f("fk_plan_user_id"), ondelete="RESTRICT"
222
+ ),
223
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_plan")),
224
+ )
225
+ with op.batch_alter_table("plan", schema=None) as batch_op:
226
+ batch_op.create_index(batch_op.f("ix_plan_user_id"), ["user_id"], unique=False)
227
+
228
+ op.create_table(
229
+ "study_session",
230
+ sa.Column("id", sa.Text(), nullable=False),
231
+ sa.Column("user_id", sa.Text(), nullable=False),
232
+ sa.Column("course_id", sa.Text(), nullable=False),
233
+ sa.Column("mode", sa.Text(), nullable=False),
234
+ sa.Column("subject_type", sa.Text(), nullable=True),
235
+ sa.Column("subject_id", sa.Text(), nullable=True),
236
+ sa.Column("position", sa.Integer(), nullable=True),
237
+ sa.Column("source", sa.Text(), nullable=False),
238
+ sa.Column("started_at", sa.Text(), nullable=False),
239
+ sa.Column("expires_at", sa.Text(), nullable=False),
240
+ sa.Column("ended_at", sa.Text(), nullable=True),
241
+ sa.CheckConstraint(
242
+ "NOT (position IS NOT NULL) OR (mode = 'exam-walkthrough')",
243
+ name=op.f("ck_study_session_position_walkthrough_only"),
244
+ ),
245
+ sa.CheckConstraint(
246
+ "mode IN ('explain-chapter', 'exam-walkthrough', 'mock', 'review-quiz')",
247
+ name=op.f("ck_study_session_mode_enum"),
248
+ ),
249
+ sa.CheckConstraint(
250
+ "source IN ('system', 'manual')", name=op.f("ck_study_session_source_enum")
251
+ ),
252
+ sa.CheckConstraint(
253
+ "subject_type IN ('topic', 'material', 'past-exam')",
254
+ name=op.f("ck_study_session_subject_type_enum"),
255
+ ),
256
+ sa.CheckConstraint(
257
+ "((subject_id IS NULL) AND (subject_type IS NULL)) OR (NOT (subject_id IS NULL) AND NOT(subject_type IS NULL))",
258
+ name=op.f("ck_study_session_subject_pair"),
259
+ ),
260
+ sa.CheckConstraint(
261
+ "ended_at >= started_at", name=op.f("ck_study_session_ended_after_start")
262
+ ),
263
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_study_session_id_ulid")),
264
+ sa.CheckConstraint("position >= 1", name=op.f("ck_study_session_position_min")),
265
+ sa.ForeignKeyConstraint(
266
+ ["course_id"],
267
+ ["course.id"],
268
+ name=op.f("fk_study_session_course_id"),
269
+ ondelete="RESTRICT",
270
+ ),
271
+ sa.ForeignKeyConstraint(
272
+ ["user_id"], ["user.id"], name=op.f("fk_study_session_user_id"), ondelete="RESTRICT"
273
+ ),
274
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_study_session")),
275
+ )
276
+ with op.batch_alter_table("study_session", schema=None) as batch_op:
277
+ batch_op.create_index(batch_op.f("ix_study_session_user_id"), ["user_id"], unique=False)
278
+
279
+ op.create_table(
280
+ "topic",
281
+ sa.Column("id", sa.Text(), nullable=False),
282
+ sa.Column("user_id", sa.Text(), nullable=False),
283
+ sa.Column("course_id", sa.Text(), nullable=False),
284
+ sa.Column("name", sa.Text(), nullable=False),
285
+ sa.Column("status", sa.Text(), nullable=False),
286
+ sa.Column("superseded_by", sa.Text(), nullable=True),
287
+ sa.Column("proposed_by", sa.Text(), nullable=False),
288
+ sa.Column("status_changed_at", sa.Text(), nullable=False),
289
+ sa.Column("created_at", sa.Text(), nullable=False),
290
+ sa.CheckConstraint(
291
+ "NOT (superseded_by IS NOT NULL) OR (status = 'superseded')",
292
+ name=op.f("ck_topic_superseded_by_status"),
293
+ ),
294
+ sa.CheckConstraint(
295
+ "proposed_by IN ('material', 'profile', 'split')",
296
+ name=op.f("ck_topic_proposed_by_enum"),
297
+ ),
298
+ sa.CheckConstraint(
299
+ "status IN ('proposed', 'active', 'unexamined', 'superseded', 'declined')",
300
+ name=op.f("ck_topic_status_enum"),
301
+ ),
302
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_topic_id_ulid")),
303
+ sa.ForeignKeyConstraint(
304
+ ["course_id"], ["course.id"], name=op.f("fk_topic_course_id"), ondelete="RESTRICT"
305
+ ),
306
+ sa.ForeignKeyConstraint(
307
+ ["superseded_by"],
308
+ ["topic.id"],
309
+ name=op.f("fk_topic_superseded_by"),
310
+ ondelete="RESTRICT",
311
+ ),
312
+ sa.ForeignKeyConstraint(
313
+ ["user_id"], ["user.id"], name=op.f("fk_topic_user_id"), ondelete="RESTRICT"
314
+ ),
315
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_topic")),
316
+ )
317
+ with op.batch_alter_table("topic", schema=None) as batch_op:
318
+ batch_op.create_index(batch_op.f("ix_topic_user_id"), ["user_id"], unique=False)
319
+
320
+ op.create_table(
321
+ "assessment",
322
+ sa.Column("id", sa.Text(), nullable=False),
323
+ sa.Column("user_id", sa.Text(), nullable=False),
324
+ sa.Column("slot_id", sa.Text(), nullable=False),
325
+ sa.Column("weight", sa.Float(), nullable=True),
326
+ sa.Column("weight_tier", sa.Text(), nullable=False),
327
+ sa.Column("date", sa.Text(), nullable=True),
328
+ sa.Column("date_approx", sa.Integer(), server_default=sa.text("0"), nullable=False),
329
+ sa.Column("time", sa.Text(), nullable=True),
330
+ sa.Column("room", sa.Text(), nullable=True),
331
+ sa.Column("session_type", sa.Text(), nullable=True),
332
+ sa.Column("status", sa.Text(), server_default=sa.text("'upcoming'"), nullable=False),
333
+ sa.Column("mark", sa.Float(), nullable=True),
334
+ sa.Column("created_at", sa.Text(), nullable=False),
335
+ sa.CheckConstraint(
336
+ "((mark IS NOT NULL) AND (status = 'graded')) OR (NOT (mark IS NOT NULL) AND NOT(status = 'graded'))",
337
+ name=op.f("ck_assessment_check_graded"),
338
+ ),
339
+ sa.CheckConstraint(
340
+ "((weight_tier = 'unknown') AND (weight is NULL)) OR (NOT (weight_tier = 'unknown') AND NOT(weight is NULL))",
341
+ name=op.f("ck_assessment_weight_tier_rule"),
342
+ ),
343
+ sa.CheckConstraint(
344
+ "session_type IN ('first', 'second')", name=op.f("ck_assessment_session_type_enum")
345
+ ),
346
+ sa.CheckConstraint(
347
+ "status IN ('upcoming', 'sat', 'graded')", name=op.f("ck_assessment_status_enum")
348
+ ),
349
+ sa.CheckConstraint(
350
+ "weight_tier IN ('declared', 'inferred', 'unknown')",
351
+ name=op.f("ck_assessment_weight_tier_enum"),
352
+ ),
353
+ sa.CheckConstraint(
354
+ "NOT (date_approx = 1) OR (date IS NOT NULL)",
355
+ name=op.f("ck_assessment_approx_needs_date"),
356
+ ),
357
+ sa.CheckConstraint("date_approx IN (0,1)", name=op.f("ck_assessment_date_approx_bool")),
358
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_assessment_id_ulid")),
359
+ sa.CheckConstraint(
360
+ "mark >= 0 AND mark <= 100", name=op.f("ck_assessment_mark_range_check")
361
+ ),
362
+ sa.CheckConstraint(
363
+ "weight >= 0 AND weight <= 100", name=op.f("ck_assessment_weight_range")
364
+ ),
365
+ sa.ForeignKeyConstraint(
366
+ ["slot_id"],
367
+ ["assessment_slot.id"],
368
+ name=op.f("fk_assessment_slot_id"),
369
+ ondelete="RESTRICT",
370
+ ),
371
+ sa.ForeignKeyConstraint(
372
+ ["user_id"], ["user.id"], name=op.f("fk_assessment_user_id"), ondelete="RESTRICT"
373
+ ),
374
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_assessment")),
375
+ sa.UniqueConstraint(
376
+ "slot_id", "session_type", name=op.f("uq_assessment_slot_id_session_type")
377
+ ),
378
+ )
379
+ with op.batch_alter_table("assessment", schema=None) as batch_op:
380
+ batch_op.create_index("ix_assessment_user_date", ["user_id", "date"], unique=False)
381
+ batch_op.create_index(batch_op.f("ix_assessment_user_id"), ["user_id"], unique=False)
382
+
383
+ op.create_table(
384
+ "exam_profile",
385
+ sa.Column("id", sa.Text(), nullable=False),
386
+ sa.Column("owner_id", sa.Text(), nullable=False),
387
+ sa.Column("slot_id", sa.Text(), nullable=False),
388
+ sa.Column("version", sa.Integer(), nullable=False),
389
+ sa.Column("derived_at", sa.Text(), nullable=False),
390
+ sa.Column(
391
+ "provisional_syllabus", sa.Integer(), server_default=sa.text("0"), nullable=False
392
+ ),
393
+ sa.Column(
394
+ "provisional_instructor", sa.Integer(), server_default=sa.text("0"), nullable=False
395
+ ),
396
+ sa.Column("provisional_format", sa.Integer(), server_default=sa.text("0"), nullable=False),
397
+ sa.Column(
398
+ "recency_weight_newest", sa.Integer(), server_default=sa.text("3"), nullable=False
399
+ ),
400
+ sa.Column(
401
+ "recency_weight_next_two", sa.Integer(), server_default=sa.text("2"), nullable=False
402
+ ),
403
+ sa.Column(
404
+ "recency_weight_older", sa.Integer(), server_default=sa.text("1"), nullable=False
405
+ ),
406
+ sa.Column("evidence_count_syllabus", sa.Integer(), nullable=False),
407
+ sa.Column("evidence_count_instructor", sa.Integer(), nullable=False),
408
+ sa.Column("evidence_count_format", sa.Integer(), nullable=False),
409
+ sa.CheckConstraint(
410
+ "evidence_count_format >= 0", name=op.f("ck_exam_profile_count_format_min")
411
+ ),
412
+ sa.CheckConstraint(
413
+ "evidence_count_instructor >= 0", name=op.f("ck_exam_profile_count_instructor_min")
414
+ ),
415
+ sa.CheckConstraint(
416
+ "evidence_count_syllabus >= 0", name=op.f("ck_exam_profile_count_syllabus_min")
417
+ ),
418
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_exam_profile_id_ulid")),
419
+ sa.CheckConstraint(
420
+ "provisional_format IN (0,1)", name=op.f("ck_exam_profile_provisional_format_bool")
421
+ ),
422
+ sa.CheckConstraint(
423
+ "provisional_instructor IN (0,1)",
424
+ name=op.f("ck_exam_profile_provisional_instructor_bool"),
425
+ ),
426
+ sa.CheckConstraint(
427
+ "provisional_syllabus IN (0,1)", name=op.f("ck_exam_profile_provisional_syllabus_bool")
428
+ ),
429
+ sa.CheckConstraint("version >= 1", name=op.f("ck_exam_profile_version_min")),
430
+ sa.ForeignKeyConstraint(
431
+ ["owner_id"], ["user.id"], name=op.f("fk_exam_profile_owner_id"), ondelete="RESTRICT"
432
+ ),
433
+ sa.ForeignKeyConstraint(
434
+ ["slot_id"],
435
+ ["assessment_slot.id"],
436
+ name=op.f("fk_exam_profile_slot_id"),
437
+ ondelete="RESTRICT",
438
+ ),
439
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_exam_profile")),
440
+ sa.UniqueConstraint("slot_id", "version", name=op.f("uq_exam_profile_slot_id_version")),
441
+ )
442
+ with op.batch_alter_table("exam_profile", schema=None) as batch_op:
443
+ batch_op.create_index(batch_op.f("ix_exam_profile_owner_id"), ["owner_id"], unique=False)
444
+
445
+ op.create_table(
446
+ "hours_entry",
447
+ sa.Column("id", sa.Text(), nullable=False),
448
+ sa.Column("user_id", sa.Text(), nullable=False),
449
+ sa.Column("course_id", sa.Text(), nullable=False),
450
+ sa.Column("session_id", sa.Text(), nullable=True),
451
+ sa.Column("topic_id", sa.Text(), nullable=True),
452
+ sa.Column("setup_key", sa.Text(), nullable=True),
453
+ sa.Column("minutes", sa.Integer(), nullable=False),
454
+ sa.Column("occurred_at", sa.Text(), nullable=False),
455
+ sa.Column("source", sa.Text(), nullable=False),
456
+ sa.Column("deviation_reason", sa.Text(), nullable=True),
457
+ sa.Column("voided_at", sa.Text(), nullable=True),
458
+ sa.Column("created_at", sa.Text(), nullable=False),
459
+ sa.CheckConstraint(
460
+ "deviation_reason IN ('external-deadline', 'disagreed', 'too-tired', 'topic-wrong', 'unexplained')",
461
+ name=op.f("ck_hours_entry_deviation_reason_enum"),
462
+ ),
463
+ sa.CheckConstraint(
464
+ "source IN ('tool', 'timer', 'sweep')", name=op.f("ck_hours_entry_source_enum")
465
+ ),
466
+ sa.CheckConstraint(
467
+ "NOT (topic_id IS NOT NULL AND setup_key IS NOT NULL)",
468
+ name=op.f("ck_hours_entry_one_scope"),
469
+ ),
470
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_hours_entry_id_ulid")),
471
+ sa.CheckConstraint(
472
+ "minutes >= 1 AND minutes <= 720", name=op.f("ck_hours_entry_minutes_range")
473
+ ),
474
+ sa.ForeignKeyConstraint(
475
+ ["course_id"], ["course.id"], name=op.f("fk_hours_entry_course_id"), ondelete="RESTRICT"
476
+ ),
477
+ sa.ForeignKeyConstraint(
478
+ ["session_id"],
479
+ ["study_session.id"],
480
+ name=op.f("fk_hours_entry_session_id"),
481
+ ondelete="RESTRICT",
482
+ ),
483
+ sa.ForeignKeyConstraint(
484
+ ["topic_id"], ["topic.id"], name=op.f("fk_hours_entry_topic_id"), ondelete="RESTRICT"
485
+ ),
486
+ sa.ForeignKeyConstraint(
487
+ ["user_id"], ["user.id"], name=op.f("fk_hours_entry_user_id"), ondelete="RESTRICT"
488
+ ),
489
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_hours_entry")),
490
+ )
491
+ with op.batch_alter_table("hours_entry", schema=None) as batch_op:
492
+ batch_op.create_index(batch_op.f("ix_hours_entry_user_id"), ["user_id"], unique=False)
493
+ batch_op.create_index(
494
+ "ix_hours_entry_user_occurred", ["user_id", "occurred_at"], unique=False
495
+ )
496
+ batch_op.create_index("ix_hours_entry_user_setup", ["user_id", "setup_key"], unique=False)
497
+
498
+ op.create_table(
499
+ "topic_split",
500
+ sa.Column("id", sa.Text(), nullable=False),
501
+ sa.Column("user_id", sa.Text(), nullable=False),
502
+ sa.Column("parent_topic_id", sa.Text(), nullable=False),
503
+ sa.Column("child_topic_id", sa.Text(), nullable=False),
504
+ sa.Column("created_at", sa.Text(), nullable=False),
505
+ sa.CheckConstraint(
506
+ "child_topic_id <> parent_topic_id", name=op.f("ck_topic_split_not_self")
507
+ ),
508
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_topic_split_id_ulid")),
509
+ sa.ForeignKeyConstraint(
510
+ ["child_topic_id"],
511
+ ["topic.id"],
512
+ name=op.f("fk_topic_split_child_topic_id"),
513
+ ondelete="RESTRICT",
514
+ ),
515
+ sa.ForeignKeyConstraint(
516
+ ["parent_topic_id"],
517
+ ["topic.id"],
518
+ name=op.f("fk_topic_split_parent_topic_id"),
519
+ ondelete="RESTRICT",
520
+ ),
521
+ sa.ForeignKeyConstraint(
522
+ ["user_id"], ["user.id"], name=op.f("fk_topic_split_user_id"), ondelete="RESTRICT"
523
+ ),
524
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_topic_split")),
525
+ sa.UniqueConstraint(
526
+ "parent_topic_id",
527
+ "child_topic_id",
528
+ name=op.f("uq_topic_split_parent_topic_id_child_topic_id"),
529
+ ),
530
+ )
531
+ with op.batch_alter_table("topic_split", schema=None) as batch_op:
532
+ batch_op.create_index(batch_op.f("ix_topic_split_user_id"), ["user_id"], unique=False)
533
+
534
+ op.create_table(
535
+ "topic_state",
536
+ sa.Column("user_id", sa.Text(), nullable=False),
537
+ sa.Column("topic_id", sa.Text(), nullable=False),
538
+ sa.Column("stability", sa.Float(), nullable=True),
539
+ sa.Column("difficulty", sa.Float(), nullable=True),
540
+ sa.Column("due_at", sa.Text(), nullable=True),
541
+ sa.Column("last_reviewed_at", sa.Text(), nullable=True),
542
+ sa.Column("reps", sa.Integer(), nullable=False),
543
+ sa.Column("lapses", sa.Integer(), nullable=False),
544
+ sa.Column("inferred", sa.Integer(), server_default=sa.text("0"), nullable=False),
545
+ sa.Column("updated_at", sa.Text(), nullable=False),
546
+ sa.CheckConstraint("inferred IN (0,1)", name=op.f("ck_topic_state_inferred_bool")),
547
+ sa.CheckConstraint("lapses >= 0", name=op.f("ck_topic_state_lapses_min")),
548
+ sa.CheckConstraint("reps >= 0", name=op.f("ck_topic_state_reps_min")),
549
+ sa.ForeignKeyConstraint(
550
+ ["topic_id"], ["topic.id"], name=op.f("fk_topic_state_topic_id"), ondelete="RESTRICT"
551
+ ),
552
+ sa.ForeignKeyConstraint(
553
+ ["user_id"], ["user.id"], name=op.f("fk_topic_state_user_id"), ondelete="RESTRICT"
554
+ ),
555
+ sa.PrimaryKeyConstraint("user_id", "topic_id", name=op.f("pk_topic_state")),
556
+ )
557
+ with op.batch_alter_table("topic_state", schema=None) as batch_op:
558
+ batch_op.create_index("ix_topic_state_user_due", ["user_id", "due_at"], unique=False)
559
+
560
+ op.create_table(
561
+ "coverage",
562
+ sa.Column("id", sa.Text(), nullable=False),
563
+ sa.Column("user_id", sa.Text(), nullable=False),
564
+ sa.Column("topic_id", sa.Text(), nullable=False),
565
+ sa.Column("assessment_id", sa.Text(), nullable=True),
566
+ sa.Column("material_id", sa.Text(), nullable=True),
567
+ sa.Column("tier", sa.Text(), nullable=False),
568
+ sa.Column("confirmed_at", sa.Text(), nullable=True),
569
+ sa.Column("created_at", sa.Text(), nullable=False),
570
+ sa.CheckConstraint("tier IN ('declared', 'inferred')", name=op.f("ck_coverage_tier_enum")),
571
+ sa.CheckConstraint(
572
+ "CASE WHEN material_id IS NULL THEN 0 ELSE 1 END + CASE WHEN assessment_id IS NULL THEN 0 ELSE 1 END = 1",
573
+ name=op.f("ck_coverage_exactly_one"),
574
+ ),
575
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_coverage_id_ulid")),
576
+ sa.ForeignKeyConstraint(
577
+ ["assessment_id"],
578
+ ["assessment.id"],
579
+ name=op.f("fk_coverage_assessment_id"),
580
+ ondelete="RESTRICT",
581
+ ),
582
+ sa.ForeignKeyConstraint(
583
+ ["material_id"],
584
+ ["material.id"],
585
+ name=op.f("fk_coverage_material_id"),
586
+ ondelete="RESTRICT",
587
+ ),
588
+ sa.ForeignKeyConstraint(
589
+ ["topic_id"], ["topic.id"], name=op.f("fk_coverage_topic_id"), ondelete="RESTRICT"
590
+ ),
591
+ sa.ForeignKeyConstraint(
592
+ ["user_id"], ["user.id"], name=op.f("fk_coverage_user_id"), ondelete="RESTRICT"
593
+ ),
594
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_coverage")),
595
+ )
596
+ with op.batch_alter_table("coverage", schema=None) as batch_op:
597
+ batch_op.create_index("ix_coverage_material", ["material_id"], unique=False)
598
+ batch_op.create_index("ix_coverage_topic", ["topic_id"], unique=False)
599
+ batch_op.create_index(
600
+ "ix_coverage_user_confirmed", ["user_id", "confirmed_at"], unique=False
601
+ )
602
+ batch_op.create_index(batch_op.f("ix_coverage_user_id"), ["user_id"], unique=False)
603
+ batch_op.create_index(
604
+ "ux_coverage_assessment",
605
+ ["topic_id", "assessment_id"],
606
+ unique=True,
607
+ sqlite_where=sa.text("assessment_id IS NOT NULL"),
608
+ postgresql_where=sa.text("assessment_id IS NOT NULL"),
609
+ )
610
+ batch_op.create_index(
611
+ "ux_coverage_material",
612
+ ["topic_id", "material_id"],
613
+ unique=True,
614
+ sqlite_where=sa.text("material_id IS NOT NULL"),
615
+ postgresql_where=sa.text("material_id IS NOT NULL"),
616
+ )
617
+
618
+ op.create_table(
619
+ "generation_task",
620
+ sa.Column("id", sa.Text(), nullable=False),
621
+ sa.Column("user_id", sa.Text(), nullable=False),
622
+ sa.Column("kind", sa.Text(), nullable=False),
623
+ sa.Column("scope_type", sa.Text(), nullable=False),
624
+ sa.Column("scope_id", sa.Text(), nullable=False),
625
+ sa.Column("exam_profile_id", sa.Text(), nullable=True),
626
+ sa.Column("route", sa.Text(), nullable=False),
627
+ sa.Column("bytes_sent", sa.Integer(), server_default=sa.text("0"), nullable=False),
628
+ sa.Column("bytes_returned", sa.Integer(), server_default=sa.text("0"), nullable=False),
629
+ sa.Column("status", sa.Text(), server_default=sa.text("'open'"), nullable=False),
630
+ sa.Column("superseded_by", sa.Text(), nullable=True),
631
+ sa.Column("created_at", sa.Text(), nullable=False),
632
+ sa.Column("closed_at", sa.Text(), nullable=True),
633
+ sa.CheckConstraint(
634
+ "((closed_at IS NOT NULL) AND (status <> 'open')) OR (NOT (closed_at IS NOT NULL) AND NOT(status <> 'open'))",
635
+ name=op.f("ck_generation_task_closed_when_not_open"),
636
+ ),
637
+ sa.CheckConstraint(
638
+ "((superseded_by IS NOT NULL) AND (status = 'superseded')) OR (NOT (superseded_by IS NOT NULL) AND NOT(status = 'superseded'))",
639
+ name=op.f("ck_generation_task_superseded_by_status"),
640
+ ),
641
+ sa.CheckConstraint(
642
+ "kind IN ('note', 'practice', 'memory', 'transcription', 'timetable')",
643
+ name=op.f("ck_generation_task_kind_enum"),
644
+ ),
645
+ sa.CheckConstraint(
646
+ "route IN ('host', 'sampling')", name=op.f("ck_generation_task_route_enum")
647
+ ),
648
+ sa.CheckConstraint(
649
+ "scope_type IN ('course', 'material', 'topic', 'past-exam', 'user')",
650
+ name=op.f("ck_generation_task_scope_type_enum"),
651
+ ),
652
+ sa.CheckConstraint(
653
+ "status IN ('open', 'closed', 'partial', 'superseded', 'invalidated')",
654
+ name=op.f("ck_generation_task_status_enum"),
655
+ ),
656
+ sa.CheckConstraint(
657
+ "bytes_returned >= 0", name=op.f("ck_generation_task_bytes_returned_min")
658
+ ),
659
+ sa.CheckConstraint("bytes_sent >= 0", name=op.f("ck_generation_task_bytes_sent_min")),
660
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_generation_task_id_ulid")),
661
+ sa.ForeignKeyConstraint(
662
+ ["exam_profile_id"],
663
+ ["exam_profile.id"],
664
+ name=op.f("fk_generation_task_exam_profile_id"),
665
+ ondelete="RESTRICT",
666
+ ),
667
+ sa.ForeignKeyConstraint(
668
+ ["superseded_by"],
669
+ ["generation_task.id"],
670
+ name=op.f("fk_generation_task_superseded_by"),
671
+ ondelete="RESTRICT",
672
+ ),
673
+ sa.ForeignKeyConstraint(
674
+ ["user_id"], ["user.id"], name=op.f("fk_generation_task_user_id"), ondelete="RESTRICT"
675
+ ),
676
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_generation_task")),
677
+ )
678
+ with op.batch_alter_table("generation_task", schema=None) as batch_op:
679
+ batch_op.create_index(batch_op.f("ix_generation_task_user_id"), ["user_id"], unique=False)
680
+
681
+ op.create_table(
682
+ "plan_item",
683
+ sa.Column("id", sa.Text(), nullable=False),
684
+ sa.Column("user_id", sa.Text(), nullable=False),
685
+ sa.Column("plan_id", sa.Text(), nullable=False),
686
+ sa.Column("lane", sa.Text(), nullable=False),
687
+ sa.Column("course_id", sa.Text(), nullable=False),
688
+ sa.Column("topic_id", sa.Text(), nullable=True),
689
+ sa.Column("setup_kind", sa.Text(), nullable=True),
690
+ sa.Column("setup_key", sa.Text(), nullable=True),
691
+ sa.Column("rank", sa.Integer(), nullable=False),
692
+ sa.Column("shown", sa.Integer(), server_default=sa.text("0"), nullable=False),
693
+ sa.Column("score", sa.Float(), nullable=True),
694
+ sa.Column("reason", sa.Text(), nullable=False),
695
+ sa.Column("estimated_minutes", sa.Integer(), nullable=True),
696
+ sa.Column("marks_unlocked_estimate", sa.Float(), nullable=True),
697
+ sa.Column("exam_profile_id", sa.Text(), nullable=True),
698
+ sa.CheckConstraint(
699
+ "((score IS NOT NULL) AND (lane <> 'setup')) OR (NOT (score IS NOT NULL) AND NOT(lane <> 'setup'))",
700
+ name=op.f("ck_plan_item_score_unless_setup"),
701
+ ),
702
+ sa.CheckConstraint(
703
+ "((setup_key IS NOT NULL) AND (lane = 'setup')) OR (NOT (setup_key IS NOT NULL) AND NOT(lane = 'setup'))",
704
+ name=op.f("ck_plan_item_setup_key_for_setup"),
705
+ ),
706
+ sa.CheckConstraint(
707
+ "((setup_kind IS NOT NULL) AND (lane = 'setup')) OR (NOT (setup_kind IS NOT NULL) AND NOT(lane = 'setup'))",
708
+ name=op.f("ck_plan_item_setup_kind_for_setup"),
709
+ ),
710
+ sa.CheckConstraint(
711
+ "((topic_id IS NOT NULL) AND (lane <> 'setup')) OR (NOT (topic_id IS NOT NULL) AND NOT(lane <> 'setup'))",
712
+ name=op.f("ck_plan_item_topic_unless_setup"),
713
+ ),
714
+ sa.CheckConstraint(
715
+ "NOT (lane = 'setup') OR (estimated_minutes IS NULL)",
716
+ name=op.f("ck_plan_item_setup_has_no_minutes"),
717
+ ),
718
+ sa.CheckConstraint(
719
+ "NOT (marks_unlocked_estimate IS NOT NULL) OR (lane = 'setup')",
720
+ name=op.f("ck_plan_item_marks_unlocked_setup_only"),
721
+ ),
722
+ sa.CheckConstraint(
723
+ "lane IN ('review', 'study', 'setup')", name=op.f("ck_plan_item_lane_enum")
724
+ ),
725
+ sa.CheckConstraint(
726
+ "setup_kind IN ('grading', 'credits', 'exam-date', 'target-grade', 'first-material', 'past-papers')",
727
+ name=op.f("ck_plan_item_setup_kind_enum"),
728
+ ),
729
+ sa.CheckConstraint(
730
+ "estimated_minutes > 0", name=op.f("ck_plan_item_estimated_minutes_min")
731
+ ),
732
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_plan_item_id_ulid")),
733
+ sa.CheckConstraint(
734
+ "marks_unlocked_estimate >= 0", name=op.f("ck_plan_item_marks_unlocked_min")
735
+ ),
736
+ sa.CheckConstraint("rank >= 1", name=op.f("ck_plan_item_rank_min")),
737
+ sa.CheckConstraint("shown IN (0,1)", name=op.f("ck_plan_item_shown_bool")),
738
+ sa.ForeignKeyConstraint(
739
+ ["course_id"], ["course.id"], name=op.f("fk_plan_item_course_id"), ondelete="RESTRICT"
740
+ ),
741
+ sa.ForeignKeyConstraint(
742
+ ["exam_profile_id"],
743
+ ["exam_profile.id"],
744
+ name=op.f("fk_plan_item_exam_profile_id"),
745
+ ondelete="RESTRICT",
746
+ ),
747
+ sa.ForeignKeyConstraint(
748
+ ["plan_id"], ["plan.id"], name=op.f("fk_plan_item_plan_id"), ondelete="RESTRICT"
749
+ ),
750
+ sa.ForeignKeyConstraint(
751
+ ["topic_id"], ["topic.id"], name=op.f("fk_plan_item_topic_id"), ondelete="RESTRICT"
752
+ ),
753
+ sa.ForeignKeyConstraint(
754
+ ["user_id"], ["user.id"], name=op.f("fk_plan_item_user_id"), ondelete="RESTRICT"
755
+ ),
756
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_plan_item")),
757
+ sa.UniqueConstraint("plan_id", "lane", "rank", name=op.f("uq_plan_item_plan_id_lane_rank")),
758
+ )
759
+ with op.batch_alter_table("plan_item", schema=None) as batch_op:
760
+ batch_op.create_index(batch_op.f("ix_plan_item_user_id"), ["user_id"], unique=False)
761
+ batch_op.create_index("ix_plan_item_user_setup", ["user_id", "setup_key"], unique=False)
762
+ batch_op.create_index("ix_plan_item_user_topic", ["user_id", "topic_id"], unique=False)
763
+
764
+ op.create_table(
765
+ "topic_weight",
766
+ sa.Column("id", sa.Text(), nullable=False),
767
+ sa.Column("exam_profile_id", sa.Text(), nullable=False),
768
+ sa.Column("topic_id", sa.Text(), nullable=False),
769
+ sa.Column("weight", sa.Float(), nullable=False),
770
+ sa.Column("created_at", sa.Text(), nullable=False),
771
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_topic_weight_id_ulid")),
772
+ sa.CheckConstraint(
773
+ "weight >= 0 AND weight <= 100", name=op.f("ck_topic_weight_weight_range")
774
+ ),
775
+ sa.ForeignKeyConstraint(
776
+ ["exam_profile_id"],
777
+ ["exam_profile.id"],
778
+ name=op.f("fk_topic_weight_exam_profile_id"),
779
+ ondelete="RESTRICT",
780
+ ),
781
+ sa.ForeignKeyConstraint(
782
+ ["topic_id"], ["topic.id"], name=op.f("fk_topic_weight_topic_id"), ondelete="RESTRICT"
783
+ ),
784
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_topic_weight")),
785
+ sa.UniqueConstraint(
786
+ "exam_profile_id", "topic_id", name=op.f("uq_topic_weight_exam_profile_id_topic_id")
787
+ ),
788
+ )
789
+ op.create_table(
790
+ "capacity_suggestion",
791
+ sa.Column("id", sa.Text(), nullable=False),
792
+ sa.Column("user_id", sa.Text(), nullable=False),
793
+ sa.Column("hours_per_week", sa.Float(), nullable=False),
794
+ sa.Column("source_task_id", sa.Text(), nullable=False),
795
+ sa.Column("status", sa.Text(), server_default=sa.text("'pending'"), nullable=False),
796
+ sa.Column("resolved_at", sa.Text(), nullable=True),
797
+ sa.Column("created_at", sa.Text(), nullable=False),
798
+ sa.CheckConstraint(
799
+ "((resolved_at IS NOT NULL) AND (status <> 'pending')) OR (NOT (resolved_at IS NOT NULL) AND NOT(status <> 'pending'))",
800
+ name=op.f("ck_capacity_suggestion_resolved_when_done"),
801
+ ),
802
+ sa.CheckConstraint(
803
+ "status IN ('pending', 'accepted', 'dismissed')",
804
+ name=op.f("ck_capacity_suggestion_status_enum"),
805
+ ),
806
+ sa.CheckConstraint(
807
+ "hours_per_week > 0", name=op.f("ck_capacity_suggestion_hours_positive")
808
+ ),
809
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_capacity_suggestion_id_ulid")),
810
+ sa.ForeignKeyConstraint(
811
+ ["source_task_id"],
812
+ ["generation_task.id"],
813
+ name=op.f("fk_capacity_suggestion_source_task_id"),
814
+ ondelete="RESTRICT",
815
+ ),
816
+ sa.ForeignKeyConstraint(
817
+ ["user_id"],
818
+ ["user.id"],
819
+ name=op.f("fk_capacity_suggestion_user_id"),
820
+ ondelete="RESTRICT",
821
+ ),
822
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_capacity_suggestion")),
823
+ )
824
+ with op.batch_alter_table("capacity_suggestion", schema=None) as batch_op:
825
+ batch_op.create_index(
826
+ batch_op.f("ix_capacity_suggestion_user_id"), ["user_id"], unique=False
827
+ )
828
+
829
+ op.create_table(
830
+ "class_meeting",
831
+ sa.Column("id", sa.Text(), nullable=False),
832
+ sa.Column("user_id", sa.Text(), nullable=False),
833
+ sa.Column("course_id", sa.Text(), nullable=False),
834
+ sa.Column("weekday", sa.Integer(), nullable=False),
835
+ sa.Column("start_time", sa.Text(), nullable=False),
836
+ sa.Column("end_time", sa.Text(), nullable=False),
837
+ sa.Column("room", sa.Text(), nullable=True),
838
+ sa.Column("kind", sa.Text(), nullable=False),
839
+ sa.Column("tier", sa.Text(), nullable=False),
840
+ sa.Column("source_task_id", sa.Text(), nullable=True),
841
+ sa.Column("created_at", sa.Text(), nullable=False),
842
+ sa.CheckConstraint(
843
+ "kind IN ('lecture', 'lab', 'tutorial')", name=op.f("ck_class_meeting_kind_enum")
844
+ ),
845
+ sa.CheckConstraint(
846
+ "tier IN ('declared', 'inferred')", name=op.f("ck_class_meeting_tier_enum")
847
+ ),
848
+ sa.CheckConstraint("end_time > start_time", name=op.f("ck_class_meeting_time_order")),
849
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_class_meeting_id_ulid")),
850
+ sa.CheckConstraint(
851
+ "weekday >= 1 AND weekday <= 7", name=op.f("ck_class_meeting_weekday_iso")
852
+ ),
853
+ sa.ForeignKeyConstraint(
854
+ ["course_id"],
855
+ ["course.id"],
856
+ name=op.f("fk_class_meeting_course_id"),
857
+ ondelete="RESTRICT",
858
+ ),
859
+ sa.ForeignKeyConstraint(
860
+ ["source_task_id"],
861
+ ["generation_task.id"],
862
+ name=op.f("fk_class_meeting_source_task_id"),
863
+ ondelete="RESTRICT",
864
+ ),
865
+ sa.ForeignKeyConstraint(
866
+ ["user_id"], ["user.id"], name=op.f("fk_class_meeting_user_id"), ondelete="RESTRICT"
867
+ ),
868
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_class_meeting")),
869
+ sa.UniqueConstraint(
870
+ "course_id",
871
+ "weekday",
872
+ "start_time",
873
+ "kind",
874
+ name=op.f("uq_class_meeting_course_id_weekday_start_time_kind"),
875
+ ),
876
+ )
877
+ with op.batch_alter_table("class_meeting", schema=None) as batch_op:
878
+ batch_op.create_index(batch_op.f("ix_class_meeting_user_id"), ["user_id"], unique=False)
879
+
880
+ op.create_table(
881
+ "memory_item",
882
+ sa.Column("id", sa.Text(), nullable=False),
883
+ sa.Column("user_id", sa.Text(), nullable=False),
884
+ sa.Column("topic_id", sa.Text(), nullable=False),
885
+ sa.Column("task_id", sa.Text(), nullable=False),
886
+ sa.Column("item_ordinal", sa.Integer(), nullable=False),
887
+ sa.Column("term", sa.Text(), nullable=False),
888
+ sa.Column("definition", sa.Text(), nullable=False),
889
+ sa.Column("formula", sa.Text(), nullable=True),
890
+ sa.Column("state", sa.Text(), server_default=sa.text("'live'"), nullable=False),
891
+ sa.Column("source_marker", sa.Text(), nullable=False),
892
+ sa.Column("created_at", sa.Text(), nullable=False),
893
+ sa.CheckConstraint(
894
+ "source_marker IN ('from-material', 'model-knowledge')",
895
+ name=op.f("ck_memory_item_source_marker_enum"),
896
+ ),
897
+ sa.CheckConstraint(
898
+ "state IN ('live', 'needs-review', 'rejected', 'superseded')",
899
+ name=op.f("ck_memory_item_state_enum"),
900
+ ),
901
+ sa.CheckConstraint("item_ordinal >= 1", name=op.f("ck_memory_item_item_ordinal_min")),
902
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_memory_item_id_ulid")),
903
+ sa.ForeignKeyConstraint(
904
+ ["task_id"],
905
+ ["generation_task.id"],
906
+ name=op.f("fk_memory_item_task_id"),
907
+ ondelete="RESTRICT",
908
+ ),
909
+ sa.ForeignKeyConstraint(
910
+ ["topic_id"], ["topic.id"], name=op.f("fk_memory_item_topic_id"), ondelete="RESTRICT"
911
+ ),
912
+ sa.ForeignKeyConstraint(
913
+ ["user_id"], ["user.id"], name=op.f("fk_memory_item_user_id"), ondelete="RESTRICT"
914
+ ),
915
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_memory_item")),
916
+ sa.UniqueConstraint(
917
+ "task_id", "item_ordinal", name=op.f("uq_memory_item_task_id_item_ordinal")
918
+ ),
919
+ )
920
+ with op.batch_alter_table("memory_item", schema=None) as batch_op:
921
+ batch_op.create_index(batch_op.f("ix_memory_item_user_id"), ["user_id"], unique=False)
922
+
923
+ op.create_table(
924
+ "note",
925
+ sa.Column("id", sa.Text(), nullable=False),
926
+ sa.Column("user_id", sa.Text(), nullable=False),
927
+ sa.Column("material_id", sa.Text(), nullable=False),
928
+ sa.Column("task_id", sa.Text(), nullable=False),
929
+ sa.Column("section_ordinal", sa.Integer(), server_default=sa.text("1"), nullable=False),
930
+ sa.Column("exam_profile_id", sa.Text(), nullable=True),
931
+ sa.Column("body", sa.Text(), nullable=False),
932
+ sa.Column("exported_path", sa.Text(), nullable=True),
933
+ sa.Column("created_at", sa.Text(), nullable=False),
934
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_note_id_ulid")),
935
+ sa.CheckConstraint("section_ordinal >= 1", name=op.f("ck_note_section_ordinal_min")),
936
+ sa.ForeignKeyConstraint(
937
+ ["exam_profile_id"],
938
+ ["exam_profile.id"],
939
+ name=op.f("fk_note_exam_profile_id"),
940
+ ondelete="RESTRICT",
941
+ ),
942
+ sa.ForeignKeyConstraint(
943
+ ["material_id"], ["material.id"], name=op.f("fk_note_material_id"), ondelete="RESTRICT"
944
+ ),
945
+ sa.ForeignKeyConstraint(
946
+ ["task_id"], ["generation_task.id"], name=op.f("fk_note_task_id"), ondelete="RESTRICT"
947
+ ),
948
+ sa.ForeignKeyConstraint(
949
+ ["user_id"], ["user.id"], name=op.f("fk_note_user_id"), ondelete="RESTRICT"
950
+ ),
951
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_note")),
952
+ sa.UniqueConstraint(
953
+ "task_id", "section_ordinal", name=op.f("uq_note_task_id_section_ordinal")
954
+ ),
955
+ )
956
+ with op.batch_alter_table("note", schema=None) as batch_op:
957
+ batch_op.create_index(batch_op.f("ix_note_user_id"), ["user_id"], unique=False)
958
+
959
+ op.create_table(
960
+ "past_exam",
961
+ sa.Column("id", sa.Text(), nullable=False),
962
+ sa.Column("owner_id", sa.Text(), nullable=False),
963
+ sa.Column("slot_id", sa.Text(), nullable=False),
964
+ sa.Column("session_type", sa.Text(), nullable=False),
965
+ sa.Column("session_date", sa.Text(), nullable=False),
966
+ sa.Column("session_delayed", sa.Integer(), server_default=sa.text("0"), nullable=False),
967
+ sa.Column("instructor", sa.Text(), nullable=True),
968
+ sa.Column("instructor_tier", sa.Text(), nullable=False),
969
+ sa.Column("has_text_layer", sa.Integer(), server_default=sa.text("0"), nullable=False),
970
+ sa.Column("file_ref", sa.Text(), nullable=False),
971
+ sa.Column("content_sha256", sa.Text(), nullable=False),
972
+ sa.Column("transcript_task_id", sa.Text(), nullable=True),
973
+ sa.Column("created_at", sa.Text(), nullable=False),
974
+ sa.CheckConstraint(
975
+ "((instructor_tier = 'unknown') AND (instructor is NULL)) OR (NOT (instructor_tier = 'unknown') AND NOT(instructor is NULL))",
976
+ name=op.f("ck_past_exam_instructor_tier_rule"),
977
+ ),
978
+ sa.CheckConstraint(
979
+ "instructor_tier IN ('declared', 'inferred', 'unknown')",
980
+ name=op.f("ck_past_exam_instructor_tier_enum"),
981
+ ),
982
+ sa.CheckConstraint(
983
+ "session_type IN ('first', 'second')", name=op.f("ck_past_exam_session_type_enum")
984
+ ),
985
+ sa.CheckConstraint(
986
+ "has_text_layer IN (0,1)", name=op.f("ck_past_exam_has_text_layer_bool")
987
+ ),
988
+ sa.CheckConstraint("length(content_sha256) = 64", name=op.f("ck_past_exam_sha256_len")),
989
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_past_exam_id_ulid")),
990
+ sa.CheckConstraint(
991
+ "session_delayed IN (0,1)", name=op.f("ck_past_exam_session_delayed_bool")
992
+ ),
993
+ sa.ForeignKeyConstraint(
994
+ ["owner_id"], ["user.id"], name=op.f("fk_past_exam_owner_id"), ondelete="RESTRICT"
995
+ ),
996
+ sa.ForeignKeyConstraint(
997
+ ["slot_id"],
998
+ ["assessment_slot.id"],
999
+ name=op.f("fk_past_exam_slot_id"),
1000
+ ondelete="RESTRICT",
1001
+ ),
1002
+ sa.ForeignKeyConstraint(
1003
+ ["transcript_task_id"],
1004
+ ["generation_task.id"],
1005
+ name=op.f("fk_past_exam_transcript_task_id"),
1006
+ ondelete="RESTRICT",
1007
+ ),
1008
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_past_exam")),
1009
+ sa.UniqueConstraint(
1010
+ "slot_id", "content_sha256", name=op.f("uq_past_exam_slot_id_content_sha256")
1011
+ ),
1012
+ )
1013
+ with op.batch_alter_table("past_exam", schema=None) as batch_op:
1014
+ batch_op.create_index(batch_op.f("ix_past_exam_owner_id"), ["owner_id"], unique=False)
1015
+ batch_op.create_index("ix_past_exam_slot_date", ["slot_id", "session_date"], unique=False)
1016
+
1017
+ op.create_table(
1018
+ "submission",
1019
+ sa.Column("id", sa.Text(), nullable=False),
1020
+ sa.Column("user_id", sa.Text(), nullable=False),
1021
+ sa.Column("task_id", sa.Text(), nullable=False),
1022
+ sa.Column("ordinal", sa.Integer(), nullable=False),
1023
+ sa.Column("payload", sa.Text(), nullable=False),
1024
+ sa.Column("validation_result", sa.Text(), nullable=False),
1025
+ sa.Column("accepted_count", sa.Integer(), nullable=False),
1026
+ sa.Column("rejected_count", sa.Integer(), nullable=False),
1027
+ sa.Column("bytes_sent", sa.Integer(), nullable=False),
1028
+ sa.Column("bytes_returned", sa.Integer(), nullable=False),
1029
+ sa.Column("created_at", sa.Text(), nullable=False),
1030
+ sa.CheckConstraint("accepted_count >= 0", name=op.f("ck_submission_accepted_min")),
1031
+ sa.CheckConstraint("bytes_returned >= 0", name=op.f("ck_submission_bytes_returned_min")),
1032
+ sa.CheckConstraint("bytes_sent >= 0", name=op.f("ck_submission_bytes_sent_min")),
1033
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_submission_id_ulid")),
1034
+ sa.CheckConstraint(
1035
+ "ordinal >= 1 AND ordinal <= 3", name=op.f("ck_submission_ordinal_range")
1036
+ ),
1037
+ sa.CheckConstraint("rejected_count >= 0", name=op.f("ck_submission_rejected_min")),
1038
+ sa.ForeignKeyConstraint(
1039
+ ["task_id"],
1040
+ ["generation_task.id"],
1041
+ name=op.f("fk_submission_task_id"),
1042
+ ondelete="RESTRICT",
1043
+ ),
1044
+ sa.ForeignKeyConstraint(
1045
+ ["user_id"], ["user.id"], name=op.f("fk_submission_user_id"), ondelete="RESTRICT"
1046
+ ),
1047
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_submission")),
1048
+ sa.UniqueConstraint("task_id", "ordinal", name=op.f("uq_submission_task_id_ordinal")),
1049
+ )
1050
+ with op.batch_alter_table("submission", schema=None) as batch_op:
1051
+ batch_op.create_index(batch_op.f("ix_submission_user_id"), ["user_id"], unique=False)
1052
+
1053
+ op.create_table(
1054
+ "observation",
1055
+ sa.Column("id", sa.Text(), nullable=False),
1056
+ sa.Column("owner_id", sa.Text(), nullable=False),
1057
+ sa.Column("slot_id", sa.Text(), nullable=False),
1058
+ sa.Column("past_exam_id", sa.Text(), nullable=True),
1059
+ sa.Column("claim_key", sa.Text(), nullable=False),
1060
+ sa.Column("claim_class", sa.Text(), nullable=False),
1061
+ sa.Column("topic_id", sa.Text(), nullable=True),
1062
+ sa.Column("claim_value", sa.Text(), nullable=False),
1063
+ sa.Column("study_tier", sa.Text(), nullable=False),
1064
+ sa.Column("citation", sa.Text(), nullable=True),
1065
+ sa.Column("created_at", sa.Text(), nullable=False),
1066
+ sa.CheckConstraint(
1067
+ "((topic_id IS NOT NULL) AND (claim_key = 'topic-marks')) OR (NOT (topic_id IS NOT NULL) AND NOT(claim_key = 'topic-marks'))",
1068
+ name=op.f("ck_observation_topic_for_marks"),
1069
+ ),
1070
+ sa.CheckConstraint(
1071
+ "(claim_key IN ('topic-marks') AND claim_class = 'syllabus-driven') OR (claim_key IN ('duration-minutes', 'total-marks', 'question-count', 'has-mcq') AND claim_class = 'format') OR (claim_key IN ('archetype', 'phrasing', 'trap') AND claim_class = 'instructor-driven')",
1072
+ name=op.f("ck_observation_claim_class_by_key"),
1073
+ ),
1074
+ sa.CheckConstraint(
1075
+ "claim_class IN ('syllabus-driven', 'format', 'instructor-driven')",
1076
+ name=op.f("ck_observation_claim_class_enum"),
1077
+ ),
1078
+ sa.CheckConstraint(
1079
+ "claim_key IN ('topic-marks', 'duration-minutes', 'total-marks', 'question-count', 'has-mcq', 'archetype', 'phrasing', 'trap')",
1080
+ name=op.f("ck_observation_claim_key_enum"),
1081
+ ),
1082
+ sa.CheckConstraint(
1083
+ "study_tier IN ('E1', 'E2', 'E3')", name=op.f("ck_observation_study_tier_enum")
1084
+ ),
1085
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_observation_id_ulid")),
1086
+ sa.ForeignKeyConstraint(
1087
+ ["owner_id"], ["user.id"], name=op.f("fk_observation_owner_id"), ondelete="RESTRICT"
1088
+ ),
1089
+ sa.ForeignKeyConstraint(
1090
+ ["past_exam_id"],
1091
+ ["past_exam.id"],
1092
+ name=op.f("fk_observation_past_exam_id"),
1093
+ ondelete="RESTRICT",
1094
+ ),
1095
+ sa.ForeignKeyConstraint(
1096
+ ["slot_id"],
1097
+ ["assessment_slot.id"],
1098
+ name=op.f("fk_observation_slot_id"),
1099
+ ondelete="RESTRICT",
1100
+ ),
1101
+ sa.ForeignKeyConstraint(
1102
+ ["topic_id"], ["topic.id"], name=op.f("fk_observation_topic_id"), ondelete="RESTRICT"
1103
+ ),
1104
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_observation")),
1105
+ )
1106
+ with op.batch_alter_table("observation", schema=None) as batch_op:
1107
+ batch_op.create_index(batch_op.f("ix_observation_owner_id"), ["owner_id"], unique=False)
1108
+
1109
+ op.create_table(
1110
+ "practice_item",
1111
+ sa.Column("id", sa.Text(), nullable=False),
1112
+ sa.Column("user_id", sa.Text(), nullable=False),
1113
+ sa.Column("topic_id", sa.Text(), nullable=False),
1114
+ sa.Column("task_id", sa.Text(), nullable=False),
1115
+ sa.Column("item_ordinal", sa.Integer(), nullable=False),
1116
+ sa.Column("origin", sa.Text(), nullable=False),
1117
+ sa.Column("past_exam_id", sa.Text(), nullable=True),
1118
+ sa.Column("position", sa.Integer(), nullable=True),
1119
+ sa.Column("question", sa.Text(), nullable=False),
1120
+ sa.Column("answer_key", sa.Text(), nullable=True),
1121
+ sa.Column("answer_provenance", sa.Text(), nullable=False),
1122
+ sa.Column("marks", sa.Float(), nullable=True),
1123
+ sa.Column("state", sa.Text(), server_default=sa.text("'live'"), nullable=False),
1124
+ sa.Column("source_marker", sa.Text(), nullable=False),
1125
+ sa.Column("novelty_score", sa.Float(), nullable=True),
1126
+ sa.Column("nearest_item_id", sa.Text(), nullable=True),
1127
+ sa.Column("created_at", sa.Text(), nullable=False),
1128
+ sa.CheckConstraint(
1129
+ "((answer_provenance = 'none') AND (answer_key IS NULL)) OR (NOT (answer_provenance = 'none') AND NOT(answer_key IS NULL))",
1130
+ name=op.f("ck_practice_item_answer_exists"),
1131
+ ),
1132
+ sa.CheckConstraint(
1133
+ "((novelty_score IS NOT NULL) AND (origin = 'generated')) OR (NOT (novelty_score IS NOT NULL) AND NOT(origin = 'generated'))",
1134
+ name=op.f("ck_practice_item_novelty_generated"),
1135
+ ),
1136
+ sa.CheckConstraint(
1137
+ "((past_exam_id IS NOT NULL) AND (origin = 'past-exam')) OR (NOT (past_exam_id IS NOT NULL) AND NOT(origin = 'past-exam'))",
1138
+ name=op.f("ck_practice_item_past_exam_origin"),
1139
+ ),
1140
+ sa.CheckConstraint(
1141
+ "NOT (origin = 'past-exam') OR (source_marker = 'from-material')",
1142
+ name=op.f("ck_practice_item_past_exam_marker"),
1143
+ ),
1144
+ sa.CheckConstraint(
1145
+ "answer_provenance IN ('verified-source', 'unverified', 'none')",
1146
+ name=op.f("ck_practice_item_answer_provenance_enum"),
1147
+ ),
1148
+ sa.CheckConstraint(
1149
+ "origin IN ('generated', 'past-exam', 'textbook', 'instructor-set')",
1150
+ name=op.f("ck_practice_item_origin_enum"),
1151
+ ),
1152
+ sa.CheckConstraint(
1153
+ "source_marker IN ('from-material', 'model-knowledge')",
1154
+ name=op.f("ck_practice_item_source_marker_enum"),
1155
+ ),
1156
+ sa.CheckConstraint(
1157
+ "state IN ('live', 'needs-review', 'rejected', 'superseded')",
1158
+ name=op.f("ck_practice_item_state_enum"),
1159
+ ),
1160
+ sa.CheckConstraint(
1161
+ "((position IS NOT NULL) AND (past_exam_id IS NOT NULL)) OR (NOT (position IS NOT NULL) AND NOT(past_exam_id IS NOT NULL))",
1162
+ name=op.f("ck_practice_item_position_past_exam"),
1163
+ ),
1164
+ sa.CheckConstraint("item_ordinal >= 1", name=op.f("ck_practice_item_item_ordinal_min")),
1165
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_practice_item_id_ulid")),
1166
+ sa.CheckConstraint("marks >= 0", name=op.f("ck_practice_item_marks_min")),
1167
+ sa.CheckConstraint(
1168
+ "novelty_score >= 0 AND novelty_score <= 1", name=op.f("ck_practice_item_novelty_range")
1169
+ ),
1170
+ sa.CheckConstraint("position >= 1", name=op.f("ck_practice_item_position_min")),
1171
+ sa.ForeignKeyConstraint(
1172
+ ["nearest_item_id"],
1173
+ ["practice_item.id"],
1174
+ name=op.f("fk_practice_item_nearest_item_id"),
1175
+ ondelete="RESTRICT",
1176
+ ),
1177
+ sa.ForeignKeyConstraint(
1178
+ ["past_exam_id"],
1179
+ ["past_exam.id"],
1180
+ name=op.f("fk_practice_item_past_exam_id"),
1181
+ ondelete="RESTRICT",
1182
+ ),
1183
+ sa.ForeignKeyConstraint(
1184
+ ["task_id"],
1185
+ ["generation_task.id"],
1186
+ name=op.f("fk_practice_item_task_id"),
1187
+ ondelete="RESTRICT",
1188
+ ),
1189
+ sa.ForeignKeyConstraint(
1190
+ ["topic_id"], ["topic.id"], name=op.f("fk_practice_item_topic_id"), ondelete="RESTRICT"
1191
+ ),
1192
+ sa.ForeignKeyConstraint(
1193
+ ["user_id"], ["user.id"], name=op.f("fk_practice_item_user_id"), ondelete="RESTRICT"
1194
+ ),
1195
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_practice_item")),
1196
+ sa.UniqueConstraint(
1197
+ "task_id", "item_ordinal", name=op.f("uq_practice_item_task_id_item_ordinal")
1198
+ ),
1199
+ )
1200
+ with op.batch_alter_table("practice_item", schema=None) as batch_op:
1201
+ batch_op.create_index("ix_practice_item_origin_topic", ["origin", "topic_id"], unique=False)
1202
+ batch_op.create_index("ix_practice_item_topic_state", ["topic_id", "state"], unique=False)
1203
+ batch_op.create_index(batch_op.f("ix_practice_item_user_id"), ["user_id"], unique=False)
1204
+ batch_op.create_index(
1205
+ "ux_practice_item_position",
1206
+ ["past_exam_id", "position"],
1207
+ unique=True,
1208
+ sqlite_where=sa.text("past_exam_id IS NOT NULL"),
1209
+ postgresql_where=sa.text("past_exam_id IS NOT NULL"),
1210
+ )
1211
+
1212
+ op.create_table(
1213
+ "attempt",
1214
+ sa.Column("id", sa.Text(), nullable=False),
1215
+ sa.Column("user_id", sa.Text(), nullable=False),
1216
+ sa.Column("session_id", sa.Text(), nullable=True),
1217
+ sa.Column("practice_item_id", sa.Text(), nullable=True),
1218
+ sa.Column("topic_id", sa.Text(), nullable=False),
1219
+ sa.Column("source", sa.Text(), nullable=False),
1220
+ sa.Column("assisted", sa.Integer(), server_default=sa.text("0"), nullable=False),
1221
+ sa.Column("correct", sa.Integer(), nullable=True),
1222
+ sa.Column("score", sa.Float(), nullable=True),
1223
+ sa.Column("root_cause", sa.Text(), nullable=True),
1224
+ sa.Column("fix_rule", sa.Text(), nullable=True),
1225
+ sa.Column("correctness_voided", sa.Integer(), server_default=sa.text("0"), nullable=False),
1226
+ sa.Column("created_at", sa.Text(), nullable=False),
1227
+ sa.CheckConstraint(
1228
+ "NOT (session_id IS NULL) OR (source IN ('self-reported', 'exam-retro'))",
1229
+ name=op.f("ck_attempt_no_session_only_offline"),
1230
+ ),
1231
+ sa.CheckConstraint(
1232
+ "NOT (source = 'exam-walkthrough') OR (assisted = 1)",
1233
+ name=op.f("ck_attempt_walkthrough_is_assisted"),
1234
+ ),
1235
+ sa.CheckConstraint(
1236
+ "root_cause IN ('concept', 'recall', 'procedure', 'misread', 'careless', 'time-pressure')",
1237
+ name=op.f("ck_attempt_root_cause_enum"),
1238
+ ),
1239
+ sa.CheckConstraint(
1240
+ "source IN ('comprehension-check', 'practice', 'review-quiz', 'exam-walkthrough', 'exam-retro', 'self-reported')",
1241
+ name=op.f("ck_attempt_source_enum"),
1242
+ ),
1243
+ sa.CheckConstraint("assisted IN (0,1)", name=op.f("ck_attempt_assisted_bool")),
1244
+ sa.CheckConstraint("correct IN (0,1)", name=op.f("ck_attempt_correct_bool")),
1245
+ sa.CheckConstraint(
1246
+ "correct IS NOT NULL OR score IS NOT NULL", name=op.f("ck_attempt_some_result")
1247
+ ),
1248
+ sa.CheckConstraint(
1249
+ "correctness_voided IN (0,1)", name=op.f("ck_attempt_correctness_voided_bool")
1250
+ ),
1251
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_attempt_id_ulid")),
1252
+ sa.CheckConstraint("score >= 0 AND score <= 1", name=op.f("ck_attempt_score_range")),
1253
+ sa.ForeignKeyConstraint(
1254
+ ["practice_item_id"],
1255
+ ["practice_item.id"],
1256
+ name=op.f("fk_attempt_practice_item_id"),
1257
+ ondelete="RESTRICT",
1258
+ ),
1259
+ sa.ForeignKeyConstraint(
1260
+ ["session_id"],
1261
+ ["study_session.id"],
1262
+ name=op.f("fk_attempt_session_id"),
1263
+ ondelete="RESTRICT",
1264
+ ),
1265
+ sa.ForeignKeyConstraint(
1266
+ ["topic_id"], ["topic.id"], name=op.f("fk_attempt_topic_id"), ondelete="RESTRICT"
1267
+ ),
1268
+ sa.ForeignKeyConstraint(
1269
+ ["user_id"], ["user.id"], name=op.f("fk_attempt_user_id"), ondelete="RESTRICT"
1270
+ ),
1271
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_attempt")),
1272
+ )
1273
+ with op.batch_alter_table("attempt", schema=None) as batch_op:
1274
+ batch_op.create_index(batch_op.f("ix_attempt_user_id"), ["user_id"], unique=False)
1275
+ batch_op.create_index("ix_attempt_user_topic", ["user_id", "topic_id"], unique=False)
1276
+
1277
+ op.create_table(
1278
+ "discrimination_trial",
1279
+ sa.Column("id", sa.Text(), nullable=False),
1280
+ sa.Column("user_id", sa.Text(), nullable=False),
1281
+ sa.Column("session_id", sa.Text(), nullable=False),
1282
+ sa.Column("practice_item_id", sa.Text(), nullable=False),
1283
+ sa.Column("topic_id", sa.Text(), nullable=False),
1284
+ sa.Column("was_generated", sa.Integer(), server_default=sa.text("0"), nullable=False),
1285
+ sa.Column("flagged", sa.Integer(), server_default=sa.text("0"), nullable=False),
1286
+ sa.Column("tell", sa.Text(), nullable=True),
1287
+ sa.Column("created_at", sa.Text(), nullable=False),
1288
+ sa.CheckConstraint(
1289
+ "tell IN ('too-easy', 'phrasing', 'topic-scope', 'formatting', 'no-tell')",
1290
+ name=op.f("ck_discrimination_trial_tell_enum"),
1291
+ ),
1292
+ sa.CheckConstraint("flagged IN (0,1)", name=op.f("ck_discrimination_trial_flagged_bool")),
1293
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_discrimination_trial_id_ulid")),
1294
+ sa.CheckConstraint(
1295
+ "was_generated IN (0,1)", name=op.f("ck_discrimination_trial_was_generated_bool")
1296
+ ),
1297
+ sa.ForeignKeyConstraint(
1298
+ ["practice_item_id"],
1299
+ ["practice_item.id"],
1300
+ name=op.f("fk_discrimination_trial_practice_item_id"),
1301
+ ondelete="RESTRICT",
1302
+ ),
1303
+ sa.ForeignKeyConstraint(
1304
+ ["session_id"],
1305
+ ["study_session.id"],
1306
+ name=op.f("fk_discrimination_trial_session_id"),
1307
+ ondelete="RESTRICT",
1308
+ ),
1309
+ sa.ForeignKeyConstraint(
1310
+ ["topic_id"],
1311
+ ["topic.id"],
1312
+ name=op.f("fk_discrimination_trial_topic_id"),
1313
+ ondelete="RESTRICT",
1314
+ ),
1315
+ sa.ForeignKeyConstraint(
1316
+ ["user_id"],
1317
+ ["user.id"],
1318
+ name=op.f("fk_discrimination_trial_user_id"),
1319
+ ondelete="RESTRICT",
1320
+ ),
1321
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_discrimination_trial")),
1322
+ sa.UniqueConstraint(
1323
+ "session_id",
1324
+ "practice_item_id",
1325
+ name=op.f("uq_discrimination_trial_session_id_practice_item_id"),
1326
+ ),
1327
+ )
1328
+ with op.batch_alter_table("discrimination_trial", schema=None) as batch_op:
1329
+ batch_op.create_index(
1330
+ "ix_discrimination_trial_user_generated", ["user_id", "was_generated"], unique=False
1331
+ )
1332
+ batch_op.create_index(
1333
+ batch_op.f("ix_discrimination_trial_user_id"), ["user_id"], unique=False
1334
+ )
1335
+
1336
+ op.create_table(
1337
+ "evidence",
1338
+ sa.Column("id", sa.Text(), nullable=False),
1339
+ sa.Column("user_id", sa.Text(), nullable=False),
1340
+ sa.Column("practice_item_id", sa.Text(), nullable=True),
1341
+ sa.Column("memory_item_id", sa.Text(), nullable=True),
1342
+ sa.Column("observation_id", sa.Text(), nullable=True),
1343
+ sa.Column("study_tier", sa.Text(), nullable=False),
1344
+ sa.Column("locator", sa.Text(), nullable=True),
1345
+ sa.Column("created_at", sa.Text(), nullable=False),
1346
+ sa.CheckConstraint(
1347
+ "study_tier IN ('E1', 'E2', 'E3')", name=op.f("ck_evidence_study_tier_enum")
1348
+ ),
1349
+ sa.CheckConstraint(
1350
+ "CASE WHEN practice_item_id IS NULL THEN 0 ELSE 1 END + CASE WHEN memory_item_id IS NULL THEN 0 ELSE 1 END + CASE WHEN observation_id IS NULL THEN 0 ELSE 1 END = 1",
1351
+ name=op.f("ck_evidence_exactly_one"),
1352
+ ),
1353
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_evidence_id_ulid")),
1354
+ sa.ForeignKeyConstraint(
1355
+ ["memory_item_id"],
1356
+ ["memory_item.id"],
1357
+ name=op.f("fk_evidence_memory_item_id"),
1358
+ ondelete="RESTRICT",
1359
+ ),
1360
+ sa.ForeignKeyConstraint(
1361
+ ["observation_id"],
1362
+ ["observation.id"],
1363
+ name=op.f("fk_evidence_observation_id"),
1364
+ ondelete="RESTRICT",
1365
+ ),
1366
+ sa.ForeignKeyConstraint(
1367
+ ["practice_item_id"],
1368
+ ["practice_item.id"],
1369
+ name=op.f("fk_evidence_practice_item_id"),
1370
+ ondelete="RESTRICT",
1371
+ ),
1372
+ sa.ForeignKeyConstraint(
1373
+ ["user_id"], ["user.id"], name=op.f("fk_evidence_user_id"), ondelete="RESTRICT"
1374
+ ),
1375
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_evidence")),
1376
+ )
1377
+ with op.batch_alter_table("evidence", schema=None) as batch_op:
1378
+ batch_op.create_index(batch_op.f("ix_evidence_user_id"), ["user_id"], unique=False)
1379
+
1380
+ op.create_table(
1381
+ "item_rejection",
1382
+ sa.Column("id", sa.Text(), nullable=False),
1383
+ sa.Column("user_id", sa.Text(), nullable=False),
1384
+ sa.Column("practice_item_id", sa.Text(), nullable=True),
1385
+ sa.Column("memory_item_id", sa.Text(), nullable=True),
1386
+ sa.Column("reason", sa.Text(), nullable=False),
1387
+ sa.Column("note", sa.Text(), nullable=True),
1388
+ sa.Column("created_at", sa.Text(), nullable=False),
1389
+ sa.CheckConstraint(
1390
+ "NOT (reason = 'wrong-answer-key') OR (practice_item_id IS NOT NULL)",
1391
+ name=op.f("ck_item_rejection_answer_key_is_practice"),
1392
+ ),
1393
+ sa.CheckConstraint(
1394
+ "reason IN ('off-syllabus', 'wrong-answer-key', 'wrong-topic', 'duplicate', 'unclear', 'wrong-difficulty')",
1395
+ name=op.f("ck_item_rejection_reason_enum"),
1396
+ ),
1397
+ sa.CheckConstraint(
1398
+ "CASE WHEN practice_item_id IS NULL THEN 0 ELSE 1 END + CASE WHEN memory_item_id IS NULL THEN 0 ELSE 1 END = 1",
1399
+ name=op.f("ck_item_rejection_exactly_one"),
1400
+ ),
1401
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_item_rejection_id_ulid")),
1402
+ sa.ForeignKeyConstraint(
1403
+ ["memory_item_id"],
1404
+ ["memory_item.id"],
1405
+ name=op.f("fk_item_rejection_memory_item_id"),
1406
+ ondelete="RESTRICT",
1407
+ ),
1408
+ sa.ForeignKeyConstraint(
1409
+ ["practice_item_id"],
1410
+ ["practice_item.id"],
1411
+ name=op.f("fk_item_rejection_practice_item_id"),
1412
+ ondelete="RESTRICT",
1413
+ ),
1414
+ sa.ForeignKeyConstraint(
1415
+ ["user_id"], ["user.id"], name=op.f("fk_item_rejection_user_id"), ondelete="RESTRICT"
1416
+ ),
1417
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_item_rejection")),
1418
+ )
1419
+ with op.batch_alter_table("item_rejection", schema=None) as batch_op:
1420
+ batch_op.create_index(batch_op.f("ix_item_rejection_user_id"), ["user_id"], unique=False)
1421
+
1422
+ op.create_table(
1423
+ "generation_directive",
1424
+ sa.Column("id", sa.Text(), nullable=False),
1425
+ sa.Column("user_id", sa.Text(), nullable=False),
1426
+ sa.Column("course_id", sa.Text(), nullable=True),
1427
+ sa.Column("topic_id", sa.Text(), nullable=True),
1428
+ sa.Column("kind", sa.Text(), nullable=False),
1429
+ sa.Column("text", sa.Text(), nullable=False),
1430
+ sa.Column("status", sa.Text(), server_default=sa.text("'proposed'"), nullable=False),
1431
+ sa.Column("superseded_by", sa.Text(), nullable=True),
1432
+ sa.Column("source_rejection_id", sa.Text(), nullable=True),
1433
+ sa.Column("created_at", sa.Text(), nullable=False),
1434
+ sa.Column("confirmed_at", sa.Text(), nullable=True),
1435
+ sa.CheckConstraint(
1436
+ "((confirmed_at IS NOT NULL) AND (status <> 'proposed')) OR (NOT (confirmed_at IS NOT NULL) AND NOT(status <> 'proposed'))",
1437
+ name=op.f("ck_generation_directive_confirmed_when_not_proposed"),
1438
+ ),
1439
+ sa.CheckConstraint(
1440
+ "((superseded_by IS NOT NULL) AND (status = 'superseded')) OR (NOT (superseded_by IS NOT NULL) AND NOT(status = 'superseded'))",
1441
+ name=op.f("ck_generation_directive_superseded_by_status"),
1442
+ ),
1443
+ sa.CheckConstraint(
1444
+ "kind IN ('format-preference', 'generator-correction')",
1445
+ name=op.f("ck_generation_directive_kind_enum"),
1446
+ ),
1447
+ sa.CheckConstraint(
1448
+ "status IN ('proposed', 'active', 'superseded')",
1449
+ name=op.f("ck_generation_directive_status_enum"),
1450
+ ),
1451
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_generation_directive_id_ulid")),
1452
+ sa.ForeignKeyConstraint(
1453
+ ["course_id"],
1454
+ ["course.id"],
1455
+ name=op.f("fk_generation_directive_course_id"),
1456
+ ondelete="RESTRICT",
1457
+ ),
1458
+ sa.ForeignKeyConstraint(
1459
+ ["source_rejection_id"],
1460
+ ["item_rejection.id"],
1461
+ name=op.f("fk_generation_directive_source_rejection_id"),
1462
+ ondelete="RESTRICT",
1463
+ ),
1464
+ sa.ForeignKeyConstraint(
1465
+ ["superseded_by"],
1466
+ ["generation_directive.id"],
1467
+ name=op.f("fk_generation_directive_superseded_by"),
1468
+ ondelete="RESTRICT",
1469
+ ),
1470
+ sa.ForeignKeyConstraint(
1471
+ ["topic_id"],
1472
+ ["topic.id"],
1473
+ name=op.f("fk_generation_directive_topic_id"),
1474
+ ondelete="RESTRICT",
1475
+ ),
1476
+ sa.ForeignKeyConstraint(
1477
+ ["user_id"],
1478
+ ["user.id"],
1479
+ name=op.f("fk_generation_directive_user_id"),
1480
+ ondelete="RESTRICT",
1481
+ ),
1482
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_generation_directive")),
1483
+ )
1484
+ with op.batch_alter_table("generation_directive", schema=None) as batch_op:
1485
+ batch_op.create_index(
1486
+ "ix_generation_directive_course_status", ["course_id", "status"], unique=False
1487
+ )
1488
+ batch_op.create_index(
1489
+ batch_op.f("ix_generation_directive_user_id"), ["user_id"], unique=False
1490
+ )
1491
+ batch_op.create_index(
1492
+ "ix_generation_directive_user_status", ["user_id", "status"], unique=False
1493
+ )
1494
+
1495
+ op.create_table(
1496
+ "replacement_queue",
1497
+ sa.Column("id", sa.Text(), nullable=False),
1498
+ sa.Column("user_id", sa.Text(), nullable=False),
1499
+ sa.Column("topic_id", sa.Text(), nullable=False),
1500
+ sa.Column("rejection_id", sa.Text(), nullable=True),
1501
+ sa.Column("closed_task_id", sa.Text(), nullable=True),
1502
+ sa.Column("status", sa.Text(), server_default=sa.text("'pending'"), nullable=False),
1503
+ sa.Column("filled_by_task_id", sa.Text(), nullable=True),
1504
+ sa.Column("created_at", sa.Text(), nullable=False),
1505
+ sa.Column("resolved_at", sa.Text(), nullable=True),
1506
+ sa.CheckConstraint(
1507
+ "((filled_by_task_id IS NOT NULL) AND (status = 'filled')) OR (NOT (filled_by_task_id IS NOT NULL) AND NOT(status = 'filled'))",
1508
+ name=op.f("ck_replacement_queue_filled_by_when_filled"),
1509
+ ),
1510
+ sa.CheckConstraint(
1511
+ "((resolved_at IS NOT NULL) AND (status <> 'pending')) OR (NOT (resolved_at IS NOT NULL) AND NOT(status <> 'pending'))",
1512
+ name=op.f("ck_replacement_queue_resolved_when_done"),
1513
+ ),
1514
+ sa.CheckConstraint(
1515
+ "status IN ('pending', 'filled', 'dropped')",
1516
+ name=op.f("ck_replacement_queue_status_enum"),
1517
+ ),
1518
+ sa.CheckConstraint(
1519
+ "CASE WHEN rejection_id IS NULL THEN 0 ELSE 1 END + CASE WHEN closed_task_id IS NULL THEN 0 ELSE 1 END = 1",
1520
+ name=op.f("ck_replacement_queue_exactly_one"),
1521
+ ),
1522
+ sa.CheckConstraint("length(id) = 26", name=op.f("ck_replacement_queue_id_ulid")),
1523
+ sa.ForeignKeyConstraint(
1524
+ ["closed_task_id"],
1525
+ ["generation_task.id"],
1526
+ name=op.f("fk_replacement_queue_closed_task_id"),
1527
+ ondelete="RESTRICT",
1528
+ ),
1529
+ sa.ForeignKeyConstraint(
1530
+ ["filled_by_task_id"],
1531
+ ["generation_task.id"],
1532
+ name=op.f("fk_replacement_queue_filled_by_task_id"),
1533
+ ondelete="RESTRICT",
1534
+ ),
1535
+ sa.ForeignKeyConstraint(
1536
+ ["rejection_id"],
1537
+ ["item_rejection.id"],
1538
+ name=op.f("fk_replacement_queue_rejection_id"),
1539
+ ondelete="RESTRICT",
1540
+ ),
1541
+ sa.ForeignKeyConstraint(
1542
+ ["topic_id"],
1543
+ ["topic.id"],
1544
+ name=op.f("fk_replacement_queue_topic_id"),
1545
+ ondelete="RESTRICT",
1546
+ ),
1547
+ sa.ForeignKeyConstraint(
1548
+ ["user_id"], ["user.id"], name=op.f("fk_replacement_queue_user_id"), ondelete="RESTRICT"
1549
+ ),
1550
+ sa.PrimaryKeyConstraint("id", name=op.f("pk_replacement_queue")),
1551
+ )
1552
+ with op.batch_alter_table("replacement_queue", schema=None) as batch_op:
1553
+ batch_op.create_index(batch_op.f("ix_replacement_queue_user_id"), ["user_id"], unique=False)
1554
+ batch_op.create_index(
1555
+ "ix_replacement_queue_user_status", ["user_id", "status"], unique=False
1556
+ )
1557
+
1558
+ # ### end Alembic commands ###
1559
+
1560
+ # Case-insensitive uniqueness, one index per case-insensitive name (Conventions). Written by
1561
+ # hand: autogenerate skips an expression-based index because SQLite cannot reflect one, so
1562
+ # these three would go missing from every regeneration, not just this one.
1563
+ op.create_index(
1564
+ "ux_course_code",
1565
+ "course",
1566
+ [sa.text("user_id"), sa.text("lower(code)"), sa.text("semester_name")],
1567
+ unique=True,
1568
+ )
1569
+ op.create_index(
1570
+ "ux_assessment_slot_name",
1571
+ "assessment_slot",
1572
+ [sa.text("course_id"), sa.text("lower(name)")],
1573
+ unique=True,
1574
+ )
1575
+ op.create_index(
1576
+ "ux_topic_live",
1577
+ "topic",
1578
+ [sa.text("course_id"), sa.text("lower(name)")],
1579
+ unique=True,
1580
+ sqlite_where=sa.text("status IN ('proposed', 'active', 'unexamined')"),
1581
+ postgresql_where=sa.text("status IN ('proposed', 'active', 'unexamined')"),
1582
+ )
1583
+
1584
+
1585
+ def downgrade() -> None:
1586
+ # ### commands auto generated by Alembic - please adjust! ###
1587
+ op.drop_index("ux_topic_live", table_name="topic")
1588
+ op.drop_index("ux_assessment_slot_name", table_name="assessment_slot")
1589
+ op.drop_index("ux_course_code", table_name="course")
1590
+ with op.batch_alter_table("replacement_queue", schema=None) as batch_op:
1591
+ batch_op.drop_index("ix_replacement_queue_user_status")
1592
+ batch_op.drop_index(batch_op.f("ix_replacement_queue_user_id"))
1593
+
1594
+ op.drop_table("replacement_queue")
1595
+ with op.batch_alter_table("generation_directive", schema=None) as batch_op:
1596
+ batch_op.drop_index("ix_generation_directive_user_status")
1597
+ batch_op.drop_index(batch_op.f("ix_generation_directive_user_id"))
1598
+ batch_op.drop_index("ix_generation_directive_course_status")
1599
+
1600
+ op.drop_table("generation_directive")
1601
+ with op.batch_alter_table("item_rejection", schema=None) as batch_op:
1602
+ batch_op.drop_index(batch_op.f("ix_item_rejection_user_id"))
1603
+
1604
+ op.drop_table("item_rejection")
1605
+ with op.batch_alter_table("evidence", schema=None) as batch_op:
1606
+ batch_op.drop_index(batch_op.f("ix_evidence_user_id"))
1607
+
1608
+ op.drop_table("evidence")
1609
+ with op.batch_alter_table("discrimination_trial", schema=None) as batch_op:
1610
+ batch_op.drop_index(batch_op.f("ix_discrimination_trial_user_id"))
1611
+ batch_op.drop_index("ix_discrimination_trial_user_generated")
1612
+
1613
+ op.drop_table("discrimination_trial")
1614
+ with op.batch_alter_table("attempt", schema=None) as batch_op:
1615
+ batch_op.drop_index("ix_attempt_user_topic")
1616
+ batch_op.drop_index(batch_op.f("ix_attempt_user_id"))
1617
+
1618
+ op.drop_table("attempt")
1619
+ with op.batch_alter_table("practice_item", schema=None) as batch_op:
1620
+ batch_op.drop_index(
1621
+ "ux_practice_item_position",
1622
+ sqlite_where=sa.text("past_exam_id IS NOT NULL"),
1623
+ postgresql_where=sa.text("past_exam_id IS NOT NULL"),
1624
+ )
1625
+ batch_op.drop_index(batch_op.f("ix_practice_item_user_id"))
1626
+ batch_op.drop_index("ix_practice_item_topic_state")
1627
+ batch_op.drop_index("ix_practice_item_origin_topic")
1628
+
1629
+ op.drop_table("practice_item")
1630
+ with op.batch_alter_table("observation", schema=None) as batch_op:
1631
+ batch_op.drop_index(batch_op.f("ix_observation_owner_id"))
1632
+
1633
+ op.drop_table("observation")
1634
+ with op.batch_alter_table("submission", schema=None) as batch_op:
1635
+ batch_op.drop_index(batch_op.f("ix_submission_user_id"))
1636
+
1637
+ op.drop_table("submission")
1638
+ with op.batch_alter_table("past_exam", schema=None) as batch_op:
1639
+ batch_op.drop_index("ix_past_exam_slot_date")
1640
+ batch_op.drop_index(batch_op.f("ix_past_exam_owner_id"))
1641
+
1642
+ op.drop_table("past_exam")
1643
+ with op.batch_alter_table("note", schema=None) as batch_op:
1644
+ batch_op.drop_index(batch_op.f("ix_note_user_id"))
1645
+
1646
+ op.drop_table("note")
1647
+ with op.batch_alter_table("memory_item", schema=None) as batch_op:
1648
+ batch_op.drop_index(batch_op.f("ix_memory_item_user_id"))
1649
+
1650
+ op.drop_table("memory_item")
1651
+ with op.batch_alter_table("class_meeting", schema=None) as batch_op:
1652
+ batch_op.drop_index(batch_op.f("ix_class_meeting_user_id"))
1653
+
1654
+ op.drop_table("class_meeting")
1655
+ with op.batch_alter_table("capacity_suggestion", schema=None) as batch_op:
1656
+ batch_op.drop_index(batch_op.f("ix_capacity_suggestion_user_id"))
1657
+
1658
+ op.drop_table("capacity_suggestion")
1659
+ op.drop_table("topic_weight")
1660
+ with op.batch_alter_table("plan_item", schema=None) as batch_op:
1661
+ batch_op.drop_index("ix_plan_item_user_topic")
1662
+ batch_op.drop_index("ix_plan_item_user_setup")
1663
+ batch_op.drop_index(batch_op.f("ix_plan_item_user_id"))
1664
+
1665
+ op.drop_table("plan_item")
1666
+ with op.batch_alter_table("generation_task", schema=None) as batch_op:
1667
+ batch_op.drop_index(batch_op.f("ix_generation_task_user_id"))
1668
+
1669
+ op.drop_table("generation_task")
1670
+ with op.batch_alter_table("coverage", schema=None) as batch_op:
1671
+ batch_op.drop_index(
1672
+ "ux_coverage_material",
1673
+ sqlite_where=sa.text("material_id IS NOT NULL"),
1674
+ postgresql_where=sa.text("material_id IS NOT NULL"),
1675
+ )
1676
+ batch_op.drop_index(
1677
+ "ux_coverage_assessment",
1678
+ sqlite_where=sa.text("assessment_id IS NOT NULL"),
1679
+ postgresql_where=sa.text("assessment_id IS NOT NULL"),
1680
+ )
1681
+ batch_op.drop_index(batch_op.f("ix_coverage_user_id"))
1682
+ batch_op.drop_index("ix_coverage_user_confirmed")
1683
+ batch_op.drop_index("ix_coverage_topic")
1684
+ batch_op.drop_index("ix_coverage_material")
1685
+
1686
+ op.drop_table("coverage")
1687
+ with op.batch_alter_table("topic_state", schema=None) as batch_op:
1688
+ batch_op.drop_index("ix_topic_state_user_due")
1689
+
1690
+ op.drop_table("topic_state")
1691
+ with op.batch_alter_table("topic_split", schema=None) as batch_op:
1692
+ batch_op.drop_index(batch_op.f("ix_topic_split_user_id"))
1693
+
1694
+ op.drop_table("topic_split")
1695
+ with op.batch_alter_table("hours_entry", schema=None) as batch_op:
1696
+ batch_op.drop_index("ix_hours_entry_user_setup")
1697
+ batch_op.drop_index("ix_hours_entry_user_occurred")
1698
+ batch_op.drop_index(batch_op.f("ix_hours_entry_user_id"))
1699
+
1700
+ op.drop_table("hours_entry")
1701
+ with op.batch_alter_table("exam_profile", schema=None) as batch_op:
1702
+ batch_op.drop_index(batch_op.f("ix_exam_profile_owner_id"))
1703
+
1704
+ op.drop_table("exam_profile")
1705
+ with op.batch_alter_table("assessment", schema=None) as batch_op:
1706
+ batch_op.drop_index(batch_op.f("ix_assessment_user_id"))
1707
+ batch_op.drop_index("ix_assessment_user_date")
1708
+
1709
+ op.drop_table("assessment")
1710
+ with op.batch_alter_table("topic", schema=None) as batch_op:
1711
+ batch_op.drop_index(batch_op.f("ix_topic_user_id"))
1712
+
1713
+ op.drop_table("topic")
1714
+ with op.batch_alter_table("study_session", schema=None) as batch_op:
1715
+ batch_op.drop_index(batch_op.f("ix_study_session_user_id"))
1716
+
1717
+ op.drop_table("study_session")
1718
+ with op.batch_alter_table("plan", schema=None) as batch_op:
1719
+ batch_op.drop_index(batch_op.f("ix_plan_user_id"))
1720
+
1721
+ op.drop_table("plan")
1722
+ with op.batch_alter_table("material", schema=None) as batch_op:
1723
+ batch_op.drop_index(batch_op.f("ix_material_user_id"))
1724
+
1725
+ op.drop_table("material")
1726
+ with op.batch_alter_table("coverage_reading", schema=None) as batch_op:
1727
+ batch_op.drop_index(batch_op.f("ix_coverage_reading_user_id"))
1728
+
1729
+ op.drop_table("coverage_reading")
1730
+ with op.batch_alter_table("assessment_slot", schema=None) as batch_op:
1731
+ batch_op.drop_index(batch_op.f("ix_assessment_slot_owner_id"))
1732
+
1733
+ op.drop_table("assessment_slot")
1734
+ with op.batch_alter_table("course", schema=None) as batch_op:
1735
+ batch_op.drop_index(batch_op.f("ix_course_user_id"))
1736
+
1737
+ op.drop_table("course")
1738
+ with op.batch_alter_table("capacity", schema=None) as batch_op:
1739
+ batch_op.drop_index(batch_op.f("ix_capacity_user_id"))
1740
+
1741
+ op.drop_table("capacity")
1742
+ op.drop_table("user")
1743
+ # ### end Alembic commands ###