This commit is contained in:
2025-09-28 06:28:03 -07:00
parent 678d42da12
commit 913e635dd0

20
main.py
View File

@@ -31,17 +31,16 @@ logging.info(f"Database URL: {DATABASE_URL}")
os.makedirs(DATABASE_PATH, exist_ok=True) os.makedirs(DATABASE_PATH, exist_ok=True)
# For production, use PostgreSQL: DATABASE_URL = "postgresql://username:password@localhost/meal_planner" # For production, use PostgreSQL: DATABASE_URL = "postgresql://username:password@localhost/meal_planner"
try: engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {})
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()
Base = declarative_base()
# Initialize FastAPI app # Initialize FastAPI app
app = FastAPI(title="Meal Planner") app = FastAPI(title="Meal Planner")
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
# Database Models # Database Models
class Food(Base): class Food(Base):
__tablename__ = "foods" __tablename__ = "foods"
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
@@ -59,7 +58,7 @@ try:
source = Column(String, default="manual") # manual, csv, openfoodfacts source = Column(String, default="manual") # manual, csv, openfoodfacts
brand = Column(String, default="") # Brand name for the food brand = Column(String, default="") # Brand name for the food
class Meal(Base): class Meal(Base):
__tablename__ = "meals" __tablename__ = "meals"
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
@@ -215,6 +214,7 @@ def get_db():
finally: finally:
db.close() db.close()
try:
# Create tables # Create tables
Base.metadata.create_all(bind=engine) Base.metadata.create_all(bind=engine)
logging.info("Database tables checked/created successfully.") logging.info("Database tables checked/created successfully.")