Search in sources :

Example 1 with Version

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) Version(org.jooq.Version) ArrayList(java.util.ArrayList) List(java.util.List) DataMigrationException(org.jooq.exception.DataMigrationException) ArrayDeque(java.util.ArrayDeque) Commit(org.jooq.Commit) File(org.jooq.File)

Example 2 with Version

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;
}
Also used : Version(org.jooq.Version) ArrayList(java.util.ArrayList) File(org.jooq.File)

Aggregations

ArrayList (java.util.ArrayList)2 File (org.jooq.File)2 Version (org.jooq.Version)2 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Commit (org.jooq.Commit)1 DataMigrationException (org.jooq.exception.DataMigrationException)1