diff --git a/api/alembic/versions/3c3d09cd858f_make_storage_backends_workspace_id_nullable.py b/api/alembic/versions/3c3d09cd858f_make_storage_backends_workspace_id_nullable.py new file mode 100644 index 0000000..3e8b3f6 --- /dev/null +++ b/api/alembic/versions/3c3d09cd858f_make_storage_backends_workspace_id_nullable.py @@ -0,0 +1,56 @@ +"""Make storage_backends workspace_id nullable + +将 storage_backends 表的 workspace_id 字段设置为可选 + +Revision ID: 3c3d09cd858f +Revises: b14d95497d4f +Create Date: 2026-01-31 17:16:54.726838 + +""" + +from typing import Sequence, Union + +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +# 修订标识符,由 Alembic 使用 +revision: str = "3c3d09cd858f" +down_revision: Union[str, Sequence[str], None] = "b14d95497d4f" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema. + + 升级数据库架构:将 storage_backends 表的 workspace_id 字段改为可空 + 允许存储后端不绑定到特定工作空间 + """ + # ### commands auto generated by Alembic - please adjust! ### + # 修改 storage_backends 表的 workspace_id 列,允许为空 + op.alter_column( + "storage_backends", + "workspace_id", + existing_type=sa.VARCHAR(length=36), + nullable=True, + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema. + + 降级数据库架构:将 storage_backends 表的 workspace_id 字段改为不可空 + 回退到要求存储后端必须绑定到工作空间的状态 + """ + # ### commands auto generated by Alembic - please adjust! ### + # 修改 storage_backends 表的 workspace_id 列,设置为不可空 + op.alter_column( + "storage_backends", + "workspace_id", + existing_type=sa.VARCHAR(length=36), + nullable=False, + ) + # ### end Alembic commands ### diff --git a/api/app/models.py b/api/app/models.py index 74f7f29..b212561 100644 --- a/api/app/models.py +++ b/api/app/models.py @@ -246,7 +246,7 @@ class StorageBackendConfig(Base): id = Column( String(36), primary_key=True, index=True, default=lambda: str(uuid.uuid4()) ) - workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=False) + workspace_id = Column(String(36), ForeignKey("workspaces.id"), nullable=True) name: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True) backend_type: Mapped[str] = mapped_column(String, nullable=False) # local or s3 is_active: Mapped[bool] = mapped_column(Integer, default=0) # 0: False, 1: True