Skip to content

Commit

Permalink
Add migrations to make DB datetimes tz aware
Browse files Browse the repository at this point in the history
Our models expect the datetimes to be timezone aware, however the
database itself has timezone unaware datetimes. This should reconcile
that by adding a migration to make the database timezones to be datetime
aware.
  • Loading branch information
Robin5605 authored and jonathan-d-zhang committed Aug 4, 2024
1 parent 41dd32d commit 502ec9d
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions alembic/versions/56627251b8c0_make_datetimes_tz_aware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""make datetimes tz aware
Revision ID: 56627251b8c0
Revises: 6991bcb18f89
Create Date: 2024-07-04 14:23:08.370773
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql


# revision identifiers, used by Alembic.
revision = "56627251b8c0"
down_revision = "6991bcb18f89"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.alter_column(
"scans",
"queued_at",
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
postgresql_using="queued_at AS TIME ZONE 'UTC'",
)
op.alter_column(
"scans",
"pending_at",
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
postgresql_using="pending_at AS TIME ZONE 'UTC'",
)
op.alter_column(
"scans",
"finished_at",
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
postgresql_using="finished_at AS TIME ZONE 'UTC'",
)
op.alter_column(
"scans",
"reported_at",
existing_type=postgresql.TIMESTAMP(),
type_=sa.DateTime(timezone=True),
existing_nullable=True,
postgresql_using="reported_at AS TIME ZONE 'UTC'",
)


def downgrade() -> None:
op.alter_column(
"scans",
"reported_at",
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
)
op.alter_column(
"scans",
"finished_at",
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
)
op.alter_column(
"scans",
"pending_at",
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
)
op.alter_column(
"scans",
"queued_at",
existing_type=sa.DateTime(timezone=True),
type_=postgresql.TIMESTAMP(),
existing_nullable=True,
)

0 comments on commit 502ec9d

Please sign in to comment.