1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 09:00:00 +00:00

SCHEMA CHANGE: Add comment_count to Torrent

with a helper function on Torrent to update the count.
This commit is contained in:
TheAMM 2017-05-26 16:08:46 +03:00
parent b6ecad898d
commit 6c80557e39
2 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,44 @@
"""Add comment_count to Torrent
Revision ID: 2bceb2cb4d7c
Revises: d0eeb8049623
Create Date: 2017-05-26 15:07:21.114331
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '2bceb2cb4d7c'
down_revision = 'd0eeb8049623'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('nyaa_torrents', sa.Column('comment_count', sa.Integer(), nullable=False))
op.create_index(op.f('ix_nyaa_torrents_comment_count'), 'nyaa_torrents', ['comment_count'], unique=False)
try:
op.add_column('sukebei_torrents', sa.Column('comment_count', sa.Integer(), nullable=False))
op.create_index(op.f('ix_sukebei_torrents_comment_count'), 'sukebei_torrents', ['comment_count'], unique=False)
except (sa.exc.OperationalError, sa.exc.ProgrammingError):
print('Could not upgrade sukebei!')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_nyaa_torrents_comment_count'), table_name='nyaa_torrents')
op.drop_column('nyaa_torrents', 'comment_count')
try:
op.drop_index(op.f('ix_sukebei_torrents_comment_count'), table_name='sukebei_torrents')
op.drop_column('sukebei_torrents', 'comment_count')
except (sa.exc.OperationalError, sa.exc.ProgrammingError):
print('Could not downgrade sukebei!')
# ### end Alembic commands ###

View file

@ -67,6 +67,8 @@ class Torrent(db.Model):
uploader_ip = db.Column(db.Binary(length=16), default=None, nullable=True)
has_torrent = db.Column(db.Boolean, nullable=False, default=False)
comment_count = db.Column(db.Integer, default=0, nullable=False, index=True)
created_time = db.Column(db.DateTime(timezone=False), default=datetime.utcnow, nullable=False)
updated_time = db.Column(db.DateTime(timezone=False),
default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
@ -107,6 +109,10 @@ class Torrent(db.Model):
def __repr__(self):
return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(type(self).__name__, self)
def update_comment_count(self):
self.comment_count = Comment.query.filter_by(torrent_id = self.id).count()
return self.comment_count
@property
def created_utc_timestamp(self):
''' Returns a UTC POSIX timestamp, as seconds '''
@ -149,6 +155,8 @@ class Torrent(db.Model):
if self.uploader_ip:
return str(ip_address(self.uploader_ip))
# Flag getters and setters below
@property
def anonymous(self):
return self.flags & TorrentFlags.ANONYMOUS
@ -197,6 +205,8 @@ class Torrent(db.Model):
def complete(self, value):
self.flags = (self.flags & ~TorrentFlags.COMPLETE) | (value and TorrentFlags.COMPLETE)
# Class methods
@classmethod
def by_id(cls, id):
return cls.query.get(id)