| | import os |
| | from datetime import datetime |
| | from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, ForeignKey, JSON |
| | from sqlalchemy.orm import declarative_base, sessionmaker, relationship |
| |
|
| | DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "careerai.db") |
| | engine = create_engine(f"sqlite:///{DB_PATH}", connect_args={"check_same_thread": False}) |
| | SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
| |
|
| | Base = declarative_base() |
| |
|
| | class User(Base): |
| | __tablename__ = "users" |
| |
|
| | id = Column(Integer, primary_key=True, index=True) |
| | email = Column(String, unique=True, index=True, nullable=False) |
| | name = Column(String, nullable=False) |
| | picture = Column(String, nullable=True) |
| | hashed_password = Column(String, nullable=True) |
| | google_id = Column(String, unique=True, index=True, nullable=True) |
| | created_at = Column(DateTime, default=datetime.utcnow) |
| |
|
| | conversations = relationship("Conversation", back_populates="user", cascade="all, delete-orphan") |
| |
|
| | class Conversation(Base): |
| | __tablename__ = "conversations" |
| |
|
| | id = Column(String, primary_key=True, index=True) |
| | user_id = Column(Integer, ForeignKey("users.id"), nullable=False) |
| | title = Column(String, nullable=False) |
| | messages = Column(JSON, nullable=False, default=list) |
| | updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) |
| |
|
| | user = relationship("User", back_populates="conversations") |
| |
|
| | |
| | Base.metadata.create_all(bind=engine) |
| |
|
| | |
| | def get_db(): |
| | db = SessionLocal() |
| | try: |
| | yield db |
| | finally: |
| | db.close() |
| |
|