Key changes: - Removed office-centric model (deleted offices.py, office-rules) - Renamed to team-rules, managers are part of their own team - Team calendar visible to all (read-only for employees) - Admins can have a manager assigned
43 lines
963 B
Python
43 lines
963 B
Python
"""
|
|
Database Connection Management
|
|
"""
|
|
from contextlib import contextmanager
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app import config
|
|
|
|
engine = create_engine(
|
|
config.DATABASE_URL,
|
|
connect_args={"check_same_thread": False} if "sqlite" in config.DATABASE_URL else {}
|
|
)
|
|
|
|
SessionLocal = sessionmaker(bind=engine)
|
|
|
|
|
|
def get_db():
|
|
"""Database session for FastAPI dependency injection"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@contextmanager
|
|
def get_db_session():
|
|
"""Database session for regular Python code"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def init_db():
|
|
"""Create all tables"""
|
|
from database.models import Base
|
|
|
|
print(f"[init_db] Initializing database at {config.DATABASE_URL}")
|
|
Base.metadata.create_all(bind=engine)
|
|
print(f"[init_db] Tables created: {list(Base.metadata.tables.keys())}")
|