Skip to content

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

refactor: let db enforce gateway pool delete restriction via fk

bernie-g4082cecfef67
diff --git a/backend/src/db/migrations/20260414000001_add-gateway-pools.ts b/backend/src/db/migrations/20260414000001_add-gateway-pools.ts
index ad9ced4acd..7bb9797b52 100644
--- a/backend/src/db/migrations/20260414000001_add-gateway-pools.ts
+++ b/backend/src/db/migrations/20260414000001_add-gateway-pools.ts
@@ -38,7 +38,7 @@ export async function up(knex: Knex): Promise<void> {
if (!hasGatewayPoolId) {
await knex.schema.alterTable(TableName.IdentityKubernetesAuth, (t) => {
t.uuid("gatewayPoolId").nullable();
- t.foreign("gatewayPoolId").references("id").inTable(TableName.GatewayPool).onDelete("SET NULL");
+ t.foreign("gatewayPoolId").references("id").inTable(TableName.GatewayPool).onDelete("RESTRICT");
});
}
}
diff --git a/backend/src/ee/services/gateway-pool/gateway-pool-service.ts b/backend/src/ee/services/gateway-pool/gateway-pool-service.ts
index 1322014441..60b057e1da 100644
--- a/backend/src/ee/services/gateway-pool/gateway-pool-service.ts
+++ b/backend/src/ee/services/gateway-pool/gateway-pool-service.ts
@@ -165,19 +165,19 @@ export const gatewayPoolServiceFactory = ({
throw new NotFoundError({ message: `Gateway pool with ID ${poolId} not found` });
}
- await gatewayPoolDAL.transaction(async (tx) => {
- // Check for referencing consumer configs inside transaction to prevent race conditions
- // Add more DAL counts here as pool support expands
- const k8sAuthCount = await identityKubernetesAuthDAL.countByGatewayPoolId(poolId, tx);
- const totalReferences = k8sAuthCount;
- if (totalReferences > 0) {
+ try {
+ await gatewayPoolDAL.deleteById(poolId);
+ } catch (error) {
+ if (
+ error instanceof DatabaseError &&
+ (error.error as { code?: string })?.code === DatabaseErrorCode.ForeignKeyViolation
+ ) {
throw new BadRequestError({
- message: `Cannot delete pool "${existingPool.name}" because it is referenced by ${totalReferences} consumer configuration(s). Remove the pool reference from those configs first.`
+ message: `Cannot delete pool "${existingPool.name}" because it is referenced by one or more consumer configurations. Remove the pool reference from those configs first.`
});
}
-
- await gatewayPoolDAL.deleteById(poolId, tx);
- });
+ throw error;
+ }
return existingPool;
};