🏦 Database Change: convert created utc to timestamp for commentvotes (#616)

This commit is contained in:
Viet Than 2023-07-28 04:50:11 -05:00 committed by GitHub
parent 48d7f4077d
commit 64880c87fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View file

@ -0,0 +1,50 @@
"""Change created_utc to datetimez for commentvotes
Revision ID: 5195118d6a51
Revises: 7ae4658467d7
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.
revision = '5195118d6a51'
down_revision = 'a23fe2f1515c'
branch_labels = None
depends_on = None
table_name = 'commentvotes'
from_column = 'created_utc'
to_column = 'created_datetimez'
def upgrade():
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 {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():
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 {to_column})::integer,
0
)
""")
op.alter_column(table_name, from_column, nullable=False)
op.drop_column(table_name, to_column)