diff --git a/src/main/java/org/folio/fqm/repository/EntityTypeRepository.java b/src/main/java/org/folio/fqm/repository/EntityTypeRepository.java index f6b9cc1e..ddadb6cf 100644 --- a/src/main/java/org/folio/fqm/repository/EntityTypeRepository.java +++ b/src/main/java/org/folio/fqm/repository/EntityTypeRepository.java @@ -86,21 +86,28 @@ public List getEntityTypeSummary(Set entityTypeIds) .map(row -> new RawEntityTypeSummary(row.get(idField), row.get(nameField))); } - public void replaceEntityTypeDefinitions(List entityTypes) throws JsonProcessingException { + public void replaceEntityTypeDefinitions(List entityTypes) { log.info("Replacing entity type definitions with new set of {} entities", entityTypes.size()); - // we use this instead of truncate since it has better rollback support - jooqContext.deleteFrom(table(TABLE_NAME)).execute(); - - InsertValuesStep2 insert = jooqContext - .insertInto(table(TABLE_NAME)) - .columns(field(ID_FIELD_NAME, UUID.class), field(DEFINITION_FIELD_NAME, JSONB.class)); - - for (EntityType entityType : entityTypes) { - insert.values(UUID.fromString(entityType.getId()), JSONB.jsonb(objectMapper.writeValueAsString(entityType))); - } - - insert.execute(); + jooqContext.transaction(transaction -> { + transaction.dsl() + .deleteFrom(table(TABLE_NAME)) + .where( + field(ID_FIELD_NAME, UUID.class) + .in(entityTypes.stream().map(et -> UUID.fromString(et.getId())).toList()) + ) + .execute(); + + InsertValuesStep2 insert = transaction.dsl() + .insertInto(table(TABLE_NAME)) + .columns(field(ID_FIELD_NAME, UUID.class), field(DEFINITION_FIELD_NAME, JSONB.class)); + + for (EntityType entityType : entityTypes) { + insert.values(UUID.fromString(entityType.getId()), JSONB.jsonb(objectMapper.writeValueAsString(entityType))); + } + + insert.execute(); + }); } private List fetchColumnNamesForCustomFields(UUID entityTypeId) {