mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
Add new migration strategies "delete" and "ignore"
This commit is contained in:
@@ -9,6 +9,9 @@ import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import javax.inject.Inject;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Optional.of;
|
||||
|
||||
class CopyMigrationStrategy extends BaseMigrationStrategy {
|
||||
|
||||
@@ -23,14 +26,14 @@ class CopyMigrationStrategy extends BaseMigrationStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path migrate(String id, String name, String type) {
|
||||
public Optional<Path> migrate(String id, String name, String type) {
|
||||
Path repositoryBasePath = locationResolver.forClass(Path.class).createLocation(id);
|
||||
Path targetDataPath = repositoryBasePath
|
||||
.resolve(RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
|
||||
Path sourceDataPath = getSourceDataPath(name, type);
|
||||
LOG.info("copying repository data from {} to {}", sourceDataPath, targetDataPath);
|
||||
copyData(sourceDataPath, targetDataPath);
|
||||
return repositoryBasePath;
|
||||
return of(repositoryBasePath);
|
||||
}
|
||||
|
||||
private void copyData(Path sourceDirectory, Path targetDirectory) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package sonia.scm.update.repository;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
public class DeleteMigrationStrategy extends BaseMigrationStrategy {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DeleteMigrationStrategy.class);
|
||||
|
||||
@Inject
|
||||
DeleteMigrationStrategy(SCMContextProvider contextProvider) {
|
||||
super(contextProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Path> migrate(String id, String name, String type) {
|
||||
Path sourceDataPath = getSourceDataPath(name, type);
|
||||
try {
|
||||
IOUtil.delete(sourceDataPath.toFile(), true);
|
||||
} catch (IOException e) {
|
||||
LOG.warn("could not delete old repository path for repository {} with type {} and id {}", name, type, id);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package sonia.scm.update.repository;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
|
||||
public class IgnoreMigrationStrategy implements MigrationStrategy.Instance {
|
||||
@Override
|
||||
public Optional<Path> migrate(String id, String name, String type) {
|
||||
return empty();
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,9 @@ import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Optional.of;
|
||||
|
||||
class InlineMigrationStrategy extends BaseMigrationStrategy {
|
||||
|
||||
@@ -24,14 +27,14 @@ class InlineMigrationStrategy extends BaseMigrationStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path migrate(String id, String name, String type) {
|
||||
public Optional<Path> migrate(String id, String name, String type) {
|
||||
Path repositoryBasePath = getSourceDataPath(name, type);
|
||||
locationResolver.forClass(Path.class).setLocation(id, repositoryBasePath);
|
||||
Path targetDataPath = repositoryBasePath
|
||||
.resolve(RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
|
||||
LOG.info("moving repository data from {} to {}", repositoryBasePath, targetDataPath);
|
||||
moveData(repositoryBasePath, targetDataPath);
|
||||
return repositoryBasePath;
|
||||
return of(repositoryBasePath);
|
||||
}
|
||||
|
||||
private void moveData(Path sourceDirectory, Path targetDirectory) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package sonia.scm.update.repository;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
public enum MigrationStrategy {
|
||||
|
||||
@@ -15,7 +16,13 @@ public enum MigrationStrategy {
|
||||
INLINE(InlineMigrationStrategy.class,
|
||||
"Use the current directory where the repository data files are stored, but modify the directory " +
|
||||
"structure so that it can be used for SCM-Manager v2. The repository data files will be moved to a new " +
|
||||
"subdirectory 'data' inside the current directory.");
|
||||
"subdirectory 'data' inside the current directory."),
|
||||
IGNORE(IgnoreMigrationStrategy.class,
|
||||
"The repository will not be migrated and will not be visible inside SCM-Manager. " +
|
||||
"The data files will be kept at the current location."),
|
||||
DELETE(DeleteMigrationStrategy.class,
|
||||
"The repository will not be migrated and will not be visible inside SCM-Manager. " +
|
||||
"The data files will be deleted!");
|
||||
|
||||
private final Class<? extends Instance> implementationClass;
|
||||
private final String description;
|
||||
@@ -25,6 +32,10 @@ public enum MigrationStrategy {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Class<? extends Instance> getImplementationClass() {
|
||||
return implementationClass;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
@@ -34,6 +45,6 @@ public enum MigrationStrategy {
|
||||
}
|
||||
|
||||
interface Instance {
|
||||
Path migrate(String id, String name, String type);
|
||||
Optional<Path> migrate(String id, String name, String type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,10 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Optional.of;
|
||||
|
||||
class MoveMigrationStrategy extends BaseMigrationStrategy {
|
||||
|
||||
@@ -27,7 +29,7 @@ class MoveMigrationStrategy extends BaseMigrationStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path migrate(String id, String name, String type) {
|
||||
public Optional<Path> migrate(String id, String name, String type) {
|
||||
Path repositoryBasePath = locationResolver.forClass(Path.class).createLocation(id);
|
||||
Path targetDataPath = repositoryBasePath
|
||||
.resolve(RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
|
||||
@@ -35,7 +37,7 @@ class MoveMigrationStrategy extends BaseMigrationStrategy {
|
||||
LOG.info("moving repository data from {} to {}", sourceDataPath, targetDataPath);
|
||||
moveData(sourceDataPath, targetDataPath);
|
||||
deleteOldDataDir(getTypeDependentPath(type), name);
|
||||
return repositoryBasePath;
|
||||
return of(repositoryBasePath);
|
||||
}
|
||||
|
||||
private void deleteOldDataDir(Path rootPath, String name) {
|
||||
|
||||
@@ -141,21 +141,25 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
||||
}
|
||||
|
||||
private void update(V1Repository v1Repository) {
|
||||
Path destination = handleDataDirectory(v1Repository);
|
||||
Repository repository = new Repository(
|
||||
v1Repository.id,
|
||||
v1Repository.type,
|
||||
v1Repository.getNewNamespace(),
|
||||
v1Repository.getNewName(),
|
||||
v1Repository.contact,
|
||||
v1Repository.description,
|
||||
createPermissions(v1Repository));
|
||||
LOG.info("creating new repository {} with id {} from old repository {} in directory {}", repository.getNamespaceAndName(), repository.getId(), v1Repository.name, destination);
|
||||
repositoryDao.add(repository, destination);
|
||||
propertyStore.put(v1Repository.id, v1Repository.properties);
|
||||
Optional<Path> destination = handleDataDirectory(v1Repository);
|
||||
destination.ifPresent(
|
||||
newPath -> {
|
||||
Repository repository = new Repository(
|
||||
v1Repository.id,
|
||||
v1Repository.type,
|
||||
v1Repository.getNewNamespace(),
|
||||
v1Repository.getNewName(),
|
||||
v1Repository.contact,
|
||||
v1Repository.description,
|
||||
createPermissions(v1Repository));
|
||||
LOG.info("creating new repository {} with id {} from old repository {} in directory {}", repository.getNamespaceAndName(), repository.getId(), v1Repository.name, newPath);
|
||||
repositoryDao.add(repository, newPath);
|
||||
propertyStore.put(v1Repository.id, v1Repository.properties);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private Path handleDataDirectory(V1Repository v1Repository) {
|
||||
private Optional<Path> handleDataDirectory(V1Repository v1Repository) {
|
||||
MigrationStrategy dataMigrationStrategy = readMigrationStrategy(v1Repository);
|
||||
LOG.info("using strategy {} to migrate repository {} with id {}", dataMigrationStrategy.getClass(), v1Repository.name, v1Repository.id);
|
||||
return dataMigrationStrategy.from(injector).migrate(v1Repository.id, v1Repository.name, v1Repository.type);
|
||||
|
||||
Reference in New Issue
Block a user