use of org.jooq.Version in project jOOQ by jOOQ.
the class CommitImpl method migrateTo0.
private final Files migrateTo0(Commit resultCommit) {
// History are all the files that have been applied before this commit
Map<String, File> history = new LinkedHashMap<>();
Map<String, String> historyKeys = new HashMap<>();
// Result are all the files that are applied starting from this commit
Map<String, File> result = new LinkedHashMap<>();
// Temporary FileType.SCHEMA changes that are collapsed until a FileType.INCREMENT is encountered
Map<String, File> tempHistory = new LinkedHashMap<>();
Map<String, String> tempHistoryKeys = new HashMap<>();
Deque<Commit> commitHistory = new ArrayDeque<>();
history(commitHistory, new HashSet<>(), Arrays.asList(resultCommit));
boolean recordingResult = false;
boolean hasDeletions = false;
for (Commit commit : commitHistory) {
List<File> commitFiles = new ArrayList<>(commit.delta());
// Deletions
Iterator<File> deletions = commitFiles.iterator();
while (deletions.hasNext()) {
File file = deletions.next();
if (file.content() == null) {
hasDeletions |= true;
String path = file.path();
String tempKey = tempHistoryKeys.remove(path);
String tempRemove = tempKey != null ? tempKey : path;
String key = historyKeys.remove(path);
String remove = key != null ? key : path;
if (recordingResult && result.remove(tempRemove) == null && file.type() == INCREMENT && history.containsKey(tempRemove))
result.put(tempRemove, file);
else if (recordingResult && result.remove(remove) == null && file.type() == SCHEMA && history.containsKey(remove))
result.put(remove, file);
else
history.remove(tempRemove);
tempHistory.remove(path);
deletions.remove();
}
}
// Increments
Iterator<File> increments = commitFiles.iterator();
while (increments.hasNext()) {
File file = increments.next();
if (file.type() == INCREMENT) {
String path = file.path();
File oldFile = recordingResult ? history.get(path) : history.put(path, file);
if (oldFile == null && !tempHistory.isEmpty() && !result.containsKey(path))
move(tempHistory, result, tempHistoryKeys);
if (recordingResult)
result.put(path, file);
increments.remove();
}
}
// Schema files
Iterator<File> schemas = commitFiles.iterator();
while (schemas.hasNext()) {
File file = schemas.next();
if (file.type() == SCHEMA) {
String path = file.path();
String key = commit.id() + "-" + path;
if (recordingResult) {
tempHistory.put(path, file);
tempHistoryKeys.put(path, key);
} else {
history.put(key, file);
historyKeys.put(path, key);
}
schemas.remove();
}
}
recordingResult |= id().equals(commit.id());
}
move(tempHistory, result, tempHistoryKeys);
// See if resulting increments try to alter history
for (Iterator<Entry<String, File>> it = result.entrySet().iterator(); it.hasNext(); ) {
Entry<String, File> entry = it.next();
String path = entry.getKey();
File file = entry.getValue();
if (file.type() == INCREMENT) {
File historicFile = history.get(path);
if (historicFile != null) {
// Altering history is not allowed
if (!StringUtils.equals(historicFile.content(), file.content()))
throw new DataMigrationException("Cannot edit increment file that has already been applied: " + file);
else
// History was altered, but the alteration was reverted
it.remove();
}
}
}
// prior to the deletion.
if (hasDeletions) {
Map<String, List<String>> keys = new LinkedHashMap<>();
Set<String> remove = new LinkedHashSet<>();
result.forEach((key, file) -> {
if (file.type() == SCHEMA)
keys.computeIfAbsent(file.path(), p -> new ArrayList<>()).add(key);
else
moveAllButLast(keys, remove);
});
moveAllButLast(keys, remove);
for (String r : remove) result.remove(r);
}
Map<String, File> versionFiles = new HashMap<>();
Version from = version(ctx.version("init"), id(), versionFiles, history.values());
Version to = version(from, resultCommit.id(), versionFiles, result.values());
return new FilesImpl(from, to, result.values());
}
use of org.jooq.Version in project jOOQ by jOOQ.
the class CommitImpl method version.
private static final Version version(Version from, String newId, Map<String, File> files, Collection<File> result) {
Version to = from;
List<File> list = new ArrayList<>(result);
for (int j = 0; j < list.size(); j++) {
File file = list.get(j);
String commitId = newId + "-" + file.path();
if (file.type() == SCHEMA)
to = to.commit(commitId, sources(apply(files, file, true).values()).toArray(EMPTY_SOURCE));
else
to = to.apply(commitId, file.content());
}
return to;
}
Aggregations