"""change_datetime_in_models Revision ID: bfbe9c9f0423 Revises: 12f2a62e9787 Create Date: 2020-04-03 12:12:04.153323 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. revision = 'bfbe9c9f0423' down_revision = '12f2a62e9787' branch_labels = None depends_on = None def upgrade(): op.drop_column('Campaign', 'latest_end_date') op.drop_column('Campaign', 'earliest_start_date') op.add_column('Activity', sa.Column('temp_start_date', postgresql.TIMESTAMP(timezone=True), server_default=sa.func.current_timestamp(), nullable=False)) op.add_column('Activity', sa.Column('temp_end_date', postgresql.TIMESTAMP(timezone=True), server_default=sa.func.current_timestamp(), nullable=False)) bind = op.get_bind() bind.execute(''' UPDATE "Activity" SET temp_start_date = start_date::timestamp at time zone 'America/New_York' at time zone 'UTC', temp_end_date = end_date::timestamp at time zone 'America/New_York' at time zone 'UTC'; ALTER TABLE "Activity" DROP COLUMN start_date, DROP COLUMN end_date; ''') op.add_column('Activity', sa.Column('start_date', sa.Date(), nullable=True)) op.add_column('Activity', sa.Column('end_date', sa.Date(), nullable=True)) bind.execute(''' UPDATE "Activity" SET start_date = temp_start_date, end_date = temp_end_date; ALTER TABLE "Activity" ALTER COLUMN start_date SET NOT NULL, ALTER COLUMN end_date SET NOT NULL, DROP COLUMN temp_start_date, DROP COLUMN temp_end_date; ''') def downgrade(): op.add_column('Campaign', sa.Column('earliest_start_date', postgresql.TIMESTAMP(timezone=True), nullable=True)) op.add_column('Campaign', sa.Column('latest_end_date', postgresql.TIMESTAMP(timezone=True), nullable=True)) op.add_column('Activity', sa.Column('temp_start_date', postgresql.TIMESTAMP(timezone=True), server_default=sa.func.current_timestamp(), nullable=False)) op.add_column('Activity', sa.Column('temp_end_date', postgresql.TIMESTAMP(timezone=True), server_default=sa.func.current_timestamp(), nullable=False)) bind = op.get_bind() bind.execute(''' UPDATE "Activity" SET temp_start_date = start_date::timestamp at time zone 'UTC', temp_end_date = end_date::timestamp at time zone 'UTC'; ALTER TABLE "Activity" DROP COLUMN start_date, DROP COLUMN end_date; ''') op.add_column('Activity', sa.Column('start_date', postgresql.TIMESTAMP(timezone=True), nullable=True)) op.add_column('Activity', sa.Column('end_date', postgresql.TIMESTAMP(timezone=True), nullable=True)) bind.execute(''' UPDATE "Activity" SET start_date = temp_start_date, end_date = temp_end_date; ALTER TABLE "Activity" ALTER COLUMN start_date SET NOT NULL, ALTER COLUMN end_date SET NOT NULL, DROP COLUMN temp_start_date, DROP COLUMN temp_end_date; ''')