mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Run update steps only if they have not been run before
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
package sonia.scm;
|
||||
|
||||
import sonia.scm.migration.UpdateStep;
|
||||
import sonia.scm.version.Version;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class UpdateEngine {
|
||||
|
||||
private final List<UpdateStep> steps;
|
||||
|
||||
@Inject
|
||||
public UpdateEngine(Set<UpdateStep> steps) {
|
||||
this.steps = sortSteps(steps);
|
||||
}
|
||||
|
||||
private List<UpdateStep> sortSteps(Set<UpdateStep> steps) {
|
||||
Comparator<UpdateStep> compareByVersion = Comparator.comparing(step -> Version.parse(step.getTargetVersion()));
|
||||
return steps.stream()
|
||||
.sorted(compareByVersion.reversed())
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public void update() {
|
||||
steps.forEach(UpdateStep::doUpdate);
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ import com.google.inject.Module;
|
||||
import com.google.inject.assistedinject.FactoryModuleBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.UpdateEngine;
|
||||
import sonia.scm.SCMContext;
|
||||
import sonia.scm.ScmContextListener;
|
||||
import sonia.scm.Stage;
|
||||
@@ -51,6 +50,7 @@ import sonia.scm.plugin.PluginLoadException;
|
||||
import sonia.scm.plugin.PluginWrapper;
|
||||
import sonia.scm.plugin.PluginsInternal;
|
||||
import sonia.scm.plugin.SmpArchive;
|
||||
import sonia.scm.update.UpdateEngine;
|
||||
import sonia.scm.util.ClassLoaders;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
|
||||
53
scm-webapp/src/main/java/sonia/scm/update/UpdateEngine.java
Normal file
53
scm-webapp/src/main/java/sonia/scm/update/UpdateEngine.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package sonia.scm.update;
|
||||
|
||||
import sonia.scm.migration.UpdateStep;
|
||||
import sonia.scm.store.ConfigurationEntryStore;
|
||||
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class UpdateEngine {
|
||||
|
||||
private static final String STORE_NAME = "executedUpdates";
|
||||
|
||||
private final List<UpdateStep> steps;
|
||||
private final ConfigurationEntryStore<UpdateVersionInfo> store;
|
||||
|
||||
@Inject
|
||||
public UpdateEngine(Set<UpdateStep> steps, ConfigurationEntryStoreFactory storeFactory) {
|
||||
this.steps = sortSteps(steps);
|
||||
this.store = storeFactory.withType(UpdateVersionInfo.class).withName(STORE_NAME).build();
|
||||
}
|
||||
|
||||
private List<UpdateStep> sortSteps(Set<UpdateStep> steps) {
|
||||
return steps.stream()
|
||||
.sorted(Comparator.comparing(UpdateStep::getTargetVersion).reversed())
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public void update() {
|
||||
steps
|
||||
.stream()
|
||||
.filter(this::notRunYet)
|
||||
.forEach(this::execute);
|
||||
}
|
||||
|
||||
private void execute(UpdateStep updateStep) {
|
||||
updateStep.doUpdate();
|
||||
UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion());
|
||||
store.put(updateStep.affectedDataType(), newVersionInfo);
|
||||
}
|
||||
|
||||
private boolean notRunYet(UpdateStep updateStep) {
|
||||
UpdateVersionInfo updateVersionInfo = store.get(updateStep.affectedDataType());
|
||||
if (updateVersionInfo == null) {
|
||||
return true;
|
||||
}
|
||||
return updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package sonia.scm.update;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "latest-version")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class UpdateVersionInfo {
|
||||
private String latestVersion;
|
||||
|
||||
public UpdateVersionInfo() {
|
||||
}
|
||||
|
||||
public UpdateVersionInfo(String latestVersion) {
|
||||
this.latestVersion = latestVersion;
|
||||
}
|
||||
|
||||
public String getLatestVersion() {
|
||||
return latestVersion;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user