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

69 lines
2.6 KiB
Python

from datetime import datetime
from app import db
from app.models.json_column_mixin import JsonColumnMixin
from app.models.mixins import TimestampMixin, SerializableMixin
import json
class DNSZone(JsonColumnMixin, TimestampMixin, db.Model):
"""DNS zone for a domain with provider integration."""
__tablename__ = 'dns_zones'
id = db.Column(db.Integer, primary_key=True)
domain = db.Column(db.String(256), nullable=False, unique=True)
provider = db.Column(db.String(64)) # cloudflare, route53, digitalocean, manual
provider_zone_id = db.Column(db.String(128))
provider_config_json = db.Column(db.Text) # legacy inline credentials (pre-unification)
# Canonical credential link: the DNSProviderConfig (Settings -> Connections)
# whose API token manages this zone. Supersedes the inline token in
# provider_config_json; nullable so manual zones and not-yet-migrated rows work.
dns_provider_config_id = db.Column(
db.Integer, db.ForeignKey('dns_provider_configs.id'), nullable=True, index=True)
status = db.Column(db.String(32), default='active')
last_sync_at = db.Column(db.DateTime)
records = db.relationship('DNSRecord', backref='zone', lazy='dynamic', cascade='all, delete-orphan')
@property
def provider_config(self):
return self._json_read('provider_config_json')
@provider_config.setter
def provider_config(self, v):
self.provider_config_json = json.dumps(v)
def to_dict(self):
return {
'id': self.id,
'domain': self.domain,
'provider': self.provider,
'provider_zone_id': self.provider_zone_id,
'dns_provider_config_id': self.dns_provider_config_id,
'status': self.status,
'record_count': self.records.count(),
'last_sync_at': self.last_sync_at.isoformat() if self.last_sync_at else None,
'created_at': self.created_at.isoformat() if self.created_at else None,
}
class DNSRecord(SerializableMixin, TimestampMixin, db.Model):
"""Individual DNS record within a zone."""
__tablename__ = 'dns_records'
id = db.Column(db.Integer, primary_key=True)
zone_id = db.Column(db.Integer, db.ForeignKey('dns_zones.id'), nullable=False, index=True)
record_type = db.Column(db.String(10), nullable=False) # A, AAAA, CNAME, MX, TXT, SRV, CAA
name = db.Column(db.String(256), nullable=False)
content = db.Column(db.Text, nullable=False)
ttl = db.Column(db.Integer, default=3600)
priority = db.Column(db.Integer) # MX, SRV
proxied = db.Column(db.Boolean, default=False) # Cloudflare proxy
provider_record_id = db.Column(db.String(128))