🏦 Database Change: convert created utc to datetimez for notifications (#668)

This commit is contained in:
Viet Than 2023-08-02 21:35:05 -05:00 committed by GitHub
parent 34b328583c
commit e1075eb722
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 5 deletions

View file

@ -1,8 +1,8 @@
from sqlalchemy import *
from sqlalchemy.orm import relationship
from files.classes.base import CreatedBase
from files.classes.base import CreatedDateTimeBase
class Notification(CreatedBase):
class Notification(CreatedDateTimeBase):
__tablename__ = "notifications"
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)

View file

@ -30,7 +30,7 @@ def unread(v):
Comment.state_mod == StateMod.VISIBLE,
Comment.state_user_deleted_utc == None,
Comment.author_id != AUTOJANNY_ID,
).order_by(Notification.created_utc.desc()).all()
).order_by(Notification.created_datetimez.desc()).all()
for n, c in listing:
n.read = True
@ -52,7 +52,7 @@ def notifications_main(v: User):
Comment.state_mod == StateMod.VISIBLE,
Comment.state_user_deleted_utc == None,
Comment.author_id != AUTOJANNY_ID,
).order_by(Notification.created_utc.desc()))
).order_by(Notification.created_datetimez.desc()))
if not v.shadowbanned and v.admin_level < 3:
comments = comments.join(Comment.author).filter(User.shadowbanned == None)
@ -96,7 +96,7 @@ def notifications_posts(v: User):
notifications = (g.db.query(Notification, Comment)
.join(Comment, Notification.comment_id == Comment.id)
.filter(Notification.user_id == v.id, Comment.author_id == AUTOJANNY_ID)
.order_by(Notification.created_utc.desc()).offset(25 * (page - 1)).limit(101).all())
.order_by(Notification.created_datetimez.desc()).offset(25 * (page - 1)).limit(101).all())
listing = []

View file

@ -0,0 +1,52 @@
"""Change to datetimez table notifications
Revision ID: 4376dcec1ad8
Revises: 84e02ade75a8
Create Date: 2023-07-30 01:49:16.238320+00:00
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql.functions import now
# revision identifiers, used by Alembic.
revision = '4376dcec1ad8'
down_revision = '84e02ade75a8'
branch_labels = None
depends_on = None
table_name = 'notifications'
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)