RED-7834: fixes for migration

* remove color
* fix manualChangeOverwrite
This commit is contained in:
Kilian Schuettler 2023-12-07 11:38:41 +01:00
parent 43b05d58a9
commit ad6e628e0d

View File

@ -1,92 +0,0 @@
package com.iqser.red.service.redaction.v1.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import com.iqser.red.commons.jackson.ObjectMapperFactory;
import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.RedactionLog;
import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.RedactionLogEntry;
import lombok.SneakyThrows;
public class RedactionLogCrawler {
@Test
@SneakyThrows
public void findRedactionLogVersion0() {
String redactionLogsDir = "/home/kschuettler/Nextcloud/redaction_logs/";
long version0Count = Files.walk(Path.of(redactionLogsDir))
.parallel()
.filter(file -> {
try {
return Files.size(file) > 0;
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.map(Path::toFile)
.filter(File::isFile)
.filter(file -> file.toString().endsWith("redactionlog.json"))
.peek(System.out::println)
.map(this::parseRedactionLog)
.filter(LogAndFile::isValid)
.filter(LogAndFile::hasAnyDuplicates)
.peek(LogAndFile::saveToTmp)
.count();
System.out.println(version0Count + " version 0 redaction logs found");
}
@SneakyThrows
private LogAndFile parseRedactionLog(File file) {
var mapper = ObjectMapperFactory.create();
try (var in = new FileInputStream(file)) {
return new LogAndFile(mapper.readValue(in.readAllBytes(), RedactionLog.class), file);
}
}
private record LogAndFile(RedactionLog redactionLog, File file) {
public boolean hasAnyDuplicates() {
Set<String> uniqueStrings = new HashSet<>();
for (String str : redactionLog.getRedactionLogEntry().stream().map(RedactionLogEntry::getId).toList()) {
if (!uniqueStrings.add(str)) {
return true;
}
}
return false;
}
@SneakyThrows
public void saveToTmp() {
Path original = file.toPath();
Path tmpFile = Path.of("/tmp/").resolve(Path.of("/home/kschuettler/Nextcloud").relativize(original));
tmpFile.toFile().mkdirs();
Files.copy(original, tmpFile, StandardCopyOption.REPLACE_EXISTING);
}
public boolean isValid() {
return redactionLog.getRedactionLogEntry() != null;
}
}
}