server/model: use new sqlalchemy import style

This commit is contained in:
rr- 2017-02-05 23:19:05 +01:00
parent f40a8875c4
commit 49e5975254
5 changed files with 227 additions and 198 deletions

View file

@ -1,6 +1,4 @@
from sqlalchemy import Column, Integer, DateTime, UnicodeText, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.sql.expression import func
import sqlalchemy as sa
from szurubooru.db import get_session
from szurubooru.model.base import Base
@ -8,51 +6,59 @@ from szurubooru.model.base import Base
class CommentScore(Base):
__tablename__ = 'comment_score'
comment_id = Column(
comment_id = sa.Column(
'comment_id',
Integer,
ForeignKey('comment.id'),
sa.Integer,
sa.ForeignKey('comment.id'),
nullable=False,
primary_key=True)
user_id = Column(
user_id = sa.Column(
'user_id',
Integer,
ForeignKey('user.id'),
sa.Integer,
sa.ForeignKey('user.id'),
nullable=False,
primary_key=True,
index=True)
time = Column('time', DateTime, nullable=False)
score = Column('score', Integer, nullable=False)
time = sa.Column('time', sa.DateTime, nullable=False)
score = sa.Column('score', sa.Integer, nullable=False)
comment = relationship('Comment')
user = relationship(
comment = sa.orm.relationship('Comment')
user = sa.orm.relationship(
'User',
backref=backref('comment_scores', cascade='all, delete-orphan'))
backref=sa.orm.backref('comment_scores', cascade='all, delete-orphan'))
class Comment(Base):
__tablename__ = 'comment'
comment_id = Column('id', Integer, primary_key=True)
post_id = Column(
'post_id', Integer, ForeignKey('post.id'), nullable=False, index=True)
user_id = Column(
'user_id', Integer, ForeignKey('user.id'), nullable=True, index=True)
version = Column('version', Integer, default=1, nullable=False)
creation_time = Column('creation_time', DateTime, nullable=False)
last_edit_time = Column('last_edit_time', DateTime)
text = Column('text', UnicodeText, default=None)
comment_id = sa.Column('id', sa.Integer, primary_key=True)
post_id = sa.Column(
'post_id',
sa.Integer,
sa.ForeignKey('post.id'),
nullable=False,
index=True)
user_id = sa.Column(
'user_id',
sa.Integer,
sa.ForeignKey('user.id'),
nullable=True,
index=True)
version = sa.Column('version', sa.Integer, default=1, nullable=False)
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
last_edit_time = sa.Column('last_edit_time', sa.DateTime)
text = sa.Column('text', sa.UnicodeText, default=None)
user = relationship('User')
post = relationship('Post')
scores = relationship(
user = sa.orm.relationship('User')
post = sa.orm.relationship('Post')
scores = sa.orm.relationship(
'CommentScore', cascade='all, delete-orphan', lazy='joined')
@property
def score(self) -> int:
return (
get_session()
.query(func.sum(CommentScore.score))
.query(sa.sql.expression.func.sum(CommentScore.score))
.filter(CommentScore.comment_id == self.comment_id)
.one()[0] or 0)

View file

@ -1,8 +1,4 @@
from sqlalchemy.sql.expression import func, select
from sqlalchemy import (
Column, Integer, DateTime, Unicode, UnicodeText, PickleType, ForeignKey)
from sqlalchemy.orm import (
relationship, column_property, object_session, backref)
import sqlalchemy as sa
from szurubooru.model.base import Base
from szurubooru.model.comment import Comment
@ -10,95 +6,109 @@ from szurubooru.model.comment import Comment
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, index=True)
user_id = Column(
'user_id', Integer, ForeignKey('user.id'), nullable=False, index=True)
time = Column('time', DateTime, nullable=False)
post_feature_id = sa.Column('id', sa.Integer, primary_key=True)
post_id = sa.Column(
'post_id',
sa.Integer,
sa.ForeignKey('post.id'),
nullable=False,
index=True)
user_id = sa.Column(
'user_id',
sa.Integer,
sa.ForeignKey('user.id'),
nullable=False,
index=True)
time = sa.Column('time', sa.DateTime, nullable=False)
post = relationship('Post') # type: Post
user = relationship(
'User', backref=backref('post_features', cascade='all, delete-orphan'))
post = sa.orm.relationship('Post') # type: Post
user = sa.orm.relationship(
'User',
backref=sa.orm.backref(
'post_features', cascade='all, delete-orphan'))
class PostScore(Base):
__tablename__ = 'post_score'
post_id = Column(
post_id = sa.Column(
'post_id',
Integer,
ForeignKey('post.id'),
sa.Integer,
sa.ForeignKey('post.id'),
primary_key=True,
nullable=False,
index=True)
user_id = Column(
user_id = sa.Column(
'user_id',
Integer,
ForeignKey('user.id'),
sa.Integer,
sa.ForeignKey('user.id'),
primary_key=True,
nullable=False,
index=True)
time = Column('time', DateTime, nullable=False)
score = Column('score', Integer, nullable=False)
time = sa.Column('time', sa.DateTime, nullable=False)
score = sa.Column('score', sa.Integer, nullable=False)
post = relationship('Post')
user = relationship(
post = sa.orm.relationship('Post')
user = sa.orm.relationship(
'User',
backref=backref('post_scores', cascade='all, delete-orphan'))
backref=sa.orm.backref('post_scores', cascade='all, delete-orphan'))
class PostFavorite(Base):
__tablename__ = 'post_favorite'
post_id = Column(
post_id = sa.Column(
'post_id',
Integer,
ForeignKey('post.id'),
sa.Integer,
sa.ForeignKey('post.id'),
primary_key=True,
nullable=False,
index=True)
user_id = Column(
user_id = sa.Column(
'user_id',
Integer,
ForeignKey('user.id'),
sa.Integer,
sa.ForeignKey('user.id'),
primary_key=True,
nullable=False,
index=True)
time = Column('time', DateTime, nullable=False)
time = sa.Column('time', sa.DateTime, nullable=False)
post = relationship('Post')
user = relationship(
post = sa.orm.relationship('Post')
user = sa.orm.relationship(
'User',
backref=backref('post_favorites', cascade='all, delete-orphan'))
backref=sa.orm.backref('post_favorites', cascade='all, delete-orphan'))
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, index=True)
polygon = Column('polygon', PickleType, nullable=False)
text = Column('text', UnicodeText, nullable=False)
post_note_id = sa.Column('id', sa.Integer, primary_key=True)
post_id = sa.Column(
'post_id',
sa.Integer,
sa.ForeignKey('post.id'),
nullable=False,
index=True)
polygon = sa.Column('polygon', sa.PickleType, nullable=False)
text = sa.Column('text', sa.UnicodeText, nullable=False)
post = relationship('Post')
post = sa.orm.relationship('Post')
class PostRelation(Base):
__tablename__ = 'post_relation'
parent_id = Column(
parent_id = sa.Column(
'parent_id',
Integer,
ForeignKey('post.id'),
sa.Integer,
sa.ForeignKey('post.id'),
primary_key=True,
nullable=False,
index=True)
child_id = Column(
child_id = sa.Column(
'child_id',
Integer,
ForeignKey('post.id'),
sa.Integer,
sa.ForeignKey('post.id'),
primary_key=True,
nullable=False,
index=True)
@ -111,17 +121,17 @@ class PostRelation(Base):
class PostTag(Base):
__tablename__ = 'post_tag'
post_id = Column(
post_id = sa.Column(
'post_id',
Integer,
ForeignKey('post.id'),
sa.Integer,
sa.ForeignKey('post.id'),
primary_key=True,
nullable=False,
index=True)
tag_id = Column(
tag_id = sa.Column(
'tag_id',
Integer,
ForeignKey('tag.id'),
sa.Integer,
sa.ForeignKey('tag.id'),
primary_key=True,
nullable=False,
index=True)
@ -146,111 +156,123 @@ class Post(Base):
FLAG_LOOP = 'loop'
# basic meta
post_id = Column('id', Integer, primary_key=True)
user_id = Column(
post_id = sa.Column('id', sa.Integer, primary_key=True)
user_id = sa.Column(
'user_id',
Integer,
ForeignKey('user.id', ondelete='SET NULL'),
sa.Integer,
sa.ForeignKey('user.id', ondelete='SET NULL'),
nullable=True,
index=True)
version = Column('version', Integer, default=1, nullable=False)
creation_time = Column('creation_time', DateTime, nullable=False)
last_edit_time = Column('last_edit_time', DateTime)
safety = Column('safety', Unicode(32), nullable=False)
source = Column('source', Unicode(200))
flags = Column('flags', PickleType, default=None)
version = sa.Column('version', sa.Integer, default=1, nullable=False)
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
last_edit_time = sa.Column('last_edit_time', sa.DateTime)
safety = sa.Column('safety', sa.Unicode(32), nullable=False)
source = sa.Column('source', sa.Unicode(200))
flags = sa.Column('flags', sa.PickleType, default=None)
# content description
type = Column('type', Unicode(32), nullable=False)
checksum = Column('checksum', Unicode(64), nullable=False)
file_size = Column('file_size', Integer)
canvas_width = Column('image_width', Integer)
canvas_height = Column('image_height', Integer)
mime_type = Column('mime-type', Unicode(32), nullable=False)
type = sa.Column('type', sa.Unicode(32), nullable=False)
checksum = sa.Column('checksum', sa.Unicode(64), nullable=False)
file_size = sa.Column('file_size', sa.Integer)
canvas_width = sa.Column('image_width', sa.Integer)
canvas_height = sa.Column('image_height', sa.Integer)
mime_type = sa.Column('mime-type', sa.Unicode(32), nullable=False)
# foreign tables
user = relationship('User')
tags = relationship('Tag', backref='posts', secondary='post_tag')
relations = relationship(
user = sa.orm.relationship('User')
tags = sa.orm.relationship('Tag', backref='posts', secondary='post_tag')
relations = sa.orm.relationship(
'Post',
secondary='post_relation',
primaryjoin=post_id == PostRelation.parent_id,
secondaryjoin=post_id == PostRelation.child_id, lazy='joined',
backref='related_by')
features = relationship(
features = sa.orm.relationship(
'PostFeature', cascade='all, delete-orphan', lazy='joined')
scores = relationship(
scores = sa.orm.relationship(
'PostScore', cascade='all, delete-orphan', lazy='joined')
favorited_by = relationship(
favorited_by = sa.orm.relationship(
'PostFavorite', cascade='all, delete-orphan', lazy='joined')
notes = relationship(
notes = sa.orm.relationship(
'PostNote', cascade='all, delete-orphan', lazy='joined')
comments = relationship('Comment', cascade='all, delete-orphan')
comments = sa.orm.relationship('Comment', cascade='all, delete-orphan')
# dynamic columns
tag_count = column_property(
select([func.count(PostTag.tag_id)])
tag_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(PostTag.tag_id)])
.where(PostTag.post_id == post_id)
.correlate_except(PostTag))
canvas_area = column_property(canvas_width * canvas_height)
canvas_aspect_ratio = column_property(canvas_width / canvas_height)
canvas_area = sa.orm.column_property(canvas_width * canvas_height)
canvas_aspect_ratio = sa.orm.column_property(canvas_width / canvas_height)
@property
def is_featured(self) -> bool:
featured_post = object_session(self) \
featured_post = sa.orm.object_session(self) \
.query(PostFeature) \
.order_by(PostFeature.time.desc()) \
.first()
return featured_post and featured_post.post_id == self.post_id
score = column_property(
select([func.coalesce(func.sum(PostScore.score), 0)])
score = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.coalesce(
sa.sql.expression.func.sum(PostScore.score), 0)])
.where(PostScore.post_id == post_id)
.correlate_except(PostScore))
favorite_count = column_property(
select([func.count(PostFavorite.post_id)])
favorite_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(PostFavorite.post_id)])
.where(PostFavorite.post_id == post_id)
.correlate_except(PostFavorite))
last_favorite_time = column_property(
select([func.max(PostFavorite.time)])
last_favorite_time = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.max(PostFavorite.time)])
.where(PostFavorite.post_id == post_id)
.correlate_except(PostFavorite))
feature_count = column_property(
select([func.count(PostFeature.post_id)])
feature_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(PostFeature.post_id)])
.where(PostFeature.post_id == post_id)
.correlate_except(PostFeature))
last_feature_time = column_property(
select([func.max(PostFeature.time)])
last_feature_time = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.max(PostFeature.time)])
.where(PostFeature.post_id == post_id)
.correlate_except(PostFeature))
comment_count = column_property(
select([func.count(Comment.post_id)])
comment_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(Comment.post_id)])
.where(Comment.post_id == post_id)
.correlate_except(Comment))
last_comment_creation_time = column_property(
select([func.max(Comment.creation_time)])
last_comment_creation_time = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.max(Comment.creation_time)])
.where(Comment.post_id == post_id)
.correlate_except(Comment))
last_comment_edit_time = column_property(
select([func.max(Comment.last_edit_time)])
last_comment_edit_time = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.max(Comment.last_edit_time)])
.where(Comment.post_id == post_id)
.correlate_except(Comment))
note_count = column_property(
select([func.count(PostNote.post_id)])
note_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(PostNote.post_id)])
.where(PostNote.post_id == post_id)
.correlate_except(PostNote))
relation_count = column_property(
select([func.count(PostRelation.child_id)])
relation_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(PostRelation.child_id)])
.where(
(PostRelation.parent_id == post_id) |
(PostRelation.child_id == post_id))

