🏦 Database Change: convert created utc to datetimez for flags (#633)
This commit is contained in:
parent
2d6c844693
commit
eb78ec6169
3 changed files with 53 additions and 2 deletions
|
@ -6,7 +6,7 @@ from files.helpers.config.const import *
|
|||
|
||||
|
||||
|
||||
class Flag(CreatedBase):
|
||||
class Flag(CreatedDateTimeBase):
|
||||
__tablename__ = "flags"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
|
|
|
@ -141,7 +141,7 @@ class Submission(CreatedBase):
|
|||
|
||||
@lazy
|
||||
def flags(self, v):
|
||||
flags = g.db.query(Flag).filter_by(post_id=self.id).order_by(Flag.created_utc).all()
|
||||
flags = g.db.query(Flag).filter_by(post_id=self.id).order_by(Flag.created_datetimez).all()
|
||||
if not (v and (v.shadowbanned or v.admin_level >= 3)):
|
||||
for flag in flags:
|
||||
if flag.user.shadowbanned:
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
"""Change created_utc to datetimez for flags
|
||||
|
||||
Revision ID: bdb4b7a2e88b
|
||||
Revises: 55488ee27b00
|
||||
Create Date: 2023-07-28 05:33:16.984823+00:00
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql.functions import now
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'bdb4b7a2e88b'
|
||||
down_revision = '55488ee27b00'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
table_name = 'flags'
|
||||
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)
|
Loading…
Add table
Add a link
Reference in a new issue