From 567f6ea7a640c941ddc0b5668fb2faf5190318c7 Mon Sep 17 00:00:00 2001 From: Anuj-Gupta4 Date: Tue, 4 Mar 2025 15:49:36 +0545 Subject: [PATCH] fix(migration): add file to populate total tasks for old projects if null --- .../migrations/populate_total_tasks.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/backend/migrations/populate_total_tasks.py diff --git a/src/backend/migrations/populate_total_tasks.py b/src/backend/migrations/populate_total_tasks.py new file mode 100644 index 000000000..4bee6bbb4 --- /dev/null +++ b/src/backend/migrations/populate_total_tasks.py @@ -0,0 +1,32 @@ +"""Populate total_tasks for old projects.""" + +import asyncio + +from psycopg import AsyncConnection + +from app.config import settings + + +async def populate_total_tasks(): + """Populate total_tasks for all projects where it's NULL or 0.""" + async with await AsyncConnection.connect( + settings.FMTM_DB_URL.unicode_string() + ) as db: + async with db.cursor() as cur: + sql = """ + UPDATE projects + SET total_tasks = ( + SELECT COALESCE(MAX(project_task_index), 0) + FROM public.tasks + WHERE projects.id = tasks.project_id + ) + WHERE total_tasks IS NULL OR total_tasks = 0; + """ + await cur.execute(sql) + await db.commit() + + print("✅ Total tasks populated successfully") + + +if __name__ == "__main__": + asyncio.run(populate_total_tasks())