Files
MemoryBear/api/migrations/versions/325b759cd66b_2026011240.py
Ke Sun e7370489e8 Release/v0.2.2 (#260)
* [modify] migration script

* [add] migration script

* fix(web): change form message

* fix(web): the memoryContent field is compatible with numbers and strings

* feat(web): code node hidden

* fix(model):
1. create a basic model to check if the name and provider are duplicated.
2. The result shows error models because the provider created API Keys for all matching models.

---------

Co-authored-by: Mark <zhuwenhui5566@163.com>
Co-authored-by: zhaoying <yzhao96@best-inc.com>
Co-authored-by: yingzhao <zhaoyingyz@126.com>
Co-authored-by: Timebomb2018 <18868801967@163.com>
2026-01-30 15:10:22 +08:00

62 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""2026011240
Revision ID: 325b759cd66b
Revises: 9a936a9ebb20
Create Date: 2026-01-26 12:37:35.946749
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = '325b759cd66b'
down_revision: Union[str, None] = '9a936a9ebb20'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# 1. 重命名表 data_config -> memory_config
op.rename_table('data_config', 'memory_config')
# 2. 重命名列 group_id -> end_user_id
op.alter_column('memory_config', 'group_id', new_column_name='end_user_id')
# 3. config_id: INTEGER -> UUID保留旧值以便回滚
op.drop_constraint('data_config_pkey', 'memory_config', type_='primary')
op.alter_column('memory_config', 'config_id', new_column_name='config_id_old', nullable=True)
op.add_column('memory_config', sa.Column('config_id', sa.UUID(), nullable=True))
# Handle rows where apply_id might be NULL or invalid - generate new UUIDs for those
op.execute("""
UPDATE memory_config
SET config_id = CASE
WHEN apply_id IS NOT NULL AND apply_id ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
THEN apply_id::uuid
ELSE gen_random_uuid()
END
""")
op.alter_column('memory_config', 'config_id', nullable=False)
op.create_primary_key('memory_config_pkey', 'memory_config', ['config_id'])
op.execute("ALTER TABLE memory_config ALTER COLUMN config_id_old DROP DEFAULT")
op.execute("DROP SEQUENCE IF EXISTS data_config_config_id_seq")
def downgrade() -> None:
# 1. config_id: UUID -> INTEGER恢复旧值空值生成新ID
op.execute("CREATE SEQUENCE IF NOT EXISTS data_config_config_id_seq")
op.execute("UPDATE memory_config SET config_id_old = nextval('data_config_config_id_seq') WHERE config_id_old IS NULL")
op.drop_constraint('memory_config_pkey', 'memory_config', type_='primary')
op.drop_column('memory_config', 'config_id')
op.alter_column('memory_config', 'config_id_old', new_column_name='config_id', nullable=False)
op.create_primary_key('data_config_pkey', 'memory_config', ['config_id'])
op.execute("ALTER SEQUENCE data_config_config_id_seq OWNED BY memory_config.config_id")
op.execute("SELECT setval('data_config_config_id_seq', COALESCE((SELECT MAX(config_id) FROM memory_config), 1))")
# 2. 重命名列 end_user_id -> group_id
op.alter_column('memory_config', 'end_user_id', new_column_name='group_id')
# 3. 重命名表 memory_config -> data_config
op.rename_table('memory_config', 'data_config')