From 6505b471b05bc2e695874bb4e9c9fce58b0a505e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Sep 2025 22:45:58 +0000 Subject: [PATCH] Add Alembic migration for TSS database schema updates Co-authored-by: joaovitoriasilva <8648976+joaovitoriasilva@users.noreply.github.com> --- .../alembic/versions/v0_14_1_tss_migration.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 backend/app/alembic/versions/v0_14_1_tss_migration.py diff --git a/backend/app/alembic/versions/v0_14_1_tss_migration.py b/backend/app/alembic/versions/v0_14_1_tss_migration.py new file mode 100644 index 000000000..6d59d3ef3 --- /dev/null +++ b/backend/app/alembic/versions/v0_14_1_tss_migration.py @@ -0,0 +1,67 @@ +"""v0.14.1 migration - Add TSS support + +Revision ID: tss_support_001 +Revises: 86b2e24e227e +Create Date: 2024-12-08 22:30:00.000000 + +""" + +from typing import Sequence, Union +from alembic import op +import sqlalchemy as sa +from sqlalchemy import text + +# revision identifiers, used by Alembic. +revision: str = "tss_support_001" +down_revision: Union[str, None] = "86b2e24e227e" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # Add TSS column to activities table + op.add_column('activities', + sa.Column('tss', sa.Integer(), nullable=True, comment='Training Stress Score (TSS) for the activity') + ) + + # Add threshold fields to users table for TSS calculations + op.add_column('users', + sa.Column('ftp', sa.Integer(), nullable=True, comment='Functional Threshold Power (FTP) in watts for TSS calculation') + ) + op.add_column('users', + sa.Column('lthr', sa.Integer(), nullable=True, comment='Lactate Threshold Heart Rate (LTHR) in bpm for hrTSS calculation') + ) + op.add_column('users', + sa.Column('run_threshold_pace', sa.DECIMAL(precision=20, scale=10), nullable=True, comment='Running threshold pace in seconds per meter for rTSS calculation') + ) + op.add_column('users', + sa.Column('swim_threshold_pace', sa.DECIMAL(precision=20, scale=10), nullable=True, comment='Swimming threshold pace in seconds per meter for sTSS calculation') + ) + + # Add migration record if it doesn't exist + connection = op.get_bind() + + # Check if migration 6 record exists + result = connection.execute(text("SELECT COUNT(*) FROM migrations WHERE id = 6")).scalar() + + if result == 0: + # Insert migration 6 record + connection.execute(text(""" + INSERT INTO migrations (id, name, description, executed) + VALUES (6, 'TSS Support Migration', 'Add TSS calculation support with user threshold settings', 0) + """)) + + +def downgrade() -> None: + # Remove columns from users table + op.drop_column('users', 'swim_threshold_pace') + op.drop_column('users', 'run_threshold_pace') + op.drop_column('users', 'lthr') + op.drop_column('users', 'ftp') + + # Remove TSS column from activities table + op.drop_column('activities', 'tss') + + # Remove migration record + connection = op.get_bind() + connection.execute(text("DELETE FROM migrations WHERE id = 6")) \ No newline at end of file