51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
"""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 ###
|