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

95 lines
2.9 KiB
Python

import uuid
from datetime import datetime
from pydantic import BaseModel, Field
from sqlalchemy import BigInteger, Column
from sqlmodel import JSON, Relationship, SQLModel
from sqlmodel import Field as SQLField
from shared.enums.dns import DnsRecordType
from shared.utils.datetime import utc_now
class DnsLookup(SQLModel, table=True):
__tablename__ = "dns_lookups"
id: uuid.UUID = SQLField(default_factory=uuid.uuid4, primary_key=True)
host: str = SQLField(max_length=500)
status_code: str = SQLField(default="", max_length=20)
cdn: bool = SQLField(default=False)
cdn_name: str = SQLField(default="", max_length=100)
parsed_data: dict = SQLField(default_factory=dict, sa_column=Column(JSON))
queried_at: datetime = SQLField(default_factory=utc_now)
created_at: datetime = SQLField(default_factory=utc_now)
updated_at: datetime = SQLField(default_factory=utc_now)
records: list["DnsRecord"] = Relationship(
sa_relationship_kwargs={
"lazy": "selectin",
"cascade": "all, delete-orphan",
},
)
class DnsRecord(SQLModel, table=True):
__tablename__ = "dns_records"
id: uuid.UUID = SQLField(default_factory=uuid.uuid4, primary_key=True)
dns_lookup_id: uuid.UUID = SQLField(
foreign_key="dns_lookups.id", index=True, ondelete="CASCADE"
)
target_id: uuid.UUID = SQLField(
foreign_key="targets.id", index=True, ondelete="CASCADE"
)
record_type: DnsRecordType = SQLField(index=True)
value: str = SQLField(max_length=2000, index=True)
priority: int | None = SQLField(default=None)
weight: int | None = SQLField(default=None)
port: int | None = SQLField(default=None)
soa_email: str | None = SQLField(default=None, max_length=500)
soa_serial: int | None = SQLField(
default=None, sa_column=Column(BigInteger, nullable=True)
)
soa_refresh: int | None = SQLField(default=None)
soa_retry: int | None = SQLField(default=None)
soa_expire: int | None = SQLField(default=None)
soa_minttl: int | None = SQLField(default=None)
caa_tag: str | None = SQLField(default=None, max_length=100)
caa_flag: int | None = SQLField(default=None)
created_at: datetime = SQLField(default_factory=utc_now)
class DnsRecordRead(BaseModel):
id: uuid.UUID
record_type: DnsRecordType
value: str
priority: int | None = None
weight: int | None = None
port: int | None = None
soa_email: str | None = None
soa_serial: int | None = None
caa_tag: str | None = None
caa_flag: int | None = None
class DnsLookupSummary(BaseModel):
id: uuid.UUID
host: str
status_code: str = ""
cdn: bool = False
cdn_name: str = ""
queried_at: datetime
record_counts: dict[str, int] = Field(default_factory=dict)
class DnsLookupRead(DnsLookupSummary):
records: list[DnsRecordRead] = Field(default_factory=list)