78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
"""
|
|
Database initialization script.
|
|
Creates the database tables and populates with initial family member data.
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add the app directory to the path
|
|
sys.path.append(str(Path(__file__).parent))
|
|
|
|
from app.core.database import engine, Base
|
|
from app.models.user import User
|
|
from app.models.chore import Chore
|
|
from app.models.meal import Meal
|
|
from app.core.security import get_password_hash
|
|
from sqlalchemy.orm import Session
|
|
|
|
def init_database():
|
|
"""Initialize the database with tables and default data."""
|
|
print("Creating database tables...")
|
|
Base.metadata.create_all(bind=engine)
|
|
print("✅ Database tables created successfully!")
|
|
|
|
# Create a session to add default users
|
|
db = Session(bind=engine)
|
|
|
|
try:
|
|
# Check if users already exist
|
|
existing_user = db.query(User).first()
|
|
if existing_user:
|
|
print("⚠️ Database already contains users. Skipping initialization.")
|
|
return
|
|
|
|
print("Adding default family members...")
|
|
|
|
# Family members with default password "changeme123"
|
|
family_members = [
|
|
{"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},
|
|
]
|
|
|
|
default_password = "changeme123"
|
|
hashed_password = get_password_hash(default_password)
|
|
|
|
for member_data in family_members:
|
|
user = User(
|
|
username=member_data["username"],
|
|
email=member_data["email"],
|
|
full_name=member_data["full_name"],
|
|
hashed_password=hashed_password,
|
|
is_admin=member_data["is_admin"],
|
|
is_active=True
|
|
)
|
|
db.add(user)
|
|
|
|
db.commit()
|
|
print("✅ Family members added successfully!")
|
|
print("\n📋 Default Login Credentials:")
|
|
print(" Username: jess (admin) | Password: changeme123")
|
|
print(" Username: lou | Password: changeme123")
|
|
print(" Username: william | Password: changeme123")
|
|
print(" Username: xander | Password: changeme123")
|
|
print(" Username: bella | Password: changeme123")
|
|
print("\n⚠️ Please change these passwords after first login!")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error initializing database: {e}")
|
|
db.rollback()
|
|
raise
|
|
finally:
|
|
db.close()
|
|
|
|
if __name__ == "__main__":
|
|
init_database()
|