View file

@ -1,6 +1,4 @@
from sqlalchemy.orm import relationship
from sqlalchemy import (
Column, Integer, DateTime, Unicode, PickleType, ForeignKey)
import sqlalchemy as sa
from szurubooru.model.base import Base
@ -12,20 +10,20 @@ class Snapshot(Base):
OPERATION_DELETED = 'deleted'
OPERATION_MERGED = 'merged'
snapshot_id = Column('id', Integer, primary_key=True)
creation_time = Column('creation_time', DateTime, nullable=False)
operation = Column('operation', Unicode(16), nullable=False)
resource_type = Column(
'resource_type', Unicode(32), nullable=False, index=True)
resource_pkey = Column(
'resource_pkey', Integer, nullable=False, index=True)
resource_name = Column(
'resource_name', Unicode(64), nullable=False)
user_id = Column(
snapshot_id = sa.Column('id', sa.Integer, primary_key=True)
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
operation = sa.Column('operation', sa.Unicode(16), nullable=False)
resource_type = sa.Column(
'resource_type', sa.Unicode(32), nullable=False, index=True)
resource_pkey = sa.Column(
'resource_pkey', sa.Integer, nullable=False, index=True)
resource_name = sa.Column(
'resource_name', sa.Unicode(64), nullable=False)
user_id = sa.Column(
'user_id',
Integer,
ForeignKey('user.id', ondelete='set null'),
sa.Integer,
sa.ForeignKey('user.id', ondelete='set null'),
nullable=True)
data = Column('data', PickleType)
data = sa.Column('data', sa.PickleType)
user = relationship('User')
user = sa.orm.relationship('User')

View file

@ -1,7 +1,4 @@
from sqlalchemy import (
Column, Integer, DateTime, Unicode, UnicodeText, ForeignKey)
from sqlalchemy.orm import relationship, column_property
from sqlalchemy.sql.expression import func, select
import sqlalchemy as sa
from szurubooru.model.base import Base
from szurubooru.model.post import PostTag
@ -9,17 +6,17 @@ from szurubooru.model.post import PostTag
class TagSuggestion(Base):
__tablename__ = 'tag_suggestion'
parent_id = Column(
parent_id = sa.Column(
'parent_id',
Integer,
ForeignKey('tag.id'),
sa.Integer,
sa.ForeignKey('tag.id'),
nullable=False,
primary_key=True,
index=True)
child_id = Column(
child_id = sa.Column(
'child_id',
Integer,
ForeignKey('tag.id'),
sa.Integer,
sa.ForeignKey('tag.id'),
nullable=False,
primary_key=True,
index=True)
@ -32,17 +29,17 @@ class TagSuggestion(Base):
class TagImplication(Base):
__tablename__ = 'tag_implication'
parent_id = Column(
parent_id = sa.Column(
'parent_id',
Integer,
ForeignKey('tag.id'),
sa.Integer,
sa.ForeignKey('tag.id'),
nullable=False,
primary_key=True,
index=True)
child_id = Column(
child_id = sa.Column(
'child_id',
Integer,
ForeignKey('tag.id'),
sa.Integer,
sa.ForeignKey('tag.id'),
nullable=False,
primary_key=True,
index=True)
@ -55,11 +52,15 @@ class TagImplication(Base):
class TagName(Base):
__tablename__ = 'tag_name'
tag_name_id = Column('tag_name_id', Integer, primary_key=True)
tag_id = Column(
'tag_id', Integer, ForeignKey('tag.id'), nullable=False, index=True)
name = Column('name', Unicode(64), nullable=False, unique=True)
order = Column('ord', Integer, nullable=False, index=True)
tag_name_id = sa.Column('tag_name_id', sa.Integer, primary_key=True)
tag_id = sa.Column(
'tag_id',
sa.Integer,
sa.ForeignKey('tag.id'),
nullable=False,
index=True)
name = sa.Column('name', sa.Unicode(64), nullable=False, unique=True)
order = sa.Column('ord', sa.Integer, nullable=False, index=True)
def __init__(self, name: str, order: int) -> None:
self.name = name
@ -69,45 +70,46 @@ class TagName(Base):
class Tag(Base):
__tablename__ = 'tag'
tag_id = Column('id', Integer, primary_key=True)
category_id = Column(
tag_id = sa.Column('id', sa.Integer, primary_key=True)
category_id = sa.Column(
'category_id',
Integer,
ForeignKey('tag_category.id'),
sa.Integer,
sa.ForeignKey('tag_category.id'),
nullable=False,
index=True)
version = Column('version', Integer, default=1, nullable=False)
creation_time = Column('creation_time', DateTime, nullable=False)
last_edit_time = Column('last_edit_time', DateTime)
description = Column('description', UnicodeText, default=None)
version = sa.Column('version', sa.Integer, default=1, nullable=False)
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
last_edit_time = sa.Column('last_edit_time', sa.DateTime)
description = sa.Column('description', sa.UnicodeText, default=None)
category = relationship('TagCategory', lazy='joined')
names = relationship(
category = sa.orm.relationship('TagCategory', lazy='joined')
names = sa.orm.relationship(
'TagName',
cascade='all,delete-orphan',
lazy='joined',
order_by='TagName.order')
suggestions = relationship(
suggestions = sa.orm.relationship(
'Tag',
secondary='tag_suggestion',
primaryjoin=tag_id == TagSuggestion.parent_id,
secondaryjoin=tag_id == TagSuggestion.child_id,
lazy='joined')
implications = relationship(
implications = sa.orm.relationship(
'Tag',
secondary='tag_implication',
primaryjoin=tag_id == TagImplication.parent_id,
secondaryjoin=tag_id == TagImplication.child_id,
lazy='joined')
post_count = column_property(
select([func.count(PostTag.post_id)])
post_count = sa.orm.column_property(
sa.sql.expression.select(
[sa.sql.expression.func.count(PostTag.post_id)])
.where(PostTag.tag_id == tag_id)
.correlate_except(PostTag))
first_name = column_property(
first_name = sa.orm.column_property(
(
select([TagName.name])
sa.sql.expression.select([TagName.name])
.where(TagName.tag_id == tag_id)
.order_by(TagName.order)
.limit(1)
@ -115,17 +117,19 @@ class Tag(Base):
),
deferred=True)
suggestion_count = column_property(
suggestion_count = sa.orm.column_property(
(
select([func.count(TagSuggestion.child_id)])
sa.sql.expression.select(
[sa.sql.expression.func.count(TagSuggestion.child_id)])
.where(TagSuggestion.parent_id == tag_id)
.as_scalar()
),
deferred=True)
implication_count = column_property(
implication_count = sa.orm.column_property(
(
select([func.count(TagImplication.child_id)])
sa.sql.expression.select(
[sa.sql.expression.func.count(TagImplication.child_id)])
.where(TagImplication.parent_id == tag_id)
.as_scalar()
),

View file

@ -1,7 +1,5 @@
from typing import Optional
from sqlalchemy import Column, Integer, Unicode, Boolean, table
from sqlalchemy.orm import column_property
from sqlalchemy.sql.expression import func, select
import sqlalchemy as sa
from szurubooru.model.base import Base
from szurubooru.model.tag import Tag
@ -9,19 +7,20 @@ from szurubooru.model.tag import Tag
class TagCategory(Base):
__tablename__ = 'tag_category'
tag_category_id = Column('id', Integer, primary_key=True)
version = Column('version', Integer, default=1, nullable=False)
name = Column('name', Unicode(32), nullable=False)
color = Column('color', Unicode(32), nullable=False, default='#000000')
default = Column('default', Boolean, nullable=False, default=False)
tag_category_id = sa.Column('id', sa.Integer, primary_key=True)
version = sa.Column('version', sa.Integer, default=1, nullable=False)
name = sa.Column('name', sa.Unicode(32), nullable=False)
color = sa.Column(
'color', sa.Unicode(32), nullable=False, default='#000000')
default = sa.Column('default', sa.Boolean, nullable=False, default=False)
def __init__(self, name: Optional[str]=None) -> None:
self.name = name
tag_count = column_property(
select([func.count('Tag.tag_id')])
tag_count = sa.orm.column_property(
sa.sql.expression.select([sa.sql.expression.func.count('Tag.tag_id')])
.where(Tag.category_id == tag_category_id)
.correlate_except(table('Tag')))
.correlate_except(sa.table('Tag')))
__mapper_args__ = {
'version_id_col': version,