Kamixitong/migrations/versions/abcd1234_create_api_tables.py

83 lines
3.2 KiB
Python
Raw Permalink Normal View History

2025-11-15 23:57:24 +08:00
"""create api tables
Revision ID: abcd1234_create_api_tables
Revises: fe6513ff0455
Create Date: 2025-11-15 10:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
2025-12-12 11:35:14 +08:00
from sqlalchemy.engine import reflection
2025-11-15 23:57:24 +08:00
# revision identifiers, used by Alembic.
revision = 'abcd1234_create_api_tables'
down_revision = 'fe6513ff0455'
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)
2025-11-15 23:57:24 +08:00
2025-12-12 11:35:14 +08:00
# 获取现有表
existing_tables = inspector.get_table_names()
2025-11-15 23:57:24 +08:00
2025-12-12 11:35:14 +08:00
# 只有当表不存在时才创建
if 'api' not in existing_tables:
op.create_table('api',
sa.Column('api_id', sa.String(length=32), nullable=False),
sa.Column('api_name', sa.String(length=64), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('status', sa.Integer(), nullable=False),
sa.Column('create_time', sa.DateTime(), nullable=True),
sa.Column('update_time', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('api_id')
)
2025-11-15 23:57:24 +08:00
2025-12-12 11:35:14 +08:00
if 'api_version' not in existing_tables:
op.create_table('api_version',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('version_num', sa.String(length=32), nullable=False),
sa.Column('api_id', sa.String(length=32), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('publish_status', sa.Integer(), nullable=False),
sa.Column('create_time', sa.DateTime(), nullable=True),
sa.Column('update_time', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['api_id'], ['api.api_id'], ),
sa.PrimaryKeyConstraint('id')
)
if 'api_key' not in existing_tables:
op.create_table('api_key',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('key', sa.String(length=64), nullable=False),
sa.Column('api_id', sa.String(length=32), nullable=False),
sa.Column('name', sa.String(length=64), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('status', sa.Integer(), nullable=False),
sa.Column('create_time', sa.DateTime(), nullable=True),
sa.Column('update_time', sa.DateTime(), nullable=True),
sa.Column('expire_time', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['api_id'], ['api.api_id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('key')
)
# 添加索引
op.create_index(op.f('ix_api_key_api_id'), 'api_key', ['api_id'], unique=False)
op.create_index(op.f('ix_api_version_api_id'), 'api_version', ['api_id'], unique=False)
2025-11-15 23:57:24 +08:00
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_api_version_api_id'), table_name='api_version')
op.drop_index(op.f('ix_api_key_api_id'), table_name='api_key')
op.drop_table('api_key')
op.drop_table('api_version')
op.drop_table('api')
# ### end Alembic commands ###