Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration file to populate total tasks for old projects if null #2246

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/backend/migrations/populate_total_tasks.py
Original file line number Diff line number Diff line change
@@ -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())
Loading