🏦 Database Change: convert created utc to datetimez for votes (#670)
This commit is contained in:
parent
53c78c4536
commit
15f387f109
4 changed files with 60 additions and 7 deletions
|
@ -1,11 +1,11 @@
|
|||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from files.classes.base import CreatedBase, CreatedDateTimeBase
|
||||
from files.classes.base import CreatedDateTimeBase
|
||||
from files.helpers.lazy import lazy
|
||||
|
||||
|
||||
class Vote(CreatedBase):
|
||||
class Vote(CreatedDateTimeBase):
|
||||
__tablename__ = "votes"
|
||||
|
||||
submission_id = Column(Integer, ForeignKey("submissions.id"), primary_key=True)
|
||||
|
|
|
@ -55,7 +55,7 @@ def support(v):
|
|||
def participation_stats(v):
|
||||
day = int(time.time()) - 86400
|
||||
|
||||
week = int(time.time()) - 604800
|
||||
week = int(time.time()) - 604800 # TODO themotte#601 use created_datetimez once all is converted
|
||||
posters = g.db.query(Submission.author_id).distinct(Submission.author_id).filter(Submission.created_utc > week).all()
|
||||
commenters = g.db.query(Comment.author_id).distinct(Comment.author_id).filter(Comment.created_utc > week).all()
|
||||
voters = g.db.query(Vote.user_id).distinct(Vote.user_id).filter(Vote.created_utc > week).all()
|
||||
|
|
|
@ -29,18 +29,18 @@ def admin_vote_info_get(v):
|
|||
thing_id = g.db.query(Submission.id).filter_by(upvotes=thing.upvotes, downvotes=thing.downvotes).order_by(Submission.id).first()[0]
|
||||
else: thing_id = thing.id
|
||||
|
||||
ups = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=1).order_by(Vote.created_utc).all()
|
||||
ups = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=1).order_by(Vote.created_datetimez).all()
|
||||
|
||||
downs = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=-1).order_by(Vote.created_utc).all()
|
||||
downs = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=-1).order_by(Vote.created_datetimez).all()
|
||||
|
||||
elif isinstance(thing, Comment):
|
||||
if thing.author.shadowbanned and not (v and v.admin_level):
|
||||
thing_id = g.db.query(Comment.id).filter_by(upvotes=thing.upvotes, downvotes=thing.downvotes).order_by(Comment.id).first()[0]
|
||||
else: thing_id = thing.id
|
||||
|
||||
ups = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=1).order_by(CommentVote.created_utc).all()
|
||||
ups = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=1).order_by(CommentVote.created_datetimez).all()
|
||||
|
||||
downs = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=-1 ).order_by(CommentVote.created_utc).all()
|
||||
downs = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=-1 ).order_by(CommentVote.created_datetimez).all()
|
||||
|
||||
else: abort(400)
|
||||
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
"""Change to datetimez table votes
|
||||
|
||||
Revision ID: 85dad82a4a67
|
||||
Revises: 7f0427506952
|
||||
Create Date: 2023-07-30 02:16:18.963489+00:00
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql.functions import now
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '85dad82a4a67'
|
||||
down_revision = '7f0427506952'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
table_name = 'votes'
|
||||
from_column = 'created_utc'
|
||||
to_column = 'created_datetimez'
|
||||
|
||||
def upgrade():
|
||||
op.add_column(table_name, sa.Column(to_column, sa.DateTime(timezone=True), nullable=True, server_default=now()))
|
||||
op.execute(f"""
|
||||
UPDATE {table_name}
|
||||
SET {to_column} =
|
||||
CASE
|
||||
WHEN {from_column} > 0 THEN
|
||||
(timestamp 'epoch' + {from_column} * interval '1 second') at time zone 'utc'
|
||||
ELSE NULL
|
||||
END
|
||||
""")
|
||||
op.alter_column(table_name, to_column, nullable=False)
|
||||
op.drop_column(table_name, from_column)
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""
|
||||
Downgrade will truncate the milliseconds.
|
||||
"""
|
||||
op.add_column(table_name, sa.Column('created_utc', sa.Integer(), server_default=sa.text('0'), nullable=True))
|
||||
op.execute(f"""
|
||||
UPDATE {table_name}
|
||||
SET created_utc =
|
||||
COALESCE(
|
||||
EXTRACT(EPOCH FROM {to_column})::integer,
|
||||
0
|
||||
)
|
||||
""")
|
||||
op.alter_column(table_name, from_column, nullable=False)
|
||||
op.drop_column(table_name, to_column)
|
Loading…
Add table
Add a link
Reference in a new issue