Files
rengine/shared/models/scan_command.py
T
Yogesh OjhaandClaude Opus 5 163df52ad5 perf(db): a primary key is already an index, so the second one on id was write cost for nothing
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
2026-09-10 21:24:43 +05:30

50 lines
1.6 KiB
Python

import uuid
from datetime import datetime
from pydantic import BaseModel
from sqlalchemy import Column, Text
from sqlmodel import Field, SQLModel
from shared.enums.scan import ScanActivityStatus
from shared.utils.datetime import utc_now
class ScanCommand(SQLModel, table=True):
__tablename__ = "scan_commands"
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
scan_id: uuid.UUID = Field(foreign_key="scans.id", index=True, ondelete="CASCADE")
activity_id: uuid.UUID | None = Field(
default=None, foreign_key="scan_activities.id", index=True, ondelete="CASCADE"
)
project_id: uuid.UUID = Field(foreign_key="projects.id", index=True)
tool: str = Field(max_length=100, index=True)
command: str = Field(sa_column=Column(Text, nullable=False))
status: str = Field(default=ScanActivityStatus.RUNNING.value, index=True)
return_code: int | None = Field(default=None)
output: str | None = Field(default=None, sa_column=Column(Text))
error: str | None = Field(default=None, max_length=2000)
duration_seconds: float | None = Field(default=None)
started_at: datetime = Field(default_factory=utc_now)
completed_at: datetime | None = Field(default=None)
class ScanCommandRead(BaseModel):
id: uuid.UUID
scan_id: uuid.UUID
activity_id: uuid.UUID | None = None
tool: str
command: str
status: str
return_code: int | None = None
error: str | None = None
duration_seconds: float | None = None
started_at: datetime
completed_at: datetime | None = None
class ScanCommandDetail(ScanCommandRead):
output: str | None = None