95 lines
3.0 KiB
Python
95 lines
3.0 KiB
Python
from sqlalchemy import inspect, text
|
|
|
|
from app.db.base import engine
|
|
|
|
|
|
def _add_column_if_missing(table: str, column: str, ddl: str) -> None:
|
|
inspector = inspect(engine)
|
|
if table not in inspector.get_table_names():
|
|
return
|
|
columns = {col["name"] for col in inspector.get_columns(table)}
|
|
if column in columns:
|
|
return
|
|
with engine.begin() as conn:
|
|
conn.execute(text(ddl))
|
|
|
|
|
|
def run_fitness_migrations() -> None:
|
|
inspector = inspect(engine)
|
|
|
|
if "fitness_profiles" in inspector.get_table_names():
|
|
_add_column_if_missing(
|
|
"fitness_profiles",
|
|
"baseline_steps",
|
|
"ALTER TABLE fitness_profiles ADD COLUMN baseline_steps INTEGER",
|
|
)
|
|
_add_column_if_missing(
|
|
"fitness_profiles",
|
|
"baseline_workout_kcal",
|
|
"ALTER TABLE fitness_profiles ADD COLUMN baseline_workout_kcal FLOAT",
|
|
)
|
|
|
|
if "workout_logs" in inspector.get_table_names():
|
|
_add_column_if_missing(
|
|
"workout_logs",
|
|
"active_calories",
|
|
"ALTER TABLE workout_logs ADD COLUMN active_calories FLOAT",
|
|
)
|
|
_add_column_if_missing(
|
|
"workout_logs",
|
|
"total_calories",
|
|
"ALTER TABLE workout_logs ADD COLUMN total_calories FLOAT",
|
|
)
|
|
_add_column_if_missing(
|
|
"workout_logs",
|
|
"steps",
|
|
"ALTER TABLE workout_logs ADD COLUMN steps INTEGER",
|
|
)
|
|
|
|
if "step_logs" not in inspector.get_table_names():
|
|
with engine.begin() as conn:
|
|
conn.execute(
|
|
text(
|
|
"CREATE TABLE step_logs ("
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
"logged_at DATETIME DEFAULT CURRENT_TIMESTAMP, "
|
|
"steps INTEGER DEFAULT 0, "
|
|
"active_calories FLOAT, "
|
|
"source VARCHAR(32) DEFAULT 'manual', "
|
|
"notes TEXT DEFAULT ''"
|
|
")"
|
|
)
|
|
)
|
|
|
|
if "body_metrics" in inspector.get_table_names():
|
|
_add_column_if_missing(
|
|
"body_metrics",
|
|
"neck_cm",
|
|
"ALTER TABLE body_metrics ADD COLUMN neck_cm FLOAT",
|
|
)
|
|
_add_column_if_missing(
|
|
"body_metrics",
|
|
"hip_cm",
|
|
"ALTER TABLE body_metrics ADD COLUMN hip_cm FLOAT",
|
|
)
|
|
_add_column_if_missing(
|
|
"body_metrics",
|
|
"body_fat_method",
|
|
"ALTER TABLE body_metrics ADD COLUMN body_fat_method VARCHAR(16)",
|
|
)
|
|
_add_column_if_missing(
|
|
"body_metrics",
|
|
"whr",
|
|
"ALTER TABLE body_metrics ADD COLUMN whr FLOAT",
|
|
)
|
|
_add_column_if_missing(
|
|
"body_metrics",
|
|
"lbm_kg",
|
|
"ALTER TABLE body_metrics ADD COLUMN lbm_kg FLOAT",
|
|
)
|
|
_add_column_if_missing(
|
|
"body_metrics",
|
|
"ffmi",
|
|
"ALTER TABLE body_metrics ADD COLUMN ffmi FLOAT",
|
|
)
|