[add] migration script

This commit is contained in:
Mark
2026-03-26 18:34:19 +08:00
parent 7556468c6e
commit 3d291e3c23

View File

@@ -0,0 +1,59 @@
"""202603261815
Revision ID: 1480a7d680fb
Revises: adaefcbe2aa1
Create Date: 2026-03-26 18:16:07.886033
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '1480a7d680fb'
down_revision: Union[str, None] = 'adaefcbe2aa1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('end_user_info',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('end_user_id', sa.UUID(), nullable=False, comment='关联的终端用户ID'),
sa.Column('other_name', sa.String(), nullable=False, comment='关联的用户名称'),
sa.Column('aliases', sa.ARRAY(sa.String()), nullable=True, comment='用户别名列表(字符串数组)'),
sa.Column('meta_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True, comment='用户相关的扩展信息JSON格式'),
sa.Column('created_at', sa.DateTime(), nullable=True, comment='创建时间'),
sa.Column('updated_at', sa.DateTime(), nullable=True, comment='更新时间'),
sa.ForeignKeyConstraint(['end_user_id'], ['end_users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_end_user_info_end_user_id'), 'end_user_info', ['end_user_id'], unique=False)
op.create_index(op.f('ix_end_user_info_id'), 'end_user_info', ['id'], unique=False)
connection = op.get_bind()
connection.execute(sa.text("""
INSERT INTO end_user_info (id, end_user_id, other_name, aliases, meta_data, created_at, updated_at)
SELECT
gen_random_uuid() as id,
id as end_user_id,
other_name,
'{}'::TEXT[] as aliases,
NULL as meta_data,
NOW() as created_at,
NOW() as updated_at
FROM end_users
"""))
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_end_user_info_id'), table_name='end_user_info')
op.drop_index(op.f('ix_end_user_info_end_user_id'), table_name='end_user_info')
op.drop_table('end_user_info')
# ### end Alembic commands ###