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
114 lines
3.7 KiB
Python
114 lines
3.7 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
from functools import partial
|
|
|
|
from pydantic import BaseModel, field_validator
|
|
from sqlalchemy import Column
|
|
from sqlalchemy.types import JSON
|
|
from sqlmodel import Field, SQLModel
|
|
|
|
from shared.definitions.schedule_constants import MAX_SCHEDULE_TARGETS
|
|
from shared.enums.scan_schedule import ScheduleStatus
|
|
from shared.utils.datetime import utc_now
|
|
from shared.utils.validation import clean_name, clean_optional_name
|
|
|
|
|
|
class ScanSchedule(SQLModel, table=True):
|
|
__tablename__ = "scan_schedules"
|
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
|
project_id: uuid.UUID = Field(foreign_key="projects.id", index=True)
|
|
name: str = Field(max_length=200)
|
|
target_ids: list = Field(
|
|
default_factory=list, sa_column=Column(JSON, nullable=False)
|
|
)
|
|
engine_id: uuid.UUID = Field(index=True)
|
|
engine_name: str = Field(max_length=200)
|
|
context_id: uuid.UUID | None = Field(default=None)
|
|
context_name: str | None = Field(default=None, max_length=200)
|
|
|
|
schedule_type: str = Field(index=True)
|
|
run_at: datetime | None = Field(default=None)
|
|
interval_every: int | None = Field(default=None)
|
|
interval_unit: str | None = Field(default=None, max_length=10)
|
|
daily_at_time: str | None = Field(default=None, max_length=5)
|
|
cron_expression: str | None = Field(default=None, max_length=120)
|
|
timezone: str = Field(default="UTC", max_length=64)
|
|
|
|
status: str = Field(default=ScheduleStatus.ACTIVE.value, index=True)
|
|
next_run_at: datetime | None = Field(default=None, index=True)
|
|
last_run_at: datetime | None = Field(default=None)
|
|
last_error: str | None = Field(default=None, max_length=2000)
|
|
total_run_count: int = Field(default=0)
|
|
|
|
created_by: uuid.UUID = Field(foreign_key="users.id")
|
|
created_at: datetime = Field(default_factory=utc_now)
|
|
updated_at: datetime = Field(default_factory=utc_now)
|
|
|
|
|
|
class ScheduleTargetRef(BaseModel):
|
|
id: uuid.UUID
|
|
target_value: str
|
|
target_type: str
|
|
|
|
|
|
class ScanScheduleCreate(BaseModel):
|
|
name: str
|
|
target_ids: list[uuid.UUID] = Field(min_length=1, max_length=MAX_SCHEDULE_TARGETS)
|
|
engine_id: uuid.UUID
|
|
context_id: uuid.UUID | None = None
|
|
schedule_type: str
|
|
run_at: datetime | None = None
|
|
interval_every: int | None = None
|
|
interval_unit: str | None = None
|
|
daily_at_time: str | None = None
|
|
cron_expression: str | None = None
|
|
|
|
_validate_name = field_validator("name")(partial(clean_name, max_len=200))
|
|
|
|
|
|
class ScanScheduleUpdate(BaseModel):
|
|
name: str | None = None
|
|
target_ids: list[uuid.UUID] | None = Field(
|
|
default=None, min_length=1, max_length=MAX_SCHEDULE_TARGETS
|
|
)
|
|
engine_id: uuid.UUID | None = None
|
|
context_id: uuid.UUID | None = None
|
|
schedule_type: str | None = None
|
|
run_at: datetime | None = None
|
|
interval_every: int | None = None
|
|
interval_unit: str | None = None
|
|
daily_at_time: str | None = None
|
|
cron_expression: str | None = None
|
|
|
|
_validate_name = field_validator("name")(partial(clean_optional_name, max_len=200))
|
|
|
|
|
|
class ScanScheduleRead(BaseModel):
|
|
id: uuid.UUID
|
|
project_id: uuid.UUID
|
|
name: str
|
|
target_ids: list[uuid.UUID]
|
|
targets: list[ScheduleTargetRef]
|
|
engine_id: uuid.UUID
|
|
engine_name: str
|
|
context_id: uuid.UUID | None
|
|
context_name: str | None
|
|
schedule_type: str
|
|
run_at: datetime | None
|
|
interval_every: int | None
|
|
interval_unit: str | None
|
|
daily_at_time: str | None
|
|
cron_expression: str | None
|
|
timezone: str
|
|
status: str
|
|
next_run_at: datetime | None
|
|
last_run_at: datetime | None
|
|
last_error: str | None
|
|
total_run_count: int
|
|
cadence: str
|
|
timezone_stale: bool = False
|
|
created_by: uuid.UUID
|
|
created_at: datetime
|
|
updated_at: datetime
|