"""add ip_address column to device table Revision ID: 20241119_add_ip_address_to_device Revises: abcd1234 Create Date: 2024-11-19 10:00:00.000000 """ from alembic import op import sqlalchemy as sa from sqlalchemy.engine import reflection # revision identifiers, used by Alembic. revision = '20241119_add_ip_address_to_device' down_revision = 'abcd1234' branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### # 获取数据库连接和表信息 bind = op.get_bind() inspector = reflection.Inspector.from_engine(bind) # 获取device表的现有列 existing_columns = [column['name'] for column in inspector.get_columns('device')] # 只有当列不存在时才添加 if 'ip_address' not in existing_columns: with op.batch_alter_table('device', schema=None) as batch_op: batch_op.add_column(sa.Column('ip_address', sa.String(length=45), nullable=True)) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table('device', schema=None) as batch_op: batch_op.drop_column('ip_address') # ### end Alembic commands ###