From 14059de1d7393bcac019d28b4783ba12ebe62f39 Mon Sep 17 00:00:00 2001 From: rr- Date: Fri, 22 Apr 2016 18:36:12 +0200 Subject: [PATCH] server/db: add more post tables --- server/szurubooru/db/__init__.py | 15 ++++- server/szurubooru/db/post.py | 52 +++++++++++++++- .../9587de88a84b_create_aux_post_tables.py | 59 +++++++++++++++++++ server/szurubooru/tests/db/test_post.py | 32 ++++++++++ 4 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 server/szurubooru/migrations/versions/9587de88a84b_create_aux_post_tables.py diff --git a/server/szurubooru/db/__init__.py b/server/szurubooru/db/__init__.py index e9119ec2..c08f8b1d 100644 --- a/server/szurubooru/db/__init__.py +++ b/server/szurubooru/db/__init__.py @@ -1,7 +1,18 @@ from szurubooru.db.base import Base from szurubooru.db.user import User -from szurubooru.db.tag import Tag, TagName, TagSuggestion, TagImplication from szurubooru.db.tag_category import TagCategory -from szurubooru.db.post import Post, PostTag, PostRelation +from szurubooru.db.tag import ( + Tag, + TagName, + TagSuggestion, + TagImplication) +from szurubooru.db.post import ( + Post, + PostTag, + PostRelation, + PostFavorite, + PostScore, + PostNote, + PostFeature) from szurubooru.db.snapshot import Snapshot from szurubooru.db.session import session diff --git a/server/szurubooru/db/post.py b/server/szurubooru/db/post.py index 0f617f27..85e4476b 100644 --- a/server/szurubooru/db/post.py +++ b/server/szurubooru/db/post.py @@ -1,8 +1,50 @@ -from sqlalchemy import Column, Integer, DateTime, String, ForeignKey +from sqlalchemy import Column, Integer, DateTime, String, Text, PickleType, ForeignKey from sqlalchemy.orm import relationship, column_property from sqlalchemy.sql.expression import func, select from szurubooru.db.base import Base +class PostFeature(Base): + __tablename__ = 'post_feature' + + post_feature_id = Column('id', Integer, primary_key=True) + post_id = Column('post_id', Integer, ForeignKey('post.id'), nullable=False) + user_id = Column('user_id', Integer, ForeignKey('user.id'), nullable=False) + time = Column('time', DateTime, nullable=False) + + post = relationship('Post') + user = relationship('User') + +class PostScore(Base): + __tablename__ = 'post_score' + + post_id = Column('post_id', Integer, ForeignKey('post.id'), primary_key=True) + user_id = Column('user_id', Integer, ForeignKey('user.id'), primary_key=True) + time = Column('time', DateTime, nullable=False) + score = Column('score', Integer, nullable=False) + + post = relationship('Post') + user = relationship('User') + +class PostFavorite(Base): + __tablename__ = 'post_favorite' + + post_id = Column('post_id', Integer, ForeignKey('post.id'), primary_key=True) + user_id = Column('user_id', Integer, ForeignKey('user.id'), primary_key=True) + time = Column('time', DateTime, nullable=False) + + post = relationship('Post') + user = relationship('User') + +class PostNote(Base): + __tablename__ = 'post_note' + + post_note_id = Column('id', Integer, primary_key=True) + post_id = Column('post_id', Integer, ForeignKey('post.id'), nullable=False) + polygon = Column('polygon', PickleType, nullable=False) + text = Column('text', Text, nullable=False) + + post = relationship('Post') + class PostRelation(Base): __tablename__ = 'post_relation' @@ -56,6 +98,14 @@ class Post(Base): secondary='post_relation', primaryjoin=post_id == PostRelation.parent_id, secondaryjoin=post_id == PostRelation.child_id) + features = relationship( + 'PostFeature', cascade='all, delete-orphan', lazy='joined') + scores = relationship( + 'PostScore', cascade='all, delete-orphan', lazy='joined') + favorites = relationship( + 'PostFavorite', cascade='all, delete-orphan', lazy='joined') + notes = relationship( + 'PostNote', cascade='all, delete-orphan', lazy='joined') tag_count = column_property( select([func.count(PostTag.tag_id)]) \ diff --git a/server/szurubooru/migrations/versions/9587de88a84b_create_aux_post_tables.py b/server/szurubooru/migrations/versions/9587de88a84b_create_aux_post_tables.py new file mode 100644 index 00000000..983ea94f --- /dev/null +++ b/server/szurubooru/migrations/versions/9587de88a84b_create_aux_post_tables.py @@ -0,0 +1,59 @@ +''' +Create auxilliary post tables + +Revision ID: 9587de88a84b +Created at: 2016-04-22 17:42:57.697229 +''' + +import sqlalchemy as sa +from alembic import op + +revision = '9587de88a84b' +down_revision = '46cd5229839b' +branch_labels = None +depends_on = None + +def upgrade(): + op.create_table( + 'post_favorite', + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('time', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id']), + sa.ForeignKeyConstraint(['user_id'], ['user.id']), + sa.PrimaryKeyConstraint('post_id', 'user_id')) + + op.create_table( + 'post_feature', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('time', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id']), + sa.ForeignKeyConstraint(['user_id'], ['user.id']), + sa.PrimaryKeyConstraint('id')) + + op.create_table( + 'post_note', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('text', sa.PickleType(), nullable=False), + sa.Column('polygon', sa.Text(), nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id']), + sa.PrimaryKeyConstraint('id')) + + op.create_table( + 'post_score', + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('time', sa.DateTime(), nullable=False), + sa.Column('score', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id']), + sa.ForeignKeyConstraint(['user_id'], ['user.id']), + sa.PrimaryKeyConstraint('post_id', 'user_id')) + +def downgrade(): + op.drop_table('post_score') + op.drop_table('post_note') + op.drop_table('post_feature') + op.drop_table('post_favorite') diff --git a/server/szurubooru/tests/db/test_post.py b/server/szurubooru/tests/db/test_post.py index 9362d561..acc444f0 100644 --- a/server/szurubooru/tests/db/test_post.py +++ b/server/szurubooru/tests/db/test_post.py @@ -42,11 +42,35 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): db.session.add_all([user, tag1, tag2, post, related_post1, related_post2]) db.session.flush() + score = db.PostScore() + score.post = post + score.user = user + score.time = datetime(1997, 1, 1) + score.score = 1 + favorite = db.PostFavorite() + favorite.post = post + favorite.user = user + favorite.time = datetime(1997, 1, 1) + feature = db.PostFeature() + feature.post = post + feature.user = user + feature.time = datetime(1997, 1, 1) + note = db.PostNote() + note.post = post + note.polygon = '' + note.text = '' + db.session.add_all([score]) + db.session.flush() + post.user = user post.tags.append(tag1) post.tags.append(tag2) post.relations.append(related_post1) post.relations.append(related_post2) + post.scores.append(score) + post.favorites.append(favorite) + post.features.append(feature) + post.notes.append(note) db.session.flush() assert not db.session.dirty @@ -57,6 +81,10 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): assert db.session.query(db.Post).count() == 3 assert db.session.query(db.PostTag).count() == 2 assert db.session.query(db.PostRelation).count() == 2 + assert db.session.query(db.PostScore).count() == 1 + assert db.session.query(db.PostNote).count() == 1 + assert db.session.query(db.PostFeature).count() == 1 + assert db.session.query(db.PostFavorite).count() == 1 db.session.delete(post) db.session.commit() @@ -67,6 +95,10 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): assert db.session.query(db.Post).count() == 2 assert db.session.query(db.PostTag).count() == 0 assert db.session.query(db.PostRelation).count() == 0 + assert db.session.query(db.PostScore).count() == 0 + assert db.session.query(db.PostNote).count() == 0 + assert db.session.query(db.PostFeature).count() == 0 + assert db.session.query(db.PostFavorite).count() == 0 def test_tracking_tag_count(post_factory, tag_factory): post = post_factory()