Files
Juan DenisandClaude Fable 5 5417c39100 perf(models): index every foreign-key column
An audit found 102 FK columns with no index — every join, per-parent
listing, and the delete cascades from _delete_cascade_policy scanned
whole tables on them (deployments.app_id, audit_logs.user_id,
dns_records.zone_id, webhook_logs.webhook_id, ...). All FK columns now
declare index=True; migration 096 creates the same indexes on existing
installs, deriving the list from the live schema at upgrade time (so
it is idempotent and cannot drift from what an install actually has).

Ratchet: test_every_fk_column_is_indexed pins the rule with no
allowlist — a new FK column is born indexed or the build fails.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 20:42:53 -04:00

51 lines
2.2 KiB
Python

from datetime import datetime
from app import db
from app.models.mixins import SoftDeleteMixin, TimestampMixin
class SavedView(TimestampMixin, SoftDeleteMixin, db.Model):
"""A user's saved table view for a list page (Services, Domains, …).
Captures the page's table state — filter, search, sort levels, hidden
columns, page size — as JSON so the frontend can re-apply it verbatim.
Built-in views ship in the frontend; only user-created views live here.
At most one view per (user, page) is the default, enforced by the service.
"""
__tablename__ = 'saved_views'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), nullable=False, index=True)
page = db.Column(db.String(80), nullable=False)
name = db.Column(db.String(120), nullable=False)
# URL-safe handle so a view can be linked: /domains?view=ssl-expiring-soon.
# Unique per (user, page) among LIVE rows; derived from the name on create.
slug = db.Column(db.String(140), nullable=True, index=True)
state = db.Column(db.JSON, nullable=False, default=dict)
is_default = db.Column(db.Boolean, nullable=False, default=False)
# No table-level UNIQUE on (user_id, page, name): a deleted view keeps its
# row, and the constraint would then refuse to let you re-create a view by
# the same name. Migration 083 makes it a PARTIAL unique index restricted to
# rows where deleted_at IS NULL.
__table_args__ = ()
user = db.relationship('User', backref=db.backref('saved_views', cascade='all, delete-orphan'))
def to_dict(self):
return {
'id': self.id,
'user_id': self.user_id,
'page': self.page,
'name': self.name,
'slug': self.slug,
'state': self.state or {},
**self.soft_delete_dict(),
'is_default': self.is_default,
'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
}
def __repr__(self):
return f'<SavedView {self.page}/{self.name} user={self.user_id}>'