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

166 lines
5.7 KiB
Python

"""
Sanitization Profile Model
Reusable database sanitization presets for WordPress environment sync/promote operations.
Profiles define which data to anonymize, truncate, or strip when copying databases
between environments.
"""
from datetime import datetime
from app import db
from app.models.json_column_mixin import JsonColumnMixin
import json
class SanitizationProfile(JsonColumnMixin, db.Model):
"""Reusable database sanitization preset."""
__tablename__ = 'sanitization_profiles'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
# JSON config with sanitization rules
config = db.Column(db.Text, nullable=False)
# Expected structure:
# {
# "anonymize_emails": true,
# "anonymize_names": true,
# "reset_passwords": true,
# "truncate_tables": ["wp_actionscheduler_actions", "wp_actionscheduler_logs"],
# "exclude_tables": [],
# "strip_payment_data": false,
# "remove_transients": true,
# "custom_search_replace": {}
# }
is_default = db.Column(db.Boolean, default=False)
is_builtin = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
# Relationships
user = db.relationship('User', backref=db.backref('sanitization_profiles', lazy='dynamic'))
def get_config(self):
"""Parse and return the config dict."""
return self._json_read('config')
def set_config(self, config_dict):
"""Set config from a dict."""
self.config = json.dumps(config_dict)
def to_dict(self):
return {
'id': self.id,
'user_id': self.user_id,
'name': self.name,
'description': self.description,
'config': self.get_config(),
'is_default': self.is_default,
'is_builtin': self.is_builtin,
'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'<SanitizationProfile {self.id} "{self.name}">'
# ==================== BUILT-IN PROFILES ====================
BUILTIN_PROFILES = [
{
'name': 'Basic',
'description': 'Anonymize email addresses and reset user passwords. Suitable for most development environments.',
'config': {
'anonymize_emails': True,
'anonymize_names': False,
'reset_passwords': True,
'truncate_tables': [
'actionscheduler_actions',
'actionscheduler_logs',
],
'exclude_tables': [],
'strip_payment_data': False,
'remove_transients': True,
'custom_search_replace': {},
}
},
{
'name': 'Full',
'description': 'Full sanitization: anonymize emails, names, reset passwords, and clear analytics/session data.',
'config': {
'anonymize_emails': True,
'anonymize_names': True,
'reset_passwords': True,
'truncate_tables': [
'actionscheduler_actions',
'actionscheduler_logs',
'statistics_visits',
'statistics_pages',
'slim_stats',
'redirection_logs',
'redirection_404',
],
'exclude_tables': [],
'strip_payment_data': False,
'remove_transients': True,
'custom_search_replace': {},
}
},
{
'name': 'WooCommerce',
'description': 'Strip WooCommerce order/payment data, anonymize customers, and clear session data.',
'config': {
'anonymize_emails': True,
'anonymize_names': True,
'reset_passwords': True,
'truncate_tables': [
'actionscheduler_actions',
'actionscheduler_logs',
'wc_orders',
'wc_order_addresses',
'wc_order_operational_data',
'wc_order_stats',
'woocommerce_sessions',
'woocommerce_log',
],
'exclude_tables': [],
'strip_payment_data': True,
'remove_transients': True,
'custom_search_replace': {},
}
},
]
@classmethod
def seed_builtins(cls, user_id):
"""Create built-in profiles for a user if they don't exist."""
created = []
for profile_data in cls.BUILTIN_PROFILES:
existing = cls.query.filter_by(
user_id=user_id,
name=profile_data['name'],
is_builtin=True
).first()
if not existing:
profile = cls(
user_id=user_id,
name=profile_data['name'],
description=profile_data['description'],
config=json.dumps(profile_data['config']),
is_builtin=True,
is_default=(profile_data['name'] == 'Basic'),
)
db.session.add(profile)
created.append(profile_data['name'])
if created:
db.session.commit()
return created