| import os |
| from datetime import datetime |
| from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, Boolean, ForeignKey |
| from sqlalchemy.orm import declarative_base, sessionmaker, relationship |
|
|
| |
| DB_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "letxipu.db") |
| engine = create_engine(f"sqlite:///{DB_PATH}", echo=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) |
| username = Column(String, unique=True, index=True, nullable=False) |
| hashed_password = Column(String, nullable=False) |
| role = Column(String, default="user") |
| created_at = Column(DateTime, default=datetime.utcnow) |
| |
| projects = relationship("Project", back_populates="owner", cascade="all, delete-orphan") |
|
|
|
|
| class Project(Base): |
| __tablename__ = "projects" |
| |
| id = Column(Integer, primary_key=True, index=True) |
| title = Column(String, nullable=False) |
| description = Column(Text, nullable=True) |
| owner_id = Column(Integer, ForeignKey("users.id"), nullable=False) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| |
| owner = relationship("User", back_populates="projects") |
| jobs = relationship("ResearchJob", back_populates="project", cascade="all, delete-orphan") |
|
|
|
|
| class ResearchJob(Base): |
| __tablename__ = "research_jobs" |
| |
| id = Column(Integer, primary_key=True, index=True) |
| project_id = Column(Integer, ForeignKey("projects.id"), nullable=False) |
| query = Column(Text, nullable=False) |
| status = Column(String, default="pending") |
| progress_pct = Column(Integer, default=0) |
| report_md = Column(Text, nullable=True) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| completed_at = Column(DateTime, nullable=True) |
| |
| project = relationship("Project", back_populates="jobs") |
|
|
| |
| def init_db(): |
| Base.metadata.create_all(bind=engine) |
|
|
| if __name__ == "__main__": |
| init_db() |
| print(f"Base de datos inicializada en: {DB_PATH}") |
|
|