"""add flight_has_copilot.token Revision ID: 7dd139549851 Revises: a410455fb3bb Create Date: 2024-03-02 15:02:26.594830 """ import random import string from alembic import op import sqlalchemy as sa from sqlalchemy import text # revision identifiers, used by Alembic. revision = '7dd139549851' down_revision = 'a410455fb3bb' branch_labels = None depends_on = None def rand_str(): alphabet = string.ascii_letters + string.digits return ''.join([random.choice(alphabet) for _ in range(64)]) def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.add_column('flight_has_copilot', sa.Column('token', sa.String(length=64), nullable=True)) conn = op.get_bind() data = conn.execute(text("SELECT flight_id, copilot_id FROM flight_has_copilot")) for flight_id, copilot_id in data: print(flight_id, copilot_id) conn.execute(text( f"UPDATE flight_has_copilot SET token='{rand_str()}'" f" WHERE flight_id={flight_id} AND copilot_id={copilot_id}" )) op.create_unique_constraint(None, 'flight_has_copilot', ['token']) op.add_column('photo', sa.Column('created_by_copilot_id', sa.Integer(), nullable=True)) op.create_foreign_key(None, 'photo', 'copilot', ['created_by_copilot_id'], ['id']) # ### end Alembic commands ### def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.drop_constraint('photo_ibfk_5', 'photo', type_='foreignkey') op.drop_column('photo', 'created_by_copilot_id') op.drop_constraint('token', 'flight_has_copilot', type_='unique') op.drop_column('flight_has_copilot', 'token') # ### end Alembic commands ###