use of org.jooq.Commit in project jOOQ by jOOQ.
the class CommitImpl method history.
/**
* Breadth first recursion over commit graph.
*/
private static final void history(Deque<Commit> commitHistory, Set<Commit> set, List<Commit> commits) {
for (Commit commit : commits) if (set.add(commit))
commitHistory.push(commit);
Collection<Commit> p = new LinkedHashSet<>();
for (Commit commit : commits) p.addAll(commit.parents());
if (!p.isEmpty()) {
List<Commit> l = new ArrayList<>(p);
Collections.reverse(l);
history(commitHistory, set, l);
}
}
use of org.jooq.Commit in project jOOQ by jOOQ.
the class MigrationImpl method revertUntrackedQueries.
private final Queries revertUntrackedQueries(Set<Schema> includedSchemas) {
Commit currentCommit = currentCommit();
Meta currentMeta = currentCommit.meta();
Meta existingMeta = dsl().meta().filterSchemas(includedSchemas::contains);
Set<Schema> expectedSchemas = new HashSet<>();
expectedSchemas.addAll(lookup(from().meta().getSchemas()));
expectedSchemas.addAll(lookup(to().meta().getSchemas()));
expectedSchemas.retainAll(includedSchemas);
schemaLoop: for (Schema schema : existingMeta.getSchemas()) {
if (!includedSchemas.contains(schema))
continue schemaLoop;
// TODO Why is this qualification necessary?
existingMeta = existingMeta.apply(dropTableIfExists(schema.getQualifiedName().append(CHANGELOG.getUnqualifiedName())).cascade());
if (!expectedSchemas.contains(schema))
existingMeta = existingMeta.apply(dropSchemaIfExists(schema).cascade());
else
currentMeta = currentMeta.apply(createSchemaIfNotExists(schema));
}
return existingMeta.migrateTo(currentMeta);
}
use of org.jooq.Commit in project jOOQ by jOOQ.
the class MigrationImpl method validate0.
private final void validate0(DefaultMigrationContext ctx) {
JooqMigrationsChangelogRecord currentRecord = currentChangelogRecord();
if (currentRecord != null) {
Commit currentCommit = commits().get(currentRecord.getMigratedTo());
if (currentCommit == null)
throw new DataMigrationValidationException("Version currently installed is not available from CommitProvider: " + currentRecord.getMigratedTo());
}
validateCommitProvider(ctx, from());
validateCommitProvider(ctx, to());
revertUntracked(ctx, null, currentRecord);
}
use of org.jooq.Commit 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.Commit in project jOOQ by jOOQ.
the class CommitsImpl method load.
private final Commit load(Map<String, CommitType> map, CommitType commit) {
Commit result = commits.get(commit.getId());
if (result != null)
return result;
Commit p1 = root;
Commit p2 = null;
List<ParentType> parents = commit.getParents();
int size = parents.size();
if (size > 0) {
CommitType c1 = map.get(parents.get(0).getId());
if (c1 == null)
throw new UnsupportedOperationException("Parent not found: " + parents.get(0).getId());
p1 = load(map, c1);
if (size == 2) {
CommitType c2 = map.get(parents.get(1).getId());
if (c2 == null)
throw new UnsupportedOperationException("Parent not found: " + parents.get(0).getId());
p2 = load(map, c2);
} else if (size > 2)
throw new UnsupportedOperationException("Merging more than two parents not yet supported");
}
result = p2 == null ? p1.commit(commit.getId(), commit.getMessage(), files(commit)) : p1.merge(commit.getId(), commit.getMessage(), p2, files(commit));
commits.put(commit.getId(), result);
return result;
}
Aggregations