mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
Assert that the existing repositories.xml file contains v1 data
This commit is contained in:
@@ -24,10 +24,30 @@ import java.nio.file.Path;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static java.util.Optional.empty;
|
||||||
|
import static java.util.Optional.of;
|
||||||
import static sonia.scm.version.Version.parse;
|
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
|
@Extension
|
||||||
public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
||||||
|
|
||||||
@@ -62,8 +82,9 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(V1RepositoryDatabase.class);
|
JAXBContext jaxbContext = JAXBContext.newInstance(V1RepositoryDatabase.class);
|
||||||
V1RepositoryDatabase v1Database = readV1Database(jaxbContext);
|
readV1Database(jaxbContext).ifPresent(
|
||||||
v1Database.repositoryList.repositories.forEach(this::update);
|
v1Database -> v1Database.repositoryList.repositories.forEach(this::update)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update(V1Repository v1Repository) {
|
private void update(V1Repository v1Repository) {
|
||||||
@@ -118,8 +139,13 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
|||||||
return v1Name.split("/");
|
return v1Name.split("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
private V1RepositoryDatabase readV1Database(JAXBContext jaxbContext) throws JAXBException {
|
private Optional<V1RepositoryDatabase> readV1Database(JAXBContext jaxbContext) throws JAXBException {
|
||||||
return (V1RepositoryDatabase) jaxbContext.createUnmarshaller().unmarshal(determineV1File());
|
Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(determineV1File());
|
||||||
|
if (unmarshal instanceof V1RepositoryDatabase) {
|
||||||
|
return of((V1RepositoryDatabase) unmarshal);
|
||||||
|
} else {
|
||||||
|
return empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private File determineV1File() {
|
private File determineV1File() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package sonia.scm.repository.update;
|
package sonia.scm.repository.update;
|
||||||
|
|
||||||
|
import com.google.common.io.Resources;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
@@ -14,11 +15,12 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
|||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.RepositoryPermission;
|
import sonia.scm.repository.RepositoryPermission;
|
||||||
import sonia.scm.repository.spi.ZippedRepositoryTestBase;
|
|
||||||
import sonia.scm.repository.xml.XmlRepositoryDAO;
|
import sonia.scm.repository.xml.XmlRepositoryDAO;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -165,7 +167,15 @@ class XmlRepositoryV1UpdateStepTest {
|
|||||||
@Test
|
@Test
|
||||||
void shouldNotFailIfNoOldDatabaseExists() throws JAXBException {
|
void shouldNotFailIfNoOldDatabaseExists() throws JAXBException {
|
||||||
updateStep.doUpdate();
|
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) {
|
private Optional<Repository> findByNamespace(String namespace) {
|
||||||
|
|||||||
@@ -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>
|
||||||
Reference in New Issue
Block a user