commentvotes

This commit is contained in:
Viet Than 2023-07-20 21:19:27 -04:00
parent b9441ae96d
commit 84df334bb3
2 changed files with 21 additions and 17 deletions

View file

@ -1,7 +1,7 @@
from sqlalchemy import * from sqlalchemy import *
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from files.classes.base import CreatedBase from files.classes.base import CreatedBase, CreatedDateTimeBase
from files.helpers.lazy import lazy from files.helpers.lazy import lazy
@ -42,7 +42,7 @@ class Vote(CreatedBase):
return data return data
class CommentVote(CreatedBase): class CommentVote(CreatedDateTimeBase):
__tablename__ = "commentvotes" __tablename__ = "commentvotes"
comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True) comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True)
@ -50,7 +50,6 @@ class CommentVote(CreatedBase):
vote_type = Column(Integer, nullable=False) vote_type = Column(Integer, nullable=False)
app_id = Column(Integer, ForeignKey("oauth_apps.id")) app_id = Column(Integer, ForeignKey("oauth_apps.id"))
real = Column(Boolean, default=True, nullable=False) real = Column(Boolean, default=True, nullable=False)
created_timestampz = Column(TIMESTAMP(timezone=True), nullable=False, server_default=text("now()"))
Index('cvote_user_index', user_id) Index('cvote_user_index', user_id)
Index('commentvotes_comments_type_index', vote_type) Index('commentvotes_comments_type_index', vote_type)

View file

@ -7,6 +7,7 @@ Create Date: 2023-07-11 03:27:11.520264+00:00
""" """
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy.sql.functions import now
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
@ -15,31 +16,35 @@ down_revision = '7ae4658467d7'
branch_labels = None branch_labels = None
depends_on = None depends_on = None
table_name = 'commentvotes'
from_column = 'created_utc'
to_column = 'created_datetimez'
def upgrade(): def upgrade():
op.add_column('commentvotes', sa.Column('created_timestampz', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False)) op.add_column(table_name, sa.Column(to_column, sa.DateTime(timezone=True), server_default=now(), nullable=True))
op.execute(""" op.execute(f"""
UPDATE commentvotes UPDATE {table_name}
SET created_timestampz = SET {to_column} =
CASE CASE
WHEN created_utc > 0 THEN WHEN {from_column} > 0 THEN
(timestamp 'epoch' + created_utc * interval '1 second') at time zone 'utc' (timestamp 'epoch' + {from_column} * interval '1 second') at time zone 'utc'
ELSE NULL ELSE NULL
END END
""") """)
op.drop_column('commentvotes', 'created_utc') op.alter_column(table_name, to_column, nullable=False)
op.drop_column(table_name, from_column)
def downgrade(): def downgrade():
op.add_column('commentvotes', sa.Column('created_utc', sa.INTEGER(), server_default=sa.text('0'), nullable=True)) op.add_column(table_name, sa.Column(from_column, sa.INTEGER(), server_default=sa.text('0'), nullable=True))
op.execute(""" op.execute(f"""
UPDATE commentvotes UPDATE {table_name}
SET created_utc = SET {from_column} =
COALESCE( COALESCE(
EXTRACT(EPOCH FROM created_timestampz)::integer, EXTRACT(EPOCH FROM {to_column})::integer,
0 0
) )
""") """)
op.alter_column('commentvotes', 'created_utc', nullable=False) op.alter_column(table_name, from_column, nullable=False)
op.drop_column('commentvotes', 'created_timestampz') op.drop_column(table_name, to_column)