Spaces:
Running
Running
| from datetime import datetime | |
| from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, Text, create_engine | |
| from sqlalchemy.orm import declarative_base, relationship, sessionmaker | |
| try: | |
| from .config import Config | |
| except ImportError: | |
| from config import Config | |
| DATABASE_URL = Config.SQLALCHEMY_DATABASE_URI | |
| connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {} | |
| engine = create_engine(DATABASE_URL, connect_args=connect_args) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| class User(Base): | |
| __tablename__ = "user" | |
| id = Column(Integer, primary_key=True, index=True) | |
| username = Column(String(150), unique=True, nullable=False, index=True) | |
| password = Column(String(200), nullable=False) | |
| chats = relationship("Chat", back_populates="user", cascade="all, delete-orphan") | |
| class Chat(Base): | |
| __tablename__ = "chat" | |
| id = Column(Integer, primary_key=True, index=True) | |
| user_id = Column(Integer, ForeignKey("user.id"), nullable=False) | |
| title = Column(String(200), default="New Chat") | |
| mode = Column(String(20), default="normal") | |
| is_pinned = Column(Boolean, default=False) | |
| is_archived = Column(Boolean, default=False) | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| user = relationship("User", back_populates="chats") | |
| messages = relationship("Message", back_populates="chat", cascade="all, delete-orphan") | |
| files = relationship("UploadedFile", back_populates="chat", cascade="all, delete-orphan") | |
| class Message(Base): | |
| __tablename__ = "message" | |
| id = Column(Integer, primary_key=True, index=True) | |
| chat_id = Column(Integer, ForeignKey("chat.id"), nullable=False, index=True) | |
| role = Column(String(50), nullable=False) | |
| content = Column(Text, nullable=False) | |
| timestamp = Column(DateTime, default=datetime.utcnow) | |
| chat = relationship("Chat", back_populates="messages") | |
| class UploadedFile(Base): | |
| __tablename__ = "uploaded_file" | |
| id = Column(Integer, primary_key=True, index=True) | |
| chat_id = Column(Integer, ForeignKey("chat.id"), nullable=False, index=True) | |
| filename = Column(String(200), nullable=False) | |
| filepath = Column(String(300), nullable=False) | |
| uploaded_at = Column(DateTime, default=datetime.utcnow) | |
| chat = relationship("Chat", back_populates="files") | |
| def init_db(): | |
| Base.metadata.create_all(bind=engine) | |