mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
Run update steps only if they have not been run before
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package sonia.scm.migration;
|
||||
|
||||
import sonia.scm.plugin.ExtensionPoint;
|
||||
import sonia.scm.version.Version;
|
||||
|
||||
@ExtensionPoint
|
||||
public interface UpdateStep {
|
||||
void doUpdate();
|
||||
|
||||
String getTargetVersion();
|
||||
Version getTargetVersion();
|
||||
|
||||
String affectedDataType();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package sonia.scm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sonia.scm.migration.UpdateStep;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class UpdateEngineTest {
|
||||
|
||||
List<String> processedUpdates = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
void shouldProcessStepsInCorrectOrder() {
|
||||
LinkedHashSet<UpdateStep> updateSteps = new LinkedHashSet<>();
|
||||
|
||||
updateSteps.add(new FixedVersionUpdateStep("1.1.1"));
|
||||
updateSteps.add(new FixedVersionUpdateStep("1.2.0"));
|
||||
updateSteps.add(new FixedVersionUpdateStep("1.1.0"));
|
||||
|
||||
UpdateEngine updateEngine = new UpdateEngine(updateSteps);
|
||||
updateEngine.update();
|
||||
|
||||
assertThat(processedUpdates)
|
||||
.containsExactly("1.1.0", "1.1.1", "1.2.0");
|
||||
}
|
||||
|
||||
class FixedVersionUpdateStep implements UpdateStep {
|
||||
private final String version;
|
||||
|
||||
FixedVersionUpdateStep(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTargetVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doUpdate() {
|
||||
processedUpdates.add(version);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package sonia.scm.update;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sonia.scm.migration.UpdateStep;
|
||||
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
||||
import sonia.scm.store.InMemoryConfigurationEntryStore;
|
||||
import sonia.scm.store.InMemoryConfigurationEntryStoreFactory;
|
||||
import sonia.scm.version.Version;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static sonia.scm.version.Version.parse;
|
||||
|
||||
class UpdateEngineTest {
|
||||
|
||||
ConfigurationEntryStoreFactory storeFactory = new InMemoryConfigurationEntryStoreFactory(new InMemoryConfigurationEntryStore());
|
||||
|
||||
List<String> processedUpdates = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
void shouldProcessStepsInCorrectOrder() {
|
||||
LinkedHashSet<UpdateStep> updateSteps = new LinkedHashSet<>();
|
||||
|
||||
updateSteps.add(new FixedVersionUpdateStep("test", "1.1.1"));
|
||||
updateSteps.add(new FixedVersionUpdateStep("test", "1.2.0"));
|
||||
updateSteps.add(new FixedVersionUpdateStep("test", "1.1.0"));
|
||||
|
||||
UpdateEngine updateEngine = new UpdateEngine(updateSteps, storeFactory);
|
||||
updateEngine.update();
|
||||
|
||||
assertThat(processedUpdates)
|
||||
.containsExactly("1.1.0", "1.1.1", "1.2.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRunStepsOnlyOnce() {
|
||||
LinkedHashSet<UpdateStep> updateSteps = new LinkedHashSet<>();
|
||||
|
||||
updateSteps.add(new FixedVersionUpdateStep("test", "1.1.1"));
|
||||
|
||||
UpdateEngine updateEngine = new UpdateEngine(updateSteps, storeFactory);
|
||||
updateEngine.update();
|
||||
|
||||
processedUpdates.clear();
|
||||
|
||||
updateEngine.update();
|
||||
|
||||
assertThat(processedUpdates).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRunStepsForDifferentTypesIndependently() {
|
||||
LinkedHashSet<UpdateStep> updateSteps = new LinkedHashSet<>();
|
||||
|
||||
updateSteps.add(new FixedVersionUpdateStep("test", "1.1.1"));
|
||||
|
||||
UpdateEngine updateEngine = new UpdateEngine(updateSteps, storeFactory);
|
||||
updateEngine.update();
|
||||
|
||||
processedUpdates.clear();
|
||||
|
||||
updateSteps.add(new FixedVersionUpdateStep("other", "1.1.1"));
|
||||
|
||||
updateEngine = new UpdateEngine(updateSteps, storeFactory);
|
||||
updateEngine.update();
|
||||
|
||||
assertThat(processedUpdates).containsExactly("1.1.1");
|
||||
}
|
||||
|
||||
class FixedVersionUpdateStep implements UpdateStep {
|
||||
private final String type;
|
||||
private final String version;
|
||||
|
||||
FixedVersionUpdateStep(String type, String version) {
|
||||
this.type = type;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Version getTargetVersion() {
|
||||
return parse(version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String affectedDataType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doUpdate() {
|
||||
processedUpdates.add(version);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user