diff --git a/backend/migrations/005_add_completion_logs.py b/backend/migrations/005_add_completion_logs.py new file mode 100644 index 0000000..c35d1d0 --- /dev/null +++ b/backend/migrations/005_add_completion_logs.py @@ -0,0 +1,79 @@ +"""Add chore_completion_logs table for comprehensive chore tracking.""" +import sqlite3 +import sys +from pathlib import Path + +# Add parent directory to path to import from app +sys.path.insert(0, str(Path(__file__).parent.parent)) + +def upgrade(): + """Create chore_completion_logs table.""" + db_path = Path(__file__).parent.parent / "data" / "family_hub.db" + + print(f"Connecting to database: {db_path}") + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + + try: + # Check if table already exists + cursor.execute(""" + SELECT name FROM sqlite_master + WHERE type='table' AND name='chore_completion_logs' + """) + + if cursor.fetchone(): + print("⚠️ Table chore_completion_logs already exists, skipping...") + return + + # Create chore_completion_logs table + print("Creating chore_completion_logs table...") + cursor.execute(""" + CREATE TABLE chore_completion_logs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + chore_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + completed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + notes TEXT, + verified_by_user_id INTEGER, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (chore_id) REFERENCES chores(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, + FOREIGN KEY (verified_by_user_id) REFERENCES users(id) ON DELETE SET NULL + ) + """) + + # Create indexes for better query performance + print("Creating indexes...") + cursor.execute(""" + CREATE INDEX idx_completion_logs_chore_id + ON chore_completion_logs(chore_id) + """) + + cursor.execute(""" + CREATE INDEX idx_completion_logs_user_id + ON chore_completion_logs(user_id) + """) + + cursor.execute(""" + CREATE INDEX idx_completion_logs_completed_at + ON chore_completion_logs(completed_at) + """) + + conn.commit() + print("✅ Successfully created chore_completion_logs table") + print(" Features:") + print(" - Tracks every chore completion instance") + print(" - Optional notes for each completion") + print(" - Optional verification by another user") + print(" - Full historical data for reporting") + + except Exception as e: + print(f"❌ Error during migration: {e}") + conn.rollback() + raise + finally: + conn.close() + +if __name__ == "__main__": + upgrade() + print("\n✅ Migration completed!")