Files
rengine/shared/models/target.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

230 lines
7.1 KiB
Python

import uuid
from datetime import datetime
from typing import TYPE_CHECKING
from pydantic import BaseModel, field_validator
from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint
from shared.enums.target import TargetType
from shared.enums.task_status import TaskStatus
from shared.models.bgp_summary import BgpSummaryRead, TargetBgpSummary
from shared.models.dns import DnsLookup, DnsLookupSummary
from shared.models.organization import Organization, OrganizationSummary
from shared.models.tag import TagSummary, TargetTag
from shared.models.whois import WhoisRecord, WhoisRecordSummary
from shared.utils.datetime import utc_now
from shared.utils.validation import clean_name
if TYPE_CHECKING:
from shared.models.tag import Tag
MAX_TAG_LEN = 50
MAX_ORG_LEN = 100
def _clean_labels(values: list[str] | None, max_len: int) -> list[str] | None:
"""Drop the blanks a pasted list carries, and bound what the column can hold."""
if values is None:
return None
return [clean_name(v, max_len=max_len) for v in values if (v or "").strip()]
class TargetValidationRequest(BaseModel):
target_value: str
class TargetValidationResponse(BaseModel):
valid: bool
target_type: TargetType | None
error: str | None
target_value: str
class TargetOrganization(SQLModel, table=True):
__tablename__ = "target_organizations"
target_id: uuid.UUID = Field(
foreign_key="targets.id", primary_key=True, ondelete="CASCADE"
)
organization_id: uuid.UUID = Field(
foreign_key="organizations.id", primary_key=True, ondelete="CASCADE"
)
class TargetBase(SQLModel):
target_value: str = Field(max_length=500)
display_name: str | None = Field(default=None, max_length=200)
class Target(TargetBase, table=True):
__tablename__ = "targets"
__table_args__ = (
UniqueConstraint("target_value", "project_id", name="uq_target_value_project"),
)
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
target_type: TargetType
project_id: uuid.UUID = Field(foreign_key="projects.id", index=True)
created_at: datetime = Field(default_factory=utc_now)
updated_at: datetime = Field(default_factory=utc_now)
created_by: uuid.UUID = Field(foreign_key="users.id")
whois_status: TaskStatus = Field(default=TaskStatus.PENDING, index=True)
whois_error: str | None = Field(default=None, max_length=1000)
whois_record: WhoisRecord | None = Relationship(
sa_relationship_kwargs={"lazy": "selectin"},
)
whois_record_id: uuid.UUID | None = Field(
default=None, foreign_key="whois_records.id", index=True, ondelete="SET NULL"
)
bgp_status: TaskStatus = Field(default=TaskStatus.PENDING, index=True)
bgp_summary: TargetBgpSummary | None = Relationship(
sa_relationship_kwargs={"lazy": "selectin", "passive_deletes": True},
)
dns_status: TaskStatus = Field(default=TaskStatus.PENDING, index=True)
dns_error: str | None = Field(default=None, max_length=1000)
dns_lookup: DnsLookup | None = Relationship(
sa_relationship_kwargs={
"lazy": "selectin",
"foreign_keys": "Target.dns_lookup_id",
},
)
dns_lookup_id: uuid.UUID | None = Field(
default=None, foreign_key="dns_lookups.id", index=True, ondelete="SET NULL"
)
organizations: list["Organization"] = Relationship(
link_model=TargetOrganization,
sa_relationship_kwargs={"lazy": "selectin"},
)
tags: list["Tag"] = Relationship(
back_populates="targets",
link_model=TargetTag,
sa_relationship_kwargs={"lazy": "selectin"},
)
class TargetCreate(TargetBase):
project_slug: str
organization_names: list[str] = Field(default_factory=list)
tag_names: list[str] = Field(default_factory=list)
@field_validator("tag_names", check_fields=False)
@classmethod
def _clean_tags(cls, v):
return _clean_labels(v, MAX_TAG_LEN)
@field_validator("organization_names", check_fields=False)
@classmethod
def _clean_orgs(cls, v):
return _clean_labels(v, MAX_ORG_LEN)
# bulk targets create/request/response models
class TargetBulkCreate(BaseModel):
project_slug: str
targets: list[str] = Field(
...,
min_length=1,
max_length=1000,
description="List of target values to import (max 1000)",
)
organization_names: list[str] = Field(default_factory=list)
tag_names: list[str] = Field(default_factory=list)
@field_validator("tag_names", "tags", check_fields=False)
@classmethod
def _clean_tags(cls, v):
return _clean_labels(v, MAX_TAG_LEN)
@field_validator("organization_names", "organizations", check_fields=False)
@classmethod
def _clean_orgs(cls, v):
return _clean_labels(v, MAX_ORG_LEN)
class TargetImportResult(BaseModel):
# single target import result
target_value: str
success: bool
target_type: TargetType | None = None
target_id: uuid.UUID | None = None
error: str | None = None
class TargetBulkCreateResponse(BaseModel):
total: int
imported: int
failed: int
skipped_duplicates: int
results: list[TargetImportResult]
class TargetUpdate(SQLModel):
display_name: str | None = Field(default=None, max_length=200)
organization_names: list[str] | None = None
tag_names: list[str] | None = None
@field_validator("tag_names", "tags", check_fields=False)
@classmethod
def _clean_tags(cls, v):
return _clean_labels(v, MAX_TAG_LEN)
@field_validator("organization_names", "organizations", check_fields=False)
@classmethod
def _clean_orgs(cls, v):
return _clean_labels(v, MAX_ORG_LEN)
class TargetRead(TargetBase):
id: uuid.UUID
target_type: TargetType
project_id: uuid.UUID
created_at: datetime
updated_at: datetime
created_by: uuid.UUID
whois_status: TaskStatus
whois_error: str | None
whois_record_id: uuid.UUID | None
whois: WhoisRecordSummary | None = None
bgp_status: TaskStatus
bgp: BgpSummaryRead | None = None
dns_status: TaskStatus = TaskStatus.PENDING
dns_error: str | None = None
dns_lookup_id: uuid.UUID | None = None
dns: DnsLookupSummary | None = None
organizations: list[OrganizationSummary] = Field(default_factory=list)
tags: list[TagSummary] = Field(default_factory=list)
# imports for both json and csv support
class TargetImportItem(BaseModel):
target_value: str
tags: list[str] = Field(default_factory=list)
organizations: list[str] = Field(default_factory=list)
display_name: str | None = None
@field_validator("tag_names", "tags", check_fields=False)
@classmethod
def _clean_tags(cls, v):
return _clean_labels(v, MAX_TAG_LEN)
@field_validator("organization_names", "organizations", check_fields=False)
@classmethod
def _clean_orgs(cls, v):
return _clean_labels(v, MAX_ORG_LEN)
class TargetImportRequest(BaseModel):
project_slug: str
targets: list[TargetImportItem] = Field(
...,
min_length=1,
max_length=500,
description="List of targets to import (max 500)",
)