[add] migration script
This commit is contained in:
50
api/migrations/versions/12114b3e953c_202603131647.py
Normal file
50
api/migrations/versions/12114b3e953c_202603131647.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
"""202603131647
|
||||||
|
|
||||||
|
Revision ID: 12114b3e953c
|
||||||
|
Revises: cd3a402c2f6c
|
||||||
|
Create Date: 2026-03-13 08:47:30.455956
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = '12114b3e953c'
|
||||||
|
down_revision: Union[str, None] = 'ef9d172cb753'
|
||||||
|
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! ###
|
||||||
|
conn = op.get_bind()
|
||||||
|
print("Step 1: 添加 workspace_id 列...")
|
||||||
|
op.add_column('end_users', sa.Column('workspace_id', sa.UUID(), nullable=True))
|
||||||
|
print("Step 2: 回填 workspace_id...")
|
||||||
|
conn.execute(text("""
|
||||||
|
UPDATE end_users
|
||||||
|
SET workspace_id = apps.workspace_id
|
||||||
|
FROM apps
|
||||||
|
WHERE end_users.app_id = apps.id
|
||||||
|
"""))
|
||||||
|
# Step 3: 设置 workspace_id 为 NOT NULL
|
||||||
|
print("Step 3: 设置 workspace_id 为 NOT NULL...")
|
||||||
|
op.alter_column('end_users', 'workspace_id', nullable=False)
|
||||||
|
op.alter_column('end_users', 'app_id', existing_type=sa.UUID(), nullable=True)
|
||||||
|
# Step 4: 添加外键约束
|
||||||
|
print("Step 4: 添加外键约束...")
|
||||||
|
op.create_foreign_key('fk_end_users_workspace_id','end_users', 'workspaces',
|
||||||
|
['workspace_id'], ['id']
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint('fk_end_users_workspace_id', 'end_users', type_='foreignkey')
|
||||||
|
op.alter_column('end_users', 'app_id', existing_type=sa.UUID(), nullable=False)
|
||||||
|
op.drop_column('end_users', 'workspace_id')
|
||||||
|
# ### end Alembic commands ###
|
||||||
34
api/migrations/versions/818c6c535e14_202603161825.py
Normal file
34
api/migrations/versions/818c6c535e14_202603161825.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"""202603161825
|
||||||
|
|
||||||
|
Revision ID: 818c6c535e14
|
||||||
|
Revises: 12114b3e953c
|
||||||
|
Create Date: 2026-03-16 18:33:41.883671
|
||||||
|
|
||||||
|
"""
|
||||||
|
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 = '818c6c535e14'
|
||||||
|
down_revision: Union[str, None] = '12114b3e953c'
|
||||||
|
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.add_column('agent_configs', sa.Column('features', postgresql.JSON(astext_type=sa.Text()), nullable=True, comment='功能特性配置'))
|
||||||
|
op.add_column('tool_configs', sa.Column('is_active', sa.Boolean(), server_default='true', nullable=False, comment='是否可用,False表示已删除'))
|
||||||
|
op.create_index(op.f('ix_tool_configs_is_active'), 'tool_configs', ['is_active'], unique=False)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_index(op.f('ix_tool_configs_is_active'), table_name='tool_configs')
|
||||||
|
op.drop_column('tool_configs', 'is_active')
|
||||||
|
op.drop_column('agent_configs', 'features')
|
||||||
|
# ### end Alembic commands ###
|
||||||
Reference in New Issue
Block a user