import sys import os from datetime import datetime, timedelta # Add the app directory to the path sys.path.insert(0, '/app') from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from app.db.base import Base from app.models.user import User from app.models.chore import Chore from app.core.security import get_password_hash # Database setup DATABASE_URL = "sqlite:///./family_hub.db" engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) def init_db(): """Initialize database with schema and demo data""" print("Creating all tables...") Base.metadata.create_all(bind=engine) db = SessionLocal() try: # Check if users already exist existing_users = db.query(User).count() if existing_users > 0: print(f"Database already has {existing_users} users. Skipping initialization.") return print("Creating family members...") # Create family members users_data = [ {"username": "lou", "email": "lou@family.local", "full_name": "Lou", "is_admin": False}, {"username": "jess", "email": "jess@family.local", "full_name": "Jess", "is_admin": True}, {"username": "william", "email": "william@family.local", "full_name": "William", "is_admin": False}, {"username": "xander", "email": "xander@family.local", "full_name": "Xander", "is_admin": False}, {"username": "bella", "email": "bella@family.local", "full_name": "Bella", "is_admin": False}, ] users = {} for user_data in users_data: user = User( username=user_data["username"], email=user_data["email"], full_name=user_data["full_name"], hashed_password=get_password_hash("password123"), is_admin=user_data["is_admin"], is_active=True, discord_id=None, profile_picture=None ) db.add(user) users[user_data["username"]] = user print(f" + Created user: {user_data['full_name']} ({user_data['username']})") db.commit() # Refresh to get IDs for user in users.values(): db.refresh(user) print("\nCreating demo chores...") # Create demo chores with various statuses and assignments today = datetime.now() demo_chores = [ # Daily chores { "title": "Feed the Dog", "description": "Give Rex his breakfast and dinner", "room": "Kitchen", "frequency": "daily", "assignment_type": "any_one", "status": "completed", "assigned_user_id": users["william"].id, "due_date": today.replace(hour=23, minute=59, second=59), "completed_at": today.replace(hour=8, minute=30) }, { "title": "Take Out Trash", "description": "Empty all bins and take to curb", "room": "Kitchen", "frequency": "daily", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["xander"].id, "due_date": today.replace(hour=23, minute=59, second=59), "completed_at": None }, { "title": "Wash Dishes", "description": "Load and run the dishwasher", "room": "Kitchen", "frequency": "daily", "assignment_type": "any_one", "status": "in_progress", "assigned_user_id": users["bella"].id, "due_date": today.replace(hour=23, minute=59, second=59), "completed_at": None }, # Weekly chores { "title": "Vacuum Living Room", "description": "Vacuum carpets and under furniture", "room": "Living Room", "frequency": "weekly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["lou"].id, "due_date": (today + timedelta(days=3)).replace(hour=23, minute=59, second=59), "completed_at": None }, { "title": "Clean Bathrooms", "description": "Scrub toilets, sinks, and showers", "room": "Bathroom", "frequency": "weekly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["jess"].id, "due_date": (today + timedelta(days=2)).replace(hour=23, minute=59, second=59), "completed_at": None }, { "title": "Mow Lawn", "description": "Mow front and back yards", "room": "Yard", "frequency": "weekly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["william"].id, "due_date": (today + timedelta(days=5)).replace(hour=23, minute=59, second=59), "completed_at": None }, # Monthly chores { "title": "Change Air Filters", "description": "Replace HVAC air filters throughout house", "room": "Utility Room", "frequency": "monthly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["lou"].id, "due_date": (today + timedelta(days=15)).replace(hour=23, minute=59, second=59), "completed_at": None }, { "title": "Deep Clean Fridge", "description": "Empty, wipe down shelves, check expiry dates", "room": "Kitchen", "frequency": "monthly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["jess"].id, "due_date": (today + timedelta(days=20)).replace(hour=23, minute=59, second=59), "completed_at": None }, # On-trigger chores (no specific schedule) { "title": "Water Plants", "description": "Check soil moisture and water as needed", "room": "Living Room", "frequency": "on_trigger", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["bella"].id, "due_date": today.replace(hour=23, minute=59, second=59), "completed_at": None }, { "title": "Sort Recycling", "description": "Separate recyclables into proper bins", "room": "Garage", "frequency": "on_trigger", "assignment_type": "any_one", "status": "completed", "assigned_user_id": users["xander"].id, "due_date": (today - timedelta(days=1)).replace(hour=23, minute=59, second=59), "completed_at": (today - timedelta(days=1)).replace(hour=14, minute=20) }, # Some overdue chores { "title": "Organize Garage", "description": "Sort tools and clean workspace", "room": "Garage", "frequency": "monthly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["william"].id, "due_date": (today - timedelta(days=3)).replace(hour=23, minute=59, second=59), "completed_at": None }, { "title": "Clean Windows", "description": "Wash all interior and exterior windows", "room": "Whole House", "frequency": "monthly", "assignment_type": "any_one", "status": "pending", "assigned_user_id": users["xander"].id, "due_date": (today - timedelta(days=1)).replace(hour=23, minute=59, second=59), "completed_at": None }, ] for chore_data in demo_chores: chore = Chore(**chore_data, created_by=users["jess"].id) db.add(chore) status_marker = "[DONE]" if chore_data["status"] == "completed" else "[WIP]" if chore_data["status"] == "in_progress" else "[TODO]" print(f" {status_marker} {chore_data['title']} - {chore_data['frequency']} ({chore_data['room']})") db.commit() print(f"\n[SUCCESS] Database initialized with {len(users_data)} users and {len(demo_chores)} demo chores!") except Exception as e: print(f"[ERROR] Error initializing database: {e}") db.rollback() raise finally: db.close() if __name__ == "__main__": init_db()