From 7559305fc98c7aed679065e6175d1e4e30653dd5 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 6 Feb 2026 18:06:35 +0800 Subject: [PATCH] [modify] migration script --- .../versions/bab823f7cc82_202602061730.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/api/migrations/versions/bab823f7cc82_202602061730.py b/api/migrations/versions/bab823f7cc82_202602061730.py index 9e23fc49..efef7d57 100644 --- a/api/migrations/versions/bab823f7cc82_202602061730.py +++ b/api/migrations/versions/bab823f7cc82_202602061730.py @@ -20,20 +20,25 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('model_api_keys_backup_20260123') + # 1. 先添加 memory_config 表的列 + op.add_column('memory_config', sa.Column('is_default', sa.Boolean(), nullable=True, comment='是否为工作空间默认配置')) + + # 2. 再添加 end_users 表的列和外键 op.add_column('end_users', sa.Column('memory_config_id', sa.UUID(), nullable=True, comment='关联的记忆配置ID')) op.create_index(op.f('ix_end_users_memory_config_id'), 'end_users', ['memory_config_id'], unique=False) op.create_foreign_key(None, 'end_users', 'memory_config', ['memory_config_id'], ['config_id']) - op.add_column('memory_config', sa.Column('is_default', sa.Boolean(), nullable=True, comment='是否为工作空间默认配置')) + + # 3. 最后安全删除备份表(如果存在) + conn = op.get_bind() + inspector = sa.inspect(conn) + if 'model_api_keys_backup_20260123' in inspector.get_table_names(): + op.drop_table('model_api_keys_backup_20260123') # ### end Alembic commands ### def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('memory_config', 'is_default') - op.drop_constraint(None, 'end_users', type_='foreignkey') - op.drop_index(op.f('ix_end_users_memory_config_id'), table_name='end_users') - op.drop_column('end_users', 'memory_config_id') + # 1. 先恢复备份表 op.create_table('model_api_keys_backup_20260123', sa.Column('id', sa.UUID(), autoincrement=False, nullable=True), sa.Column('model_name', sa.VARCHAR(), autoincrement=False, nullable=True), @@ -49,4 +54,12 @@ def downgrade() -> None: sa.Column('updated_at', postgresql.TIMESTAMP(precision=6), autoincrement=False, nullable=True), sa.Column('model_config_id', sa.UUID(), autoincrement=False, nullable=True) ) + + # 2. 删除 end_users 的外键和列 + op.drop_constraint(None, 'end_users', type_='foreignkey') + op.drop_index(op.f('ix_end_users_memory_config_id'), table_name='end_users') + op.drop_column('end_users', 'memory_config_id') + + # 3. 删除 memory_config 的列 + op.drop_column('memory_config', 'is_default') # ### end Alembic commands ###