mirror of
https://github.com/yogeshojha/rengine.git
synced 2026-10-20 15:21:30 +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
90 lines
2.8 KiB
Python
90 lines
2.8 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.utils.datetime import utc_now
|
|
|
|
|
|
class IpAddress(SQLModel, table=True):
|
|
__tablename__ = "ip_addresses"
|
|
__table_args__ = (UniqueConstraint("scan_id", "ip", name="uq_ipaddress_scan_ip"),)
|
|
|
|
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)
|
|
version: int = Field(default=4)
|
|
source: str = Field(max_length=30, index=True)
|
|
ptr_hostnames: list = Field(
|
|
default_factory=list, sa_column=Column(JSON, nullable=False)
|
|
)
|
|
asn: int | None = Field(default=None, index=True)
|
|
asn_org: str | None = Field(default=None, max_length=255)
|
|
prefix: str | None = Field(default=None, max_length=64)
|
|
country: str | None = Field(default=None, max_length=10)
|
|
is_cdn: bool = Field(default=False, index=True)
|
|
cdn_name: str | None = Field(default=None, max_length=100)
|
|
cdn_type: str | None = Field(default=None, max_length=20)
|
|
is_alive: bool | None = Field(default=None)
|
|
scan_policy: str | None = Field(default=None, max_length=16, index=True)
|
|
scan_policy_reason: str | None = Field(default=None, max_length=32)
|
|
|
|
discovered_at: datetime = Field(default_factory=utc_now)
|
|
created_at: datetime = Field(default_factory=utc_now)
|
|
|
|
|
|
class IpAddressRead(BaseModel):
|
|
id: uuid.UUID
|
|
scan_id: uuid.UUID
|
|
target_id: uuid.UUID
|
|
ip: str
|
|
version: int
|
|
source: str
|
|
ptr_hostnames: list[str] = Field(default_factory=list)
|
|
asn: int | None = None
|
|
asn_org: str | None = None
|
|
prefix: str | None = None
|
|
country: str | None = None
|
|
is_cdn: bool = False
|
|
cdn_name: str | None = None
|
|
cdn_type: str | None = None
|
|
is_alive: bool | None = None
|
|
scan_policy: str | None = None
|
|
scan_policy_reason: str | None = None
|
|
discovered_at: datetime
|
|
|
|
|
|
class IpAddressSummary(BaseModel):
|
|
total: int
|
|
alive: int
|
|
cdn: int
|
|
by_source: dict[str, int] = Field(default_factory=dict)
|
|
|
|
|
|
class TargetIpAddressRead(BaseModel):
|
|
ip: str
|
|
version: int
|
|
source: str
|
|
ptr_hostnames: list[str] = Field(default_factory=list)
|
|
asn: int | None = None
|
|
asn_org: str | None = None
|
|
prefix: str | None = None
|
|
country: str | None = None
|
|
is_cdn: bool = False
|
|
cdn_name: str | None = None
|
|
cdn_type: str | None = None
|
|
is_alive: bool | None = None
|
|
scan_policy: str | None = None
|
|
scan_count: int
|
|
last_scan_id: uuid.UUID
|
|
first_seen: datetime
|
|
last_seen: datetime
|