Assert that the existing repositories.xml file contains v1 data

This commit is contained in:
René Pfeuffer
2019-05-23 15:25:25 +02:00
parent 31e56a6672
commit 035abef465
3 changed files with 48 additions and 8 deletions

View File

@@ -24,10 +24,30 @@ import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static sonia.scm.version.Version.parse;
/**
* Migrates SCM-Manager v1 repository data structure to SCM-Manager v2 data structure.
* That is:
* <ul>
* <li>The old <code>repositories.xml</code> file is read</li>
* <li>For each repository in this database,
* <ul>
* <li>a new entry in the new <code>repository-paths.xml</code> database is written,</li>
* <li>the data directory is moved or copied to a SCM v2 consistent directory. How this is done
* can be specified by a strategy (@see {@link MigrationStrategy}), that has to be set in
* a database file named <code>migration-plan.xml</code></li> (to create this file, use {@link MigrationStrategyDao}),
* and
* <li>the new <code>metadata.xml</code> file is created.</li>
* </ul>
* </li>
* </ul>
*/
@Extension
public class XmlRepositoryV1UpdateStep implements UpdateStep {
@@ -62,8 +82,9 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
return;
}
JAXBContext jaxbContext = JAXBContext.newInstance(V1RepositoryDatabase.class);
V1RepositoryDatabase v1Database = readV1Database(jaxbContext);
v1Database.repositoryList.repositories.forEach(this::update);
readV1Database(jaxbContext).ifPresent(
v1Database -> v1Database.repositoryList.repositories.forEach(this::update)
);
}
private void update(V1Repository v1Repository) {
@@ -102,12 +123,12 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
private String getNamespace(V1Repository v1Repository) {
String[] nameParts = getNameParts(v1Repository.name);
return nameParts.length > 1? nameParts[0]: v1Repository.type;
return nameParts.length > 1 ? nameParts[0] : v1Repository.type;
}
private String getName(V1Repository v1Repository) {
String[] nameParts = getNameParts(v1Repository.name);
return nameParts.length == 1? nameParts[0]: concatPathElements(nameParts);
return nameParts.length == 1 ? nameParts[0] : concatPathElements(nameParts);
}
private String concatPathElements(String[] nameParts) {
@@ -118,8 +139,13 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
return v1Name.split("/");
}
private V1RepositoryDatabase readV1Database(JAXBContext jaxbContext) throws JAXBException {
return (V1RepositoryDatabase) jaxbContext.createUnmarshaller().unmarshal(determineV1File());
private Optional<V1RepositoryDatabase> readV1Database(JAXBContext jaxbContext) throws JAXBException {
Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(determineV1File());
if (unmarshal instanceof V1RepositoryDatabase) {
return of((V1RepositoryDatabase) unmarshal);
} else {
return empty();
}
}
private File determineV1File() {

View File

@@ -1,5 +1,6 @@
package sonia.scm.repository.update;
import com.google.common.io.Resources;
import com.google.inject.Injector;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
@@ -14,11 +15,12 @@ import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.SCMContextProvider;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.spi.ZippedRepositoryTestBase;
import sonia.scm.repository.xml.XmlRepositoryDAO;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
@@ -165,7 +167,15 @@ class XmlRepositoryV1UpdateStepTest {
@Test
void shouldNotFailIfNoOldDatabaseExists() throws JAXBException {
updateStep.doUpdate();
}
@Test
void shouldNotFailIfFormerV1DatabaseExists(@TempDirectory.TempDir Path tempDir) throws JAXBException, IOException {
URL url = Resources.getResource("sonia/scm/repository/update/formerV2RepositoryFile.xml");
Path configDir = tempDir.resolve("config");
Files.createDirectories(configDir);
Files.copy(url.openStream(), configDir.resolve("repositories.xml"));
updateStep.doUpdate();
}
private Optional<Repository> findByNamespace(String namespace) {

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<repositories creation-time="1558604015815" last-modified="1558605291416">
<repository id="C2RRHjjeL2">repositories/C2RRHjjeL2</repository>
</repositories>