"""Mark study-plan source quizzes with origin='plan' Board Review I–XII exist twice over: once as a study plan the learner works through block by block, and once as the bulk quiz the plan was built from. The session list showed both, so the same twelve titles appeared under "Sessions" and again under "Library" with nothing to tell them apart. Tagging the bulk quizzes keeps them intact — the questions still hang off them via `source_quiz_id` — while taking them out of the list of things a learner picks from. A quiz already sat stays in the history regardless; that filtering lives in the endpoint, not here. Revision ID: b2c3d4e5f6a7 Revises: a1b2c3d4e5f6 """ import sqlalchemy as sa from alembic import op revision = "b2c3d4e5f6a7" down_revision = "a1b2c3d4e5f6" branch_labels = None depends_on = None def _has(table: str) -> bool: return table in sa.inspect(op.get_bind()).get_table_names() def upgrade() -> None: # Guarded because create_all() may have built these tables on a fresh # deploy before Alembic ran. if not (_has("quizzes") and _has("study_plans")): return # Matched on the trimmed title: one of the imports carries a trailing # space ("Board Review VII "). op.execute(sa.text(""" UPDATE quizzes SET origin = 'plan' WHERE course_id IS NULL AND origin <> 'plan' AND btrim(title) IN (SELECT btrim(name) FROM study_plans) """)) def downgrade() -> None: if not _has("quizzes"): return op.execute(sa.text("UPDATE quizzes SET origin = 'bank' WHERE origin = 'plan'"))