Files
ServerKit/backend/app/models/container_sleep_policy.py
Juan DenisandClaude Fable 5 7fd90a6c87 refactor(models): one door for delete cascades on NOT NULL child FKs
Replaces the 29 hand-written cascade='all, delete-orphan' declarations
from c1d4c0af/1704d60b/79a78c3c with a single mapper hook
(_delete_cascade_policy): at configure time, every one-to-many whose
child FK is NOT NULL gets the delete cascade automatically — with a
NOT NULL FK the children cannot outlive the parent, so the only choice
is cascade or crash. New models (extensions included — registering one
re-fires the hook) are covered with zero ceremony; nothing to remember.

Relationships where the parent delete should be REFUSED instead opt
out in the module's DELIBERATELY_UNCASCADED registry with their
justification (User.applications guarded by delete_user's 409,
CloudProvider.servers with no delete path). The sweep test now proves
the door works rather than policing model authors, and pins the
registry so it can only shrink.

Model files revert to their pre-fix text: delete semantics live in one
place instead of twenty.

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

30 lines
1.3 KiB
Python

from datetime import datetime
from app import db
class ContainerSleepPolicy(db.Model):
"""Per-application auto-sleep policy: when enabled, an idle app is stopped
after `idle_timeout_minutes` of no recorded activity, and can be woken on
demand. One row per application."""
__tablename__ = 'container_sleep_policies'
id = db.Column(db.Integer, primary_key=True)
application_id = db.Column(db.Integer, db.ForeignKey('applications.id'),
nullable=False, unique=True, index=True)
enabled = db.Column(db.Boolean, default=False, nullable=False)
idle_timeout_minutes = db.Column(db.Integer, default=30, nullable=False)
last_activity_at = db.Column(db.DateTime)
asleep = db.Column(db.Boolean, default=False, nullable=False)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
application = db.relationship('Application', backref=db.backref('sleep_policy', uselist=False))
def to_dict(self):
return {
'application_id': self.application_id,
'enabled': self.enabled,
'idle_timeout_minutes': self.idle_timeout_minutes,
'last_activity_at': self.last_activity_at.isoformat() if self.last_activity_at else None,
'asleep': self.asleep,
}