mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Log steps for updates
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@ import sonia.scm.version.Version;
|
|||||||
|
|
||||||
@ExtensionPoint
|
@ExtensionPoint
|
||||||
public interface UpdateStep {
|
public interface UpdateStep {
|
||||||
void doUpdate();
|
void doUpdate() throws Exception;
|
||||||
|
|
||||||
Version getTargetVersion();
|
Version getTargetVersion();
|
||||||
|
|
||||||
String affectedDataType();
|
String getAffectedDataType();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package sonia.scm.update;
|
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.migration.UpdateStep;
|
||||||
import sonia.scm.store.ConfigurationEntryStore;
|
import sonia.scm.store.ConfigurationEntryStore;
|
||||||
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
||||||
@@ -13,6 +16,8 @@ import static java.util.stream.Collectors.toList;
|
|||||||
|
|
||||||
public class UpdateEngine {
|
public class UpdateEngine {
|
||||||
|
|
||||||
|
public static final Logger LOG = LoggerFactory.getLogger(UpdateEngine.class);
|
||||||
|
|
||||||
private static final String STORE_NAME = "executedUpdates";
|
private static final String STORE_NAME = "executedUpdates";
|
||||||
|
|
||||||
private final List<UpdateStep> steps;
|
private final List<UpdateStep> steps;
|
||||||
@@ -25,9 +30,12 @@ public class UpdateEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<UpdateStep> sortSteps(Set<UpdateStep> steps) {
|
private List<UpdateStep> sortSteps(Set<UpdateStep> steps) {
|
||||||
return steps.stream()
|
LOG.trace("sorting available update steps:");
|
||||||
|
List<UpdateStep> sortedSteps = steps.stream()
|
||||||
.sorted(Comparator.comparing(UpdateStep::getTargetVersion).reversed())
|
.sorted(Comparator.comparing(UpdateStep::getTargetVersion).reversed())
|
||||||
.collect(toList());
|
.collect(toList());
|
||||||
|
sortedSteps.forEach(step -> LOG.trace("{} for version {}", step.getAffectedDataType(), step.getTargetVersion()));
|
||||||
|
return sortedSteps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
@@ -38,16 +46,41 @@ public class UpdateEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void execute(UpdateStep updateStep) {
|
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());
|
UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion());
|
||||||
store.put(updateStep.affectedDataType(), newVersionInfo);
|
store.put(updateStep.getAffectedDataType(), newVersionInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean notRunYet(UpdateStep updateStep) {
|
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) {
|
if (updateVersionInfo == null) {
|
||||||
|
LOG.trace("no updates for type {} run yet; step will be executed", updateStep.getAffectedDataType());
|
||||||
return true;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class UpdateEngineTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String affectedDataType() {
|
public String getAffectedDataType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user