Files
guarddog-nexus/guarddog_nexus/models.py

69 lines
2.3 KiB
Python

"""SQLAlchemy ORM models."""
import datetime
from enum import Enum
from sqlalchemy import (
JSON,
Boolean,
DateTime,
ForeignKey,
Integer,
String,
Text,
UniqueConstraint,
func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from guarddog_nexus.database import Base
class ScanStatus(str, Enum):
PENDING = "pending"
SCANNING = "scanning"
COMPLETED = "completed"
FAILED = "failed"
class Scan(Base):
__tablename__ = "scans"
__table_args__ = (
UniqueConstraint("package_name", "package_version", "repository", name="uq_scan_pkg"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
package_name: Mapped[str] = mapped_column(String(255), nullable=False)
package_version: Mapped[str] = mapped_column(String(255), nullable=False)
ecosystem: Mapped[str] = mapped_column(String(50), nullable=False, default="pypi")
repository: Mapped[str] = mapped_column(String(255), nullable=False)
nexus_asset_url: Mapped[str] = mapped_column(Text, nullable=False)
sha256: Mapped[str | None] = mapped_column(String(64), nullable=True)
status: Mapped[str] = mapped_column(
String(20), nullable=False, default=ScanStatus.PENDING.value
)
total_findings: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
flagged: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
started_at: Mapped[datetime.datetime] = mapped_column(
DateTime, nullable=False, default=func.now()
)
finished_at: Mapped[datetime.datetime | None] = mapped_column(DateTime, nullable=True)
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
findings: Mapped[list["Finding"]] = relationship(
"Finding", back_populates="scan", cascade="all, delete-orphan"
)
class Finding(Base):
__tablename__ = "findings"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
scan_id: Mapped[int] = mapped_column(Integer, ForeignKey("scans.id"), nullable=False)
data: Mapped[dict] = mapped_column(JSON, nullable=False)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime, nullable=False, default=func.now()
)
scan: Mapped["Scan"] = relationship("Scan", back_populates="findings")