From d6b7834cf3e87e37b43008625023cbdcf952d616 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 2 Jul 2022 20:18:44 +0200 Subject: add better cascading for ORM objects This prevents "weird" objects from lingering around. Now, if a user is deleted, their tracks are also being deleted - which is good, especially for the user's privacy. For a more "graceful" closing of the account, we could implement a strategy that first re-assigns the tracks to a different owner, and then deletes the account. But that may only happen with the consent of the user, and it is a work for future improvements. --- fietsboek/models/track.py | 5 +++-- fietsboek/models/user.py | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py index c8b8dc5..315f484 100644 --- a/fietsboek/models/track.py +++ b/fietsboek/models/track.py @@ -139,12 +139,13 @@ class Track(Base): link_secret = Column(Text) owner = relationship('User', back_populates='tracks') - cache = relationship('TrackCache', back_populates='track', uselist=False) + cache = relationship('TrackCache', back_populates='track', uselist=False, + cascade="all, delete-orphan") tagged_people = relationship('User', secondary=track_people_assoc, back_populates='tagged_tracks') badges = relationship('Badge', secondary=track_badge_assoc, back_populates='tracks') tags = relationship('Tag', back_populates='track', cascade="all, delete-orphan") - comments = relationship('Comment', back_populates='track') + comments = relationship('Comment', back_populates='track', cascade="all, delete-orphan") # GPX files are XML files with a lot of repeated property names. Really, it # is quite inefficient to store a whole ton of GPS points in big XML diff --git a/fietsboek/models/user.py b/fietsboek/models/user.py index cc06225..75707e2 100644 --- a/fietsboek/models/user.py +++ b/fietsboek/models/user.py @@ -94,12 +94,19 @@ class User(Base): is_admin = Column(Boolean, default=False) is_verified = Column(Boolean, default=False) - tracks = relationship('Track', back_populates='owner') + tracks = relationship('Track', back_populates='owner', cascade="all, delete-orphan") tagged_tracks = relationship('Track', secondary='track_people_assoc', back_populates='tagged_people') - uploads = relationship('Upload', back_populates='owner') - tokens = relationship('Token', back_populates='user') - comments = relationship('Comment', back_populates='author') + uploads = relationship('Upload', back_populates='owner', cascade="all, delete-orphan") + tokens = relationship('Token', back_populates='user', cascade="all, delete-orphan") + comments = relationship('Comment', back_populates='author', cascade="all, delete-orphan") + + # We don't use them, but include them to ensure our cascading works + friends_1 = relationship('User', secondary='friends_assoc', back_populates='friends_2', + foreign_keys=[friends_assoc.c.user_1_id]) + friends_2 = relationship('User', secondary='friends_assoc', back_populates='friends_1', + foreign_keys=[friends_assoc.c.user_2_id]) + @classmethod def query_by_email(cls, email): -- cgit v1.2.3