Files
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

38 lines
1.6 KiB
Python

from datetime import datetime
from app import db
from app.models.mixins import TimestampMixin
class OAuthIdentity(TimestampMixin, db.Model):
"""Links an external OAuth/SAML identity to a local user."""
__tablename__ = 'oauth_identities'
__table_args__ = (
db.UniqueConstraint('provider', 'provider_user_id', name='uq_provider_identity'),
)
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), nullable=False, index=True)
provider = db.Column(db.String(50), nullable=False) # google, github, oidc, saml
provider_user_id = db.Column(db.String(256), nullable=False)
provider_email = db.Column(db.String(256), nullable=True)
provider_display_name = db.Column(db.String(256), nullable=True)
access_token_encrypted = db.Column(db.Text, nullable=True)
refresh_token_encrypted = db.Column(db.Text, nullable=True)
token_expires_at = db.Column(db.DateTime, nullable=True)
last_login_at = db.Column(db.DateTime, nullable=True)
user = db.relationship('User', backref=db.backref('oauth_identities', lazy='dynamic'))
def to_dict(self):
return {
'id': self.id,
'provider': self.provider,
'provider_email': self.provider_email,
'provider_display_name': self.provider_display_name,
'created_at': self.created_at.isoformat() if self.created_at else None,
'last_login_at': self.last_login_at.isoformat() if self.last_login_at else None,
}
def __repr__(self):
return f'<OAuthIdentity {self.provider}:{self.provider_user_id}>'