server/users: fix cascade deletions

This commit is contained in:
rr- 2016-08-02 11:56:19 +02:00
parent ff189a3915
commit 2429e95102
4 changed files with 88 additions and 6 deletions

View File

@ -1,5 +1,5 @@
from sqlalchemy import Column, Integer, DateTime, UnicodeText, ForeignKey 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 sqlalchemy.sql.expression import func
from szurubooru.db.base import Base from szurubooru.db.base import Base
@ -14,7 +14,9 @@ class CommentScore(Base):
score = Column('score', Integer, nullable=False) score = Column('score', Integer, nullable=False)
comment = relationship('Comment') comment = relationship('Comment')
user = relationship('User') user = relationship(
'User',
backref=backref('comment_scores', cascade='all, delete-orphan'))
class Comment(Base): class Comment(Base):
__tablename__ = 'comment' __tablename__ = 'comment'

View File

@ -1,6 +1,6 @@
from sqlalchemy import ( from sqlalchemy import (
Column, Integer, DateTime, Unicode, UnicodeText, PickleType, ForeignKey) 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 sqlalchemy.sql.expression import func, select
from szurubooru.db.base import Base from szurubooru.db.base import Base
from szurubooru.db.comment import Comment from szurubooru.db.comment import Comment
@ -16,7 +16,9 @@ class PostFeature(Base):
time = Column('time', DateTime, nullable=False) time = Column('time', DateTime, nullable=False)
post = relationship('Post') post = relationship('Post')
user = relationship('User') user = relationship(
'User',
backref=backref('post_features', cascade='all, delete-orphan'))
class PostScore(Base): class PostScore(Base):
__tablename__ = 'post_score' __tablename__ = 'post_score'
@ -29,7 +31,9 @@ class PostScore(Base):
score = Column('score', Integer, nullable=False) score = Column('score', Integer, nullable=False)
post = relationship('Post') post = relationship('Post')
user = relationship('User') user = relationship(
'User',
backref=backref('post_scores', cascade='all, delete-orphan'))
class PostFavorite(Base): class PostFavorite(Base):
__tablename__ = 'post_favorite' __tablename__ = 'post_favorite'
@ -41,7 +45,9 @@ class PostFavorite(Base):
time = Column('time', DateTime, nullable=False) time = Column('time', DateTime, nullable=False)
post = relationship('Post') post = relationship('Post')
user = relationship('User') user = relationship(
'User',
backref=backref('post_favorites', cascade='all, delete-orphan'))
class PostNote(Base): class PostNote(Base):
__tablename__ = 'post_note' __tablename__ = 'post_note'

View File

@ -1,4 +1,5 @@
from sqlalchemy import Column, Integer, Unicode, DateTime from sqlalchemy import Column, Integer, Unicode, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.sql.expression import func from sqlalchemy.sql.expression import func
from szurubooru.db.base import Base from szurubooru.db.base import Base
from szurubooru.db.post import Post, PostScore, PostFavorite from szurubooru.db.post import Post, PostScore, PostFavorite
@ -29,6 +30,8 @@ class User(Base):
avatar_style = Column( avatar_style = Column(
'avatar_style', Unicode(32), nullable=False, default=AVATAR_GRAVATAR) 'avatar_style', Unicode(32), nullable=False, default=AVATAR_GRAVATAR)
comments = relationship('Comment')
@property @property
def post_count(self): def post_count(self):
from szurubooru.db import session from szurubooru.db import session

View File

@ -96,3 +96,74 @@ def test_disliked_post_count(user_factory, post_factory):
db.session.refresh(user) db.session.refresh(user)
assert user.liked_post_count == 0 assert user.liked_post_count == 0
assert user.disliked_post_count == 1 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