From 2429e95102acb2e1f50cdf054600bf9a6a5ddd73 Mon Sep 17 00:00:00 2001 From: rr- Date: Tue, 2 Aug 2016 11:56:19 +0200 Subject: [PATCH] server/users: fix cascade deletions --- server/szurubooru/db/comment.py | 6 ++- server/szurubooru/db/post.py | 14 +++-- server/szurubooru/db/user.py | 3 ++ server/szurubooru/tests/db/test_user.py | 71 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/server/szurubooru/db/comment.py b/server/szurubooru/db/comment.py index cfe6c90b..fdccf7a0 100644 --- a/server/szurubooru/db/comment.py +++ b/server/szurubooru/db/comment.py @@ -1,5 +1,5 @@ from sqlalchemy import Column, Integer, DateTime, UnicodeText, ForeignKey -from sqlalchemy.orm import relationship +from sqlalchemy.orm import relationship, backref from sqlalchemy.sql.expression import func from szurubooru.db.base import Base @@ -14,7 +14,9 @@ class CommentScore(Base): score = Column('score', Integer, nullable=False) comment = relationship('Comment') - user = relationship('User') + user = relationship( + 'User', + backref=backref('comment_scores', cascade='all, delete-orphan')) class Comment(Base): __tablename__ = 'comment' diff --git a/server/szurubooru/db/post.py b/server/szurubooru/db/post.py index 18dddf03..0b7ef113 100644 --- a/server/szurubooru/db/post.py +++ b/server/szurubooru/db/post.py @@ -1,6 +1,6 @@ from sqlalchemy import ( Column, Integer, DateTime, Unicode, UnicodeText, PickleType, ForeignKey) -from sqlalchemy.orm import relationship, column_property, object_session +from sqlalchemy.orm import relationship, column_property, object_session, backref from sqlalchemy.sql.expression import func, select from szurubooru.db.base import Base from szurubooru.db.comment import Comment @@ -16,7 +16,9 @@ class PostFeature(Base): time = Column('time', DateTime, nullable=False) post = relationship('Post') - user = relationship('User') + user = relationship( + 'User', + backref=backref('post_features', cascade='all, delete-orphan')) class PostScore(Base): __tablename__ = 'post_score' @@ -29,7 +31,9 @@ class PostScore(Base): score = Column('score', Integer, nullable=False) post = relationship('Post') - user = relationship('User') + user = relationship( + 'User', + backref=backref('post_scores', cascade='all, delete-orphan')) class PostFavorite(Base): __tablename__ = 'post_favorite' @@ -41,7 +45,9 @@ class PostFavorite(Base): time = Column('time', DateTime, nullable=False) post = relationship('Post') - user = relationship('User') + user = relationship( + 'User', + backref=backref('post_favorites', cascade='all, delete-orphan')) class PostNote(Base): __tablename__ = 'post_note' diff --git a/server/szurubooru/db/user.py b/server/szurubooru/db/user.py index 705f0fd6..40b9b06e 100644 --- a/server/szurubooru/db/user.py +++ b/server/szurubooru/db/user.py @@ -1,4 +1,5 @@ from sqlalchemy import Column, Integer, Unicode, DateTime +from sqlalchemy.orm import relationship from sqlalchemy.sql.expression import func from szurubooru.db.base import Base from szurubooru.db.post import Post, PostScore, PostFavorite @@ -29,6 +30,8 @@ class User(Base): avatar_style = Column( 'avatar_style', Unicode(32), nullable=False, default=AVATAR_GRAVATAR) + comments = relationship('Comment') + @property def post_count(self): from szurubooru.db import session diff --git a/server/szurubooru/tests/db/test_user.py b/server/szurubooru/tests/db/test_user.py index d7a2f559..8c4d2d42 100644 --- a/server/szurubooru/tests/db/test_user.py +++ b/server/szurubooru/tests/db/test_user.py @@ -96,3 +96,74 @@ def test_disliked_post_count(user_factory, post_factory): db.session.refresh(user) assert user.liked_post_count == 0 assert user.disliked_post_count == 1 + +def test_cascade_deletions(post_factory, user_factory, comment_factory): + user = user_factory() + + post = post_factory() + post.user = user + + post_score = db.PostScore() + post_score.post = post + post_score.user = user + post_score.time = datetime(1997, 1, 1) + post_score.score = 1 + post.scores.append(post_score) + + post_favorite = db.PostFavorite() + post_favorite.post = post + post_favorite.user = user + post_favorite.time = datetime(1997, 1, 1) + post.favorited_by.append(post_favorite) + + post_feature = db.PostFeature() + post_feature.post = post + post_feature.user = user + post_feature.time = datetime(1997, 1, 1) + post.features.append(post_feature) + + comment = comment_factory(post=post, user=user) + comment_score = db.CommentScore() + comment_score.comment = comment + comment_score.user = user + comment_score.time = datetime(1997, 1, 1) + comment_score.score = 1 + comment.scores.append(comment_score) + + snapshot = db.Snapshot() + snapshot.user = user + snapshot.creation_time = datetime(1997, 1, 1) + snapshot.resource_type = '-' + snapshot.resource_id = '-' + snapshot.resource_repr = '-' + snapshot.operation = '-' + + db.session.add_all([user, post, comment, snapshot]) + db.session.flush() + + assert not db.session.dirty + assert post.user is not None and post.user.user_id is not None + assert db.session.query(db.User).count() == 1 + assert db.session.query(db.Post).count() == 1 + assert db.session.query(db.PostScore).count() == 1 + assert db.session.query(db.PostFeature).count() == 1 + assert db.session.query(db.PostFavorite).count() == 1 + assert db.session.query(db.Comment).count() == 1 + assert db.session.query(db.CommentScore).count() == 1 + assert db.session.query(db.Snapshot).count() == 1 + + db.session.delete(user) + db.session.commit() + + assert not db.session.dirty + assert db.session.query(db.User).count() == 0 + assert db.session.query(db.Post).count() == 1 + assert db.session.query(db.Post)[0].user is None + assert db.session.query(db.PostScore).count() == 0 + assert db.session.query(db.PostFeature).count() == 0 + assert db.session.query(db.PostFavorite).count() == 0 + assert db.session.query(db.Comment).count() == 1 + assert db.session.query(db.Comment)[0].user is None + assert db.session.query(db.CommentScore).count() == 0 + assert db.session.query(db.Snapshot).count() == 1 + assert db.session.query(db.Snapshot)[0].user is None