Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 218cb26d73 Custom object "agent" creation fails due to pre-existing orphaned enum types in workspace schema
https://sonarly.com/issue/9551?type=bug

Creating a custom object named "agent" fails because `CREATE TYPE` for `_agent_createdBySource_enum` hits an already-existing enum type from a previous attempt, aborting the PostgreSQL transaction and cascading the failure to the metadata INSERT.

Fix: The fix adds `IF NOT EXISTS` to the `CREATE TYPE` SQL statement in `createEnum()`, making it idempotent. Previously, if an orphaned enum type persisted in the workspace schema from a prior failed/partially-committed attempt to create a custom object with the same name (e.g. `_agent_createdBySource_enum`), the bare `CREATE TYPE` would throw `type already exists`, abort the active PostgreSQL transaction, and cascade that failure to the metadata INSERT running on the same `queryRunner`.

With `IF NOT EXISTS`, a pre-existing enum is silently skipped and the transaction proceeds normally. This matches the already-idempotent pattern used by `dropEnum`, which already uses `DROP TYPE IF EXISTS`.

```typescript file=packages/twenty-server/src/engine/twenty-orm/workspace-schema-manager/services/workspace-schema-enum-manager.service.ts lines=38-40
const sql = `CREATE TYPE IF NOT EXISTS ${escapeIdentifier(schemaName)}.${escapeIdentifier(enumName)} AS ENUM (${sanitizedValues})`;

await queryRunner.query(sql);
```
2026-03-04 18:14:42 +00:00
@@ -35,7 +35,7 @@ export class WorkspaceSchemaEnumManagerService {
.map((value) => escapeLiteral(value.toString()))
.join(', ');
const sql = `CREATE TYPE ${escapeIdentifier(schemaName)}.${escapeIdentifier(enumName)} AS ENUM (${sanitizedValues})`;
const sql = `CREATE TYPE IF NOT EXISTS ${escapeIdentifier(schemaName)}.${escapeIdentifier(enumName)} AS ENUM (${sanitizedValues})`;
await queryRunner.query(sql);
}