Files
thechart/scripts/migrate_csv.py
William Valentin e35a8af5c1 Implement dose tracking functionality and enhance CSV migration
- Added a new migration script to introduce dose tracking columns in the CSV.
- Updated DataManager to handle new dose tracking columns and methods for adding doses.
- Enhanced MedTrackerApp to support dose entry and display for each medicine.
- Modified UIManager to create a scrollable input frame with dose tracking elements.
- Implemented tests for delete functionality, dose tracking, edit functionality, and scrollable input.
- Updated existing tests to ensure compatibility with the new CSV format and dose tracking features.
2025-07-28 20:52:29 -07:00

68 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""
Migration script to add dose tracking columns to existing CSV data.
"""
import shutil
from datetime import datetime
import pandas as pd
def migrate_csv(filename: str = "thechart_data.csv") -> None:
"""Migrate existing CSV to new format with dose tracking columns."""
# Create backup
backup_name = f"{filename}.backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
shutil.copy2(filename, backup_name)
print(f"Created backup: {backup_name}")
try:
# Read existing data
df = pd.read_csv(filename)
print(f"Read {len(df)} existing entries")
# Add new dose tracking columns
df["bupropion_doses"] = ""
df["hydroxyzine_doses"] = ""
df["gabapentin_doses"] = ""
df["propranolol_doses"] = ""
# Reorder columns to match new format
new_column_order = [
"date",
"depression",
"anxiety",
"sleep",
"appetite",
"bupropion",
"bupropion_doses",
"hydroxyzine",
"hydroxyzine_doses",
"gabapentin",
"gabapentin_doses",
"propranolol",
"propranolol_doses",
"note",
]
df = df[new_column_order]
# Save migrated data
df.to_csv(filename, index=False)
print(f"Successfully migrated {filename}")
print(
"New columns added: bupropion_doses, hydroxyzine_doses, "
"gabapentin_doses, propranolol_doses"
)
except Exception as e:
print(f"Error during migration: {e}")
print(f"Restoring from backup: {backup_name}")
shutil.copy2(backup_name, filename)
raise
if __name__ == "__main__":
migrate_csv()