From ada5dfa1f02eba1d51e9e2e0310eec934c00a787 Mon Sep 17 00:00:00 2001 From: Matt Weaver Date: Wed, 17 Apr 2024 11:20:18 -0400 Subject: [PATCH] MODFQMMGR-267 Only replace defined entity types when initializing This commit changes the entity type initialization process, so that only the entity types that are defined in JSON files are replaced. Any that are in the DB, but aren't defined in JSON files, will be left untouched. This prevents a non-deterministic test failure (in FqlToSqlConverterServiceIT) and also allows for custom entity types --- .../fqm/repository/EntityTypeRepository.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) 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) {