Files
ServerKit/backend/app/models/environment_activity.py
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

55 lines
2.0 KiB
Python

"""
Environment Activity Model
Audit log for all environment actions (create, deploy, promote, lock, etc.).
Tracks who did what, when, and how long it took.
"""
from datetime import datetime
from app import db
from app.models.json_column_mixin import JsonColumnMixin
class EnvironmentActivity(JsonColumnMixin, db.Model):
"""Audit log for environment actions."""
__tablename__ = 'environment_activities'
id = db.Column(db.Integer, primary_key=True)
site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False, index=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True, index=True)
# Action details
action = db.Column(db.String(50), nullable=False) # create, deploy, promote, lock, unlock, destroy
description = db.Column(db.Text)
activity_metadata = db.Column('metadata', db.Text) # JSON
# Result
status = db.Column(db.String(20), default='completed') # pending, running, completed, failed
error_message = db.Column(db.Text)
duration_seconds = db.Column(db.Float)
# Timestamps
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Relationships
site = db.relationship('WordPressSite', backref=db.backref('activities', lazy='dynamic'))
user = db.relationship('User', backref=db.backref('environment_activities', lazy='dynamic'))
def to_dict(self):
return {
'id': self.id,
'site_id': self.site_id,
'user_id': self.user_id,
'action': self.action,
'description': self.description,
'metadata': self._json_read('activity_metadata', None),
'status': self.status,
'error_message': self.error_message,
'duration_seconds': self.duration_seconds,
'created_at': self.created_at.isoformat() if self.created_at else None,
}
def __repr__(self):
return f'<EnvironmentActivity {self.id} {self.action} site={self.site_id}>'