|
| 1 | +from uuid import UUID |
| 2 | + |
| 3 | +import typer |
| 4 | +from sqlalchemy import text |
| 5 | + |
| 6 | +from infrastructure.commands.utils import coro |
| 7 | +from infrastructure.database import session_manager |
| 8 | + |
| 9 | +app = typer.Typer(help="Delete subscales and score-type reports across all versions of an applet.") |
| 10 | + |
| 11 | +DROP_SCORE_FUNC_SQL = """ |
| 12 | +CREATE OR REPLACE FUNCTION drop_score_reports(sr jsonb) |
| 13 | +RETURNS jsonb LANGUAGE sql IMMUTABLE AS $$ |
| 14 | + SELECT CASE |
| 15 | + WHEN sr ? 'reports' THEN |
| 16 | + jsonb_set( |
| 17 | + sr, '{reports}', |
| 18 | + COALESCE( |
| 19 | + (SELECT jsonb_agg(r) |
| 20 | + FROM jsonb_array_elements(sr->'reports') r |
| 21 | + WHERE r->>'type' <> 'score'), |
| 22 | + '[]'::jsonb |
| 23 | + ), |
| 24 | + true |
| 25 | + ) |
| 26 | + ELSE sr |
| 27 | + END; |
| 28 | +$$; |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +@app.command(name="delete-subscales-for-all-applet-versions") |
| 33 | +@coro |
| 34 | +async def delete_subscales_for_all_applet_versions( |
| 35 | + applet_id: UUID = typer.Argument(..., help="Base applet ID (UUID)"), |
| 36 | +) -> None: |
| 37 | + """ |
| 38 | + Remove all subscales and score-type reports from every version of the specified applet. |
| 39 | + """ |
| 40 | + # obtain a session maker; then call it to get an async session |
| 41 | + s_maker = session_manager.get_session() |
| 42 | + async with s_maker() as session: |
| 43 | + await session.execute(text(DROP_SCORE_FUNC_SQL)) |
| 44 | + await session.commit() |
| 45 | + |
| 46 | + # Get the list of version IDs for this applet |
| 47 | + versions_sql = text(""" |
| 48 | + SELECT id_version |
| 49 | + FROM applet_histories |
| 50 | + WHERE id = :applet_id |
| 51 | + """) |
| 52 | + versions = ( |
| 53 | + ( |
| 54 | + await session.execute( |
| 55 | + versions_sql, |
| 56 | + {"applet_id": applet_id}, |
| 57 | + ) |
| 58 | + ) |
| 59 | + .scalars() |
| 60 | + .all() |
| 61 | + ) |
| 62 | + |
| 63 | + if not versions: |
| 64 | + typer.echo(f"No history versions found for applet {applet_id}.") |
| 65 | + return |
| 66 | + |
| 67 | + # Delete subscales and score-type reports in both tables |
| 68 | + hist_update_sql = text(""" |
| 69 | + UPDATE activity_histories |
| 70 | + SET subscale_setting = NULL, |
| 71 | + scores_and_reports = CASE |
| 72 | + WHEN scores_and_reports IS NULL THEN NULL |
| 73 | + ELSE drop_score_reports(scores_and_reports) |
| 74 | + END |
| 75 | + WHERE applet_id = ANY(:versions) |
| 76 | + """) |
| 77 | + live_update_sql = text(""" |
| 78 | + UPDATE activities |
| 79 | + SET subscale_setting = NULL, |
| 80 | + scores_and_reports = CASE |
| 81 | + WHEN scores_and_reports IS NULL THEN NULL |
| 82 | + ELSE drop_score_reports(scores_and_reports) |
| 83 | + END |
| 84 | + WHERE applet_id = :applet_id |
| 85 | + """) |
| 86 | + |
| 87 | + await session.execute(hist_update_sql, {"versions": versions}) |
| 88 | + await session.execute(live_update_sql, {"applet_id": applet_id}) |
| 89 | + await session.commit() |
| 90 | + |
| 91 | + typer.echo(f"Deleted subscales and score-type reports for {len(versions)} version(s) of applet {applet_id}.") |
0 commit comments