diff --git a/files/classes/votes.py b/files/classes/votes.py index 4ec23ddf1..668410a45 100644 --- a/files/classes/votes.py +++ b/files/classes/votes.py @@ -1,7 +1,7 @@ from sqlalchemy import * from sqlalchemy.orm import relationship -from files.classes.base import CreatedBase +from files.classes.base import CreatedBase, CreatedDateTimeBase from files.helpers.lazy import lazy @@ -42,7 +42,7 @@ class Vote(CreatedBase): return data -class CommentVote(CreatedBase): +class CommentVote(CreatedDateTimeBase): __tablename__ = "commentvotes" comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True) @@ -50,7 +50,6 @@ class CommentVote(CreatedBase): vote_type = Column(Integer, nullable=False) app_id = Column(Integer, ForeignKey("oauth_apps.id")) 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('commentvotes_comments_type_index', vote_type) diff --git a/migrations/versions/2023_07_11_03_27_11_5195118d6a51_change_created_utc_to_timestampz_for_.py b/migrations/versions/2023_07_11_03_27_11_5195118d6a51_change_created_utc_to_timestampz_for_.py index 8973758ed..d07b3bd68 100644 --- a/migrations/versions/2023_07_11_03_27_11_5195118d6a51_change_created_utc_to_timestampz_for_.py +++ b/migrations/versions/2023_07_11_03_27_11_5195118d6a51_change_created_utc_to_timestampz_for_.py @@ -7,6 +7,7 @@ Create Date: 2023-07-11 03:27:11.520264+00:00 """ from alembic import op import sqlalchemy as sa +from sqlalchemy.sql.functions import now # revision identifiers, used by Alembic. @@ -15,31 +16,35 @@ down_revision = '7ae4658467d7' branch_labels = None depends_on = None +table_name = 'commentvotes' +from_column = 'created_utc' +to_column = 'created_datetimez' def upgrade(): - op.add_column('commentvotes', sa.Column('created_timestampz', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False)) - op.execute(""" - UPDATE commentvotes - SET created_timestampz = + op.add_column(table_name, sa.Column(to_column, sa.DateTime(timezone=True), server_default=now(), nullable=True)) + op.execute(f""" + UPDATE {table_name} + SET {to_column} = CASE - WHEN created_utc > 0 THEN - (timestamp 'epoch' + created_utc * interval '1 second') at time zone 'utc' + WHEN {from_column} > 0 THEN + (timestamp 'epoch' + {from_column} * interval '1 second') at time zone 'utc' ELSE NULL 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(): - op.add_column('commentvotes', sa.Column('created_utc', sa.INTEGER(), server_default=sa.text('0'), nullable=True)) - op.execute(""" - UPDATE commentvotes - SET created_utc = + op.add_column(table_name, sa.Column(from_column, sa.INTEGER(), server_default=sa.text('0'), nullable=True)) + op.execute(f""" + UPDATE {table_name} + SET {from_column} = COALESCE( - EXTRACT(EPOCH FROM created_timestampz)::integer, + EXTRACT(EPOCH FROM {to_column})::integer, 0 ) """) - op.alter_column('commentvotes', 'created_utc', nullable=False) - op.drop_column('commentvotes', 'created_timestampz') + op.alter_column(table_name, from_column, nullable=False) + op.drop_column(table_name, to_column)