From a697010eb037e0ec6ca3481ea568cba31b1d14fc Mon Sep 17 00:00:00 2001 From: karimGl Date: Mon, 9 Dec 2024 18:11:07 +0100 Subject: [PATCH] fix(): transaction only when saving report in db --- .../ExecutionReportMigrator.java | 46 ++++++------------- .../infra/ExecutionReportRepository.java | 36 +++++++++++++++ 2 files changed, 49 insertions(+), 33 deletions(-) rename chutney/server/src/main/java/com/chutneytesting/migration/{infra => domain}/ExecutionReportMigrator.java (62%) create mode 100644 chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportRepository.java diff --git a/chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportMigrator.java b/chutney/server/src/main/java/com/chutneytesting/migration/domain/ExecutionReportMigrator.java similarity index 62% rename from chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportMigrator.java rename to chutney/server/src/main/java/com/chutneytesting/migration/domain/ExecutionReportMigrator.java index 6b54ec04..fd670898 100644 --- a/chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportMigrator.java +++ b/chutney/server/src/main/java/com/chutneytesting/migration/domain/ExecutionReportMigrator.java @@ -5,13 +5,12 @@ * */ -package com.chutneytesting.migration.infra; +package com.chutneytesting.migration.domain; import com.chutneytesting.execution.infra.storage.ScenarioExecutionReportJpaRepository; import com.chutneytesting.execution.infra.storage.jpa.ScenarioExecutionReportEntity; import com.chutneytesting.index.infra.ScenarioExecutionReportIndexRepository; -import com.chutneytesting.migration.domain.DataMigrator; -import jakarta.persistence.EntityManager; +import com.chutneytesting.migration.infra.ExecutionReportRepository; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,69 +18,50 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; @Component public class ExecutionReportMigrator implements DataMigrator { - - private final ScenarioExecutionReportIndexRepository scenarioExecutionReportIndexRepository; private final ScenarioExecutionReportJpaRepository scenarioExecutionReportJpaRepository; - private final EntityManager entityManager; + private final ScenarioExecutionReportIndexRepository scenarioExecutionReportIndexRepository; + private final ExecutionReportRepository executionReportRepository; private static final Logger LOGGER = LoggerFactory.getLogger(ExecutionReportMigrator.class); - - public ExecutionReportMigrator(ScenarioExecutionReportIndexRepository scenarioExecutionReportIndexRepository, - ScenarioExecutionReportJpaRepository scenarioExecutionReportJpaRepository, - EntityManager entityManager) { - this.scenarioExecutionReportIndexRepository = scenarioExecutionReportIndexRepository; + public ExecutionReportMigrator(ExecutionReportRepository executionReportRepository, + ScenarioExecutionReportJpaRepository scenarioExecutionReportJpaRepository, ScenarioExecutionReportIndexRepository scenarioExecutionReportIndexRepository) { this.scenarioExecutionReportJpaRepository = scenarioExecutionReportJpaRepository; - this.entityManager = entityManager; + this.executionReportRepository = executionReportRepository; + this.scenarioExecutionReportIndexRepository = scenarioExecutionReportIndexRepository; } @Override - @Transactional public void migrate() { if (isMigrationDone()) { LOGGER.info("Report index not empty. Skipping indexing and in-db compression..."); return; } - LOGGER.info("Start indexing and in-db compression..."); PageRequest firstPage = PageRequest.of(0, 10); int count = 0; - compressAndIndex(firstPage, count); + migrate(firstPage, count); } - private void compressAndIndex(Pageable pageable, int previousCount) { + protected void migrate(Pageable pageable, int previousCount) { LOGGER.debug("Indexing and compressing reports in page n° {}", pageable.getPageNumber()); Slice slice = scenarioExecutionReportJpaRepository.findAll(pageable); List reports = slice.getContent(); - compressAndSaveInDb(reports); + executionReportRepository.compressAndSaveInDb(reports); index(reports); + int count = previousCount + slice.getNumberOfElements(); if (slice.hasNext()) { - compressAndIndex(slice.nextPageable(), count); + migrate(slice.nextPageable(), count); } else { LOGGER.info("{} report(s) successfully compressed and indexed", count); } } - private void compressAndSaveInDb(List reportsInDb) { - // calling scenarioExecutionReportJpaRepository find() and then save() doesn't call ReportConverter. - // ReportConverter will be called by entityManager update. So compression will be done. - reportsInDb.forEach(report -> { - entityManager.createQuery( - "UPDATE SCENARIO_EXECUTIONS_REPORTS SET report = :report WHERE id = :id") - .setParameter("report", report.getReport()) - .setParameter("id", report.scenarioExecutionId()) - .executeUpdate(); - entityManager.flush(); - entityManager.detach(report); - }); - } - private void index(List reportsInDb) { scenarioExecutionReportIndexRepository.saveAll(reportsInDb); } diff --git a/chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportRepository.java b/chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportRepository.java new file mode 100644 index 00000000..08bcaf52 --- /dev/null +++ b/chutney/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportRepository.java @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2017-2024 Enedis + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package com.chutneytesting.migration.infra; + +import com.chutneytesting.execution.infra.storage.jpa.ScenarioExecutionReportEntity; +import jakarta.persistence.EntityManager; +import java.util.List; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +public class ExecutionReportRepository { + + private final EntityManager entityManager; + + public ExecutionReportRepository(EntityManager entityManager ) { + this.entityManager = entityManager; + } + + @Transactional + public void compressAndSaveInDb(List reportsInDb) { + reportsInDb.forEach(report -> { + entityManager.createQuery( + "UPDATE SCENARIO_EXECUTIONS_REPORTS SET report = :report WHERE id = :id") + .setParameter("report", report.getReport()) + .setParameter("id", report.scenarioExecutionId()) + .executeUpdate(); + entityManager.detach(report); + }); + } +}