doneth.at/migrations/versions/687170684a50_add_created_on...

54 lines
1.5 KiB
Python

"""Add created_on to Accomplishment
Revision ID: 687170684a50
Revises: 517c61e085a2
Create Date: 2020-09-26 17:43:11.471553
"""
from alembic import op
import sqlalchemy as sa
from datetime import datetime, timezone
# revision identifiers, used by Alembic.
revision = '687170684a50'
down_revision = '517c61e085a2'
branch_labels = None
depends_on = None
accomphelper = sa.Table(
'accomplishment',
sa.MetaData(),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('time', sa.DateTime(), nullable=False),
sa.Column('created_on', sa.DateTime(), nullable=True),
)
def upgrade():
with op.batch_alter_table('accomplishment', recreate='always') as batch_op:
batch_op.add_column(
sa.Column(
'created_on', sa.DateTime(),
server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True
))
connection = op.get_bind()
for accomplishment in connection.execute(accomphelper.select()):
t = accomplishment.time
connection.execute(
accomphelper.update().where(
accomphelper.c.id == accomplishment.id
).values(
created_on=accomplishment.time,
time=datetime(t.year, t.month, t.day, tzinfo=timezone.utc)
)
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('accomplishment', 'created_on')
# ### end Alembic commands ###