Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Ensure unique (name, projectId) constraint for AI MCP endpoints by renaming duplicates

Saif4b7aad77cf7d
diff --git a/backend/src/db/migrations/20260120120000_add-ai-mcp-endpoint-name-project-unique-constraint.ts b/backend/src/db/migrations/20260120120000_add-ai-mcp-endpoint-name-project-unique-constraint.ts
index dc91dff8ef..f63658e956 100644
--- a/backend/src/db/migrations/20260120120000_add-ai-mcp-endpoint-name-project-unique-constraint.ts
+++ b/backend/src/db/migrations/20260120120000_add-ai-mcp-endpoint-name-project-unique-constraint.ts
@@ -11,6 +11,18 @@ export async function up(knex: Knex): Promise<void> {
const hasProjectId = await knex.schema.hasColumn(TableName.AiMcpEndpoint, "projectId");
if (hasName && hasProjectId) {
+ // Rename any duplicate endpoints (keep oldest, rename newer ones)
+ await knex.raw(`
+ UPDATE ${TableName.AiMcpEndpoint} e1
+ SET "name" = e1."name" || '-' || substr(md5(random()::text), 1, 4)
+ WHERE EXISTS (
+ SELECT 1 FROM ${TableName.AiMcpEndpoint} e2
+ WHERE e2."projectId" = e1."projectId"
+ AND e2."name" = e1."name"
+ AND e2."createdAt" < e1."createdAt"
+ )
+ `);
+
await knex.schema.alterTable(TableName.AiMcpEndpoint, (table) => {
table.unique(["name", "projectId"], { indexName: CONSTRAINT_NAME });
});