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

80 lines
2.3 KiB
Python

import uuid
from datetime import datetime, timedelta
from pydantic import BaseModel, Field, field_validator
from sqlalchemy import Text
from sqlalchemy.dialects.postgresql import JSONB
from sqlmodel import Column, SQLModel
from sqlmodel import Field as SQLField
from shared.enums.notification import NotificationSeverity, NotificationType
from shared.utils.datetime import utc_now
MAX_URL_LENGTH = 500
class NotificationMetadata(BaseModel):
url: str | None = None
open_new_tab: bool = False
scan_id: str | None = None
target_id: str | None = None
action_label: str | None = Field(default=None, max_length=50)
@field_validator("url")
@classmethod
def validate_url(cls, v: str | None) -> str | None:
if v is None:
return v
if (
not v.startswith("/")
and not v.startswith("http://")
and not v.startswith("https://")
):
msg = "URL must be relative (start with /) or absolute (http/https)"
raise ValueError(msg)
if len(v) > MAX_URL_LENGTH:
msg = f"URL too long (max {MAX_URL_LENGTH} characters)"
raise ValueError(msg)
return v
class NotificationBase(SQLModel):
type: NotificationType
severity: NotificationSeverity
title: str = SQLField(max_length=200)
message: str = SQLField(sa_column=Column(Text))
class Notification(NotificationBase, table=True):
__tablename__ = "notifications"
id: int = SQLField(default=None, primary_key=True)
project_id: uuid.UUID | None = SQLField(default=None, index=True)
notification_metadata: dict = SQLField(
default_factory=dict, sa_column=Column(JSONB)
)
is_read: bool = SQLField(default=False, index=True)
created_at: datetime = SQLField(default_factory=utc_now, index=True)
expires_at: datetime = SQLField(
default_factory=lambda: utc_now() + timedelta(days=7),
index=True,
)
class NotificationCreate(NotificationBase):
notification_metadata: NotificationMetadata | None = None
class NotificationRead(NotificationBase):
id: int
project_id: uuid.UUID | None = None
notification_metadata: dict
is_read: bool
created_at: datetime
expires_at: datetime
class NotificationStats(BaseModel):
total: int
unread: int