Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions api/alembic/versions/5bb5281de909_make_workspace_id_required.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""Make_workspace_id_required_and_update_foreign_key_constraints

Revision ID: 5bb5281de909
Revises: 54ffc5a9b833
Create Date: 2026-01-31 11:20:54.887765

"""

from typing import Sequence, Union

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "5bb5281de909"
down_revision: Union[str, Sequence[str], None] = "54ffc5a9b833"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###

# 将 files 表的 workspace_id 设置为必填字段
op.alter_column(
"files", "workspace_id", existing_type=sa.VARCHAR(length=36), nullable=False
)

# 将 folders 表的 workspace_id 设置为必填字段
op.alter_column(
"folders", "workspace_id", existing_type=sa.VARCHAR(length=36), nullable=False
)

# 将 notes 表的 workspace_id 设置为必填字段
op.alter_column(
"notes", "workspace_id", existing_type=sa.VARCHAR(length=36), nullable=False
)

# 将 storage_backends 表的 workspace_id 设置为必填字段
op.alter_column(
"storage_backends",
"workspace_id",
existing_type=sa.VARCHAR(length=36),
nullable=False,
)

# 将 workspace_user_association 表的 created_at 改为可选字段
op.alter_column(
"workspace_user_association",
"created_at",
existing_type=postgresql.TIMESTAMP(),
nullable=True,
)

# 删除旧的工作空间外键约束(带有 SET NULL 行为)
op.drop_constraint(
op.f("workspaces_created_by_fkey"), "workspaces", type_="foreignkey"
)

# 创建新的工作空间外键约束(使用默认删除行为)
op.create_foreign_key(None, "workspaces", "users", ["created_by"], ["id"])
# ### end Alembic commands ###


def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###

# 恢复外键约束为原来的 SET NULL 行为
op.drop_constraint(None, "workspaces", type_="foreignkey")
op.create_foreign_key(
op.f("workspaces_created_by_fkey"),
"workspaces",
"users",
["created_by"],
["id"],
ondelete="SET NULL",
)

# 恢复 workspace_user_association 表的 created_at 为必填字段
op.alter_column(
"workspace_user_association",
"created_at",
existing_type=postgresql.TIMESTAMP(),
nullable=False,
)

# 恢复 storage_backends 表的 workspace_id 为可选字段
op.alter_column(
"storage_backends",
"workspace_id",
existing_type=sa.VARCHAR(length=36),
nullable=True,
)

# 恢复 notes 表的 workspace_id 为可选字段
op.alter_column(
"notes", "workspace_id", existing_type=sa.VARCHAR(length=36), nullable=True
)

# 恢复 folders 表的 workspace_id 为可选字段
op.alter_column(
"folders", "workspace_id", existing_type=sa.VARCHAR(length=36), nullable=True
)

# 恢复 files 表的 workspace_id 为可选字段
op.alter_column(
"files", "workspace_id", existing_type=sa.VARCHAR(length=36), nullable=True
)
# ### end Alembic commands ###