Kamixitong/migrations/versions/20241119_add_ip_address_to_device.py

39 lines
1.2 KiB
Python
Raw Normal View History

2025-11-19 22:49:24 +08:00
"""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
2025-12-12 11:35:14 +08:00
from sqlalchemy.engine import reflection
2025-11-19 22:49:24 +08:00
# 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! ###
2025-12-12 11:35:14 +08:00
# 获取数据库连接和表信息
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))
2025-11-19 22:49:24 +08:00
# ### 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 ###