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

40 lines
1.7 KiB
Python

from datetime import datetime
from app import db
from app.models.mixins import EncryptedSecret, SerializableMixin
class SourceConnection(SerializableMixin, db.Model):
"""External source-code provider connection for repository imports.
The ``provider`` column is a plain string and accepts any supported
provider value (e.g. ``'github'`` or ``'gitlab'``); there is no enum or
allow-list to extend.
"""
__tablename__ = 'source_connections'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True)
provider = db.Column(db.String(40), nullable=False, index=True)
provider_account_id = db.Column(db.String(120), nullable=True, index=True)
provider_username = db.Column(db.String(120), nullable=True)
display_name = db.Column(db.String(180), nullable=True)
avatar_url = db.Column(db.String(500), nullable=True)
access_token_encrypted = db.Column(db.Text, nullable=False)
access_token = EncryptedSecret('access_token_encrypted', legacy_plaintext=True)
scope = db.Column(db.String(500), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
last_used_at = db.Column(db.DateTime, nullable=True)
user = db.relationship('User', backref=db.backref('source_connections', lazy='dynamic'))
__table_args__ = (
db.UniqueConstraint('user_id', 'provider', name='uq_source_connection_user_provider'),
)
# Serialization comes from SerializableMixin; these columns stay out
# of API payloads (parity with the deleted hand-written to_dict).
__serialize_exclude__ = ('user_id',)