Search in sources :

Example 1 with Commit

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

Example 2 with Commit

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);
}
Also used : Meta(org.jooq.Meta) Commit(org.jooq.Commit) MigrationSchema(org.jooq.conf.MigrationSchema) Schema(org.jooq.Schema) InterpreterSearchSchema(org.jooq.conf.InterpreterSearchSchema) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with Commit

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);
}
Also used : Commit(org.jooq.Commit) DataMigrationValidationException(org.jooq.exception.DataMigrationValidationException)

Example 4 with Commit

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());
}
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 5 with Commit

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;
}
Also used : CommitType(org.jooq.migrations.xml.jaxb.CommitType) Commit(org.jooq.Commit) ParentType(org.jooq.migrations.xml.jaxb.ParentType)

Aggregations

Commit (org.jooq.Commit)6 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)2 IOException (java.io.IOException)1 ArrayDeque (java.util.ArrayDeque)1 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 Git (org.eclipse.jgit.api.Git)1 Status (org.eclipse.jgit.api.Status)1 NoHeadException (org.eclipse.jgit.api.errors.NoHeadException)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 Commits (org.jooq.Commits)1 File (org.jooq.File)1 Meta (org.jooq.Meta)1 Schema (org.jooq.Schema)1