university-final-iot-backend/migrations/versions/dad1f9b4eec2_.py

52 lines
1.5 KiB
Python
Raw Normal View History

2018-10-29 21:47:03 +00:00
"""empty message
Revision ID: dad1f9b4eec2
Revises: b5eb4a04c77e
Create Date: 2018-10-29 20:48:49.588509
"""
from alembic import op
from secrets import token_urlsafe
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'dad1f9b4eec2'
down_revision = 'b5eb4a04c77e'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('devices', sa.Column('device_secret', sa.String(),
nullable=True))
op.add_column('devices', sa.Column('secret_algorithm', sa.String(),
nullable=True))
devices = sa.sql.table('devices', sa.column('id', sa.Integer),
sa.sql.column('device_secret'),
sa.sql.column('secret_algorithm'))
conn = op.get_bind()
res = conn.execute("select id from devices")
results = res.fetchall()
for result in results:
op.execute(devices.update().where(devices.c.id ==
op.inline_literal(result[0])).
values({'device_secret': token_urlsafe(32),
'secret_algorithm': 'sha512'})
)
# Forbid nulls
op.alter_column('devices', 'device_secret', nullable=False)
op.alter_column('devices', 'secret_algorithm', nullable=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('devices', 'secret_algorithm')
op.drop_column('devices', 'device_secret')
# ### end Alembic commands ###