diff --git a/ai_knowledge_base_v2/apps/backend/alembic/versions/0008_unique_knowledge_feishu_node.py b/ai_knowledge_base_v2/apps/backend/alembic/versions/0008_unique_knowledge_feishu_node.py index 06ec548..d2c1e67 100644 --- a/ai_knowledge_base_v2/apps/backend/alembic/versions/0008_unique_knowledge_feishu_node.py +++ b/ai_knowledge_base_v2/apps/backend/alembic/versions/0008_unique_knowledge_feishu_node.py @@ -1,20 +1,25 @@ from __future__ import annotations from alembic import op +import sqlalchemy as sa -revision = "0008_unique_knowledge_feishu_node" +revision = "0008_unique_kb_node" down_revision = "0007_knowledge_agent_rebuild" branch_labels = None depends_on = None def upgrade() -> None: - op.create_unique_constraint( - "uq_knowledge_feishu_node", - "sys_knowledge", - ["feishu_space_id", "feishu_node_id"], - ) + constraints = {item["name"] for item in sa.inspect(op.get_bind()).get_unique_constraints("sys_knowledge")} + if "uq_knowledge_feishu_node" not in constraints: + op.create_unique_constraint( + "uq_knowledge_feishu_node", + "sys_knowledge", + ["feishu_space_id", "feishu_node_id"], + ) def downgrade() -> None: - op.drop_constraint("uq_knowledge_feishu_node", "sys_knowledge", type_="unique") + constraints = {item["name"] for item in sa.inspect(op.get_bind()).get_unique_constraints("sys_knowledge")} + if "uq_knowledge_feishu_node" in constraints: + op.drop_constraint("uq_knowledge_feishu_node", "sys_knowledge", type_="unique")