server/db: add more post tables

This commit is contained in:
rr- 2016-04-22 18:36:12 +02:00
parent a67a5069b7
commit 14059de1d7
4 changed files with 155 additions and 3 deletions

View file

@ -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

View file

@ -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)]) \

View file

@ -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')

View file

@ -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()