enhance: remove table mcp_use's foreign key(mcp_id) PR406

This commit is contained in:
Wendong-Fan 2025-09-29 01:31:51 +08:00
parent 67f4aab698
commit 6a64fe47e4
2 changed files with 54 additions and 3 deletions

View file

@ -23,10 +23,62 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
"""Upgrade schema.""" """Upgrade schema."""
# Drop the foreign key constraint for mcp_id in mcp_user table # Drop the foreign key constraint for mcp_id in mcp_user table
# Use try-catch to handle cases where constraint might not exist or have different name
try:
op.drop_constraint("mcp_user_mcp_id_fkey", "mcp_user", type_="foreignkey") op.drop_constraint("mcp_user_mcp_id_fkey", "mcp_user", type_="foreignkey")
except Exception as e:
# If the expected constraint name doesn't work, try to find it dynamically
try:
connection = op.get_bind()
inspector = sa.inspect(connection)
fk_constraints = inspector.get_foreign_keys("mcp_user")
# Find the constraint that references mcp_id -> mcp.id
target_constraint = None
for fk in fk_constraints:
if (fk.get("constrained_columns") == ["mcp_id"] and
fk.get("referred_table") == "mcp" and
fk.get("referred_columns") == ["id"]):
target_constraint = fk.get("name")
break
if target_constraint:
op.drop_constraint(target_constraint, "mcp_user", type_="foreignkey")
else:
print("Warning: No foreign key constraint found for mcp_user.mcp_id -> mcp.id")
except Exception as e2:
print(f"Warning: Could not drop foreign key constraint: {e2}")
def downgrade() -> None: def downgrade() -> None:
"""Downgrade schema.""" """Downgrade schema."""
# Re-add the foreign key constraint for mcp_id in mcp_user table # Re-add the foreign key constraint for mcp_id in mcp_user table
op.create_foreign_key("mcp_user_mcp_id_fkey", "mcp_user", "mcp", ["mcp_id"], ["id"]) # Check if the constraint already exists before creating it
try:
connection = op.get_bind()
inspector = sa.inspect(connection)
fk_constraints = inspector.get_foreign_keys("mcp_user")
# Check if the constraint already exists
constraint_exists = False
for fk in fk_constraints:
if (fk.get("constrained_columns") == ["mcp_id"] and
fk.get("referred_table") == "mcp" and
fk.get("referred_columns") == ["id"]):
constraint_exists = True
break
if not constraint_exists:
op.create_foreign_key(
"mcp_user_mcp_id_fkey",
"mcp_user",
"mcp",
["mcp_id"],
["id"]
)
else:
print("Info: Foreign key constraint for mcp_user.mcp_id -> mcp.id already exists")
except Exception as e:
print(f"Warning: Could not check or create foreign key constraint: {e}")

View file

@ -71,7 +71,6 @@ class McpUserUpdate(BaseModel):
server_url: Optional[str] = None server_url: Optional[str] = None
command: Optional[str] = None command: Optional[str] = None
args: Optional[str] = None args: Optional[str] = None
env: Optional[dict] = None
mcp_key: Optional[str] = None mcp_key: Optional[str] = None