mirror of
https://github.com/yogeshojha/rengine.git
synced 2026-10-20 23:31:31 +02:00
SQLModel emitted index=True beside primary_key=True on every id field, so 41 tables carried ix_<table>_id alongside <table>_pkey: the same single-column btree twice. 12 MB of duplicate index, and every insert maintaining both — on the hot path of a scan writing 345,152 endpoints. The planner had been using them (72,160 scans across the set) simply because they were there and equivalent; with them gone it uses the primary key, verified on subdomains: Index Scan using subdomains_pkey. The migration finds them rather than naming them, because which tables exist depends on how far an install has migrated and a name already gone must not fail an upgrade. It drops only a non-unique, single-column btree on id belonging to a table that has a primary key — no other index can match that shape. There is no downgrade: the primary key serves every lookup they served. The 33 models are corrected too, so the next hand-written migration is not comparing against a schema that would want them back. Counted honestly: 41, not the 102 an earlier pass reported — that number included the 61 primary keys themselves. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel
|
|
from sqlalchemy import Column
|
|
from sqlalchemy.types import JSON
|
|
from sqlmodel import Field, SQLModel, UniqueConstraint
|
|
|
|
from shared.definitions.ports import PortSource, PortState, ServiceClass
|
|
from shared.utils.datetime import utc_now
|
|
|
|
|
|
class Port(SQLModel, table=True):
|
|
__tablename__ = "ports"
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"scan_id", "ip", "number", "protocol", name="uq_port_scan_ip_num_proto"
|
|
),
|
|
)
|
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
|
scan_id: uuid.UUID = Field(foreign_key="scans.id", index=True, ondelete="CASCADE")
|
|
target_id: uuid.UUID = Field(
|
|
foreign_key="targets.id", index=True, ondelete="CASCADE"
|
|
)
|
|
project_id: uuid.UUID = Field(foreign_key="projects.id", index=True)
|
|
|
|
ip: str = Field(max_length=45, index=True)
|
|
number: int = Field(index=True)
|
|
protocol: str = Field(default="tcp", max_length=8)
|
|
state: str = Field(default=PortState.OPEN.value, max_length=16)
|
|
service_name: str | None = Field(default=None, max_length=64, index=True)
|
|
service_class: str = Field(
|
|
default=ServiceClass.OTHER.value, max_length=16, index=True
|
|
)
|
|
source: str = Field(max_length=30)
|
|
|
|
is_http: bool = Field(default=False, index=True)
|
|
tls: bool = Field(default=False)
|
|
product: str | None = Field(default=None, max_length=200)
|
|
version: str | None = Field(default=None, max_length=100)
|
|
banner: str | None = Field(default=None, max_length=1000)
|
|
cpe: list = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
|
|
|
|
discovered_at: datetime = Field(default_factory=utc_now)
|
|
created_at: datetime = Field(default_factory=utc_now)
|
|
|
|
|
|
class PortRead(BaseModel):
|
|
id: uuid.UUID
|
|
scan_id: uuid.UUID
|
|
target_id: uuid.UUID
|
|
ip: str
|
|
number: int
|
|
protocol: str
|
|
state: str
|
|
service_name: str | None = None
|
|
service_class: str = ServiceClass.OTHER.value
|
|
source: str = PortSource.NAABU.value
|
|
is_http: bool = False
|
|
tls: bool = False
|
|
product: str | None = None
|
|
version: str | None = None
|
|
banner: str | None = None
|
|
cpe: list[str] = Field(default_factory=list)
|
|
discovered_at: datetime
|
|
|
|
|
|
class PortSummary(BaseModel):
|
|
total: int
|
|
by_service: dict[str, int] = Field(default_factory=dict)
|