From ed7c2a01e79cb2c42ca0d727dffdc413c9530df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Wed, 15 May 2019 13:29:50 +0200 Subject: [PATCH] Log steps for updates --- .../sonia/scm/migration/UpdateException.java | 11 +++++ .../java/sonia/scm/migration/UpdateStep.java | 4 +- .../java/sonia/scm/update/UpdateEngine.java | 43 ++++++++++++++++--- .../sonia/scm/update/UpdateEngineTest.java | 2 +- 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 scm-core/src/main/java/sonia/scm/migration/UpdateException.java diff --git a/scm-core/src/main/java/sonia/scm/migration/UpdateException.java b/scm-core/src/main/java/sonia/scm/migration/UpdateException.java new file mode 100644 index 0000000000..4023620b96 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/migration/UpdateException.java @@ -0,0 +1,11 @@ +package sonia.scm.migration; + +public class UpdateException extends RuntimeException { + public UpdateException(String message) { + super(message); + } + + public UpdateException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/scm-core/src/main/java/sonia/scm/migration/UpdateStep.java b/scm-core/src/main/java/sonia/scm/migration/UpdateStep.java index b29de6b151..eaa6d8d549 100644 --- a/scm-core/src/main/java/sonia/scm/migration/UpdateStep.java +++ b/scm-core/src/main/java/sonia/scm/migration/UpdateStep.java @@ -5,9 +5,9 @@ import sonia.scm.version.Version; @ExtensionPoint public interface UpdateStep { - void doUpdate(); + void doUpdate() throws Exception; Version getTargetVersion(); - String affectedDataType(); + String getAffectedDataType(); } diff --git a/scm-webapp/src/main/java/sonia/scm/update/UpdateEngine.java b/scm-webapp/src/main/java/sonia/scm/update/UpdateEngine.java index b558650a29..910d9ee054 100644 --- a/scm-webapp/src/main/java/sonia/scm/update/UpdateEngine.java +++ b/scm-webapp/src/main/java/sonia/scm/update/UpdateEngine.java @@ -1,5 +1,8 @@ package sonia.scm.update; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sonia.scm.migration.UpdateException; import sonia.scm.migration.UpdateStep; import sonia.scm.store.ConfigurationEntryStore; import sonia.scm.store.ConfigurationEntryStoreFactory; @@ -13,6 +16,8 @@ import static java.util.stream.Collectors.toList; public class UpdateEngine { + public static final Logger LOG = LoggerFactory.getLogger(UpdateEngine.class); + private static final String STORE_NAME = "executedUpdates"; private final List steps; @@ -25,9 +30,12 @@ public class UpdateEngine { } private List sortSteps(Set steps) { - return steps.stream() + LOG.trace("sorting available update steps:"); + List sortedSteps = steps.stream() .sorted(Comparator.comparing(UpdateStep::getTargetVersion).reversed()) .collect(toList()); + sortedSteps.forEach(step -> LOG.trace("{} for version {}", step.getAffectedDataType(), step.getTargetVersion())); + return sortedSteps; } public void update() { @@ -38,16 +46,41 @@ public class UpdateEngine { } private void execute(UpdateStep updateStep) { - updateStep.doUpdate(); + try { + LOG.info("running update step for type {} and version {}", + updateStep.getAffectedDataType(), + updateStep.getTargetVersion() + ); + updateStep.doUpdate(); + } catch (Exception e) { + throw new UpdateException( + String.format( + "could not execute update for type %s to version %s in class %s", + updateStep.getAffectedDataType(), + updateStep.getTargetVersion(), + updateStep.getClass()), + e); + } UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion()); - store.put(updateStep.affectedDataType(), newVersionInfo); + store.put(updateStep.getAffectedDataType(), newVersionInfo); } private boolean notRunYet(UpdateStep updateStep) { - UpdateVersionInfo updateVersionInfo = store.get(updateStep.affectedDataType()); + LOG.trace("checking whether to run update step for type {} and version {}", + updateStep.getAffectedDataType(), + updateStep.getTargetVersion() + ); + UpdateVersionInfo updateVersionInfo = store.get(updateStep.getAffectedDataType()); if (updateVersionInfo == null) { + LOG.trace("no updates for type {} run yet; step will be executed", updateStep.getAffectedDataType()); return true; } - return updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion()); + boolean result = updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion()); + LOG.trace("latest version for type {}: {}; step will be executed: {}", + updateStep.getAffectedDataType(), + updateVersionInfo.getLatestVersion(), + result + ); + return result; } } diff --git a/scm-webapp/src/test/java/sonia/scm/update/UpdateEngineTest.java b/scm-webapp/src/test/java/sonia/scm/update/UpdateEngineTest.java index fb90d9d470..02ff0967bb 100644 --- a/scm-webapp/src/test/java/sonia/scm/update/UpdateEngineTest.java +++ b/scm-webapp/src/test/java/sonia/scm/update/UpdateEngineTest.java @@ -85,7 +85,7 @@ class UpdateEngineTest { } @Override - public String affectedDataType() { + public String getAffectedDataType() { return type; }