Search in sources :

Example 21 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class DiffToChangeLog method generateChangeSets.

public List<ChangeSet> generateChangeSets() {
    final ChangeGeneratorFactory changeGeneratorFactory = ChangeGeneratorFactory.getInstance();
    DatabaseObjectComparator comparator = new DatabaseObjectComparator();
    String created = null;
    if (GlobalConfiguration.GENERATE_CHANGESET_CREATED_VALUES.getCurrentValue()) {
        created = new SimpleDateFormat("yyyy-MM-dd HH:mmZ").format(new Date());
    }
    List<Class<? extends DatabaseObject>> types = getOrderedOutputTypes(ChangedObjectChangeGenerator.class);
    List<ChangeSet> updateChangeSets = new ArrayList<ChangeSet>();
    // Keep a reference to DiffResult in the comparision database so that it can be retrieved later
    // This is to avoid changing the MissingObjectChangeGenerator API and still be able to pass the
    // initial DiffResult Object which can be used to check for the objects available in the database
    // without doing any expensive db calls. Example usage is in MissingUniqueConstraintChangeGenerator#alreadyExists()
    Database comparisionDatabase = diffResult.getComparisonSnapshot().getDatabase();
    if (comparisionDatabase instanceof AbstractJdbcDatabase) {
        ((AbstractJdbcDatabase) comparisionDatabase).set("diffResult", diffResult);
    }
    for (Class<? extends DatabaseObject> type : types) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        for (Map.Entry<? extends DatabaseObject, ObjectDifferences> entry : diffResult.getChangedObjects(type, comparator).entrySet()) {
            if (!diffResult.getReferenceSnapshot().getDatabase().isLiquibaseObject(entry.getKey()) && !diffResult.getReferenceSnapshot().getDatabase().isSystemObject(entry.getKey())) {
                Change[] changes = changeGeneratorFactory.fixChanged(entry.getKey(), entry.getValue(), diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
                addToChangeSets(changes, updateChangeSets, quotingStrategy, created);
            }
        }
    }
    types = getOrderedOutputTypes(MissingObjectChangeGenerator.class);
    List<DatabaseObject> missingObjects = new ArrayList<DatabaseObject>();
    for (Class<? extends DatabaseObject> type : types) {
        for (DatabaseObject object : diffResult.getMissingObjects(type, getDbObjectComparator())) {
            if (object == null) {
                continue;
            }
            if (!diffResult.getReferenceSnapshot().getDatabase().isLiquibaseObject(object) && !diffResult.getReferenceSnapshot().getDatabase().isSystemObject(object)) {
                missingObjects.add(object);
            }
        }
    }
    List<ChangeSet> createChangeSets = new ArrayList<ChangeSet>();
    for (DatabaseObject object : sortMissingObjects(missingObjects, diffResult.getReferenceSnapshot().getDatabase())) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        Change[] changes = changeGeneratorFactory.fixMissing(object, diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
        addToChangeSets(changes, createChangeSets, quotingStrategy, created);
    }
    List<ChangeSet> deleteChangeSets = new ArrayList<ChangeSet>();
    types = getOrderedOutputTypes(UnexpectedObjectChangeGenerator.class);
    for (Class<? extends DatabaseObject> type : types) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        for (DatabaseObject object : sortUnexpectedObjects(diffResult.getUnexpectedObjects(type, comparator), diffResult.getReferenceSnapshot().getDatabase())) {
            if (!diffResult.getComparisonSnapshot().getDatabase().isLiquibaseObject(object) && !diffResult.getComparisonSnapshot().getDatabase().isSystemObject(object)) {
                Change[] changes = changeGeneratorFactory.fixUnexpected(object, diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
                addToChangeSets(changes, deleteChangeSets, quotingStrategy, created);
            }
        }
    }
    // remove the diffResult from the database object
    if (comparisionDatabase instanceof AbstractJdbcDatabase) {
        ((AbstractJdbcDatabase) comparisionDatabase).set("diffResult", null);
    }
    List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    changeSets.addAll(createChangeSets);
    changeSets.addAll(deleteChangeSets);
    changeSets.addAll(updateChangeSets);
    return changeSets;
}
Also used : Change(liquibase.change.Change) ObjectDifferences(liquibase.diff.ObjectDifferences) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) DatabaseObject(liquibase.structure.DatabaseObject) SimpleDateFormat(java.text.SimpleDateFormat) ChangeSet(liquibase.changelog.ChangeSet)

Example 22 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class DiffToChangeLog method addToChangeSets.

private void addToChangeSets(Change[] changes, List<ChangeSet> changeSets, ObjectQuotingStrategy quotingStrategy, String created) {
    if (changes != null) {
        String csContext = this.changeSetContext;
        if (diffOutputControl.getContext() != null) {
            csContext = diffOutputControl.getContext().toString().replaceFirst("^\\(", "").replaceFirst("\\)$", "");
        }
        if (useSeparateChangeSets(changes)) {
            for (Change change : changes) {
                ChangeSet changeSet = new ChangeSet(generateId(changes), getChangeSetAuthor(), false, false, this.changeSetPath, changeSetContext, null, false, quotingStrategy, null);
                changeSet.setCreated(created);
                if (diffOutputControl.getLabels() != null) {
                    changeSet.setLabels(diffOutputControl.getLabels());
                }
                changeSet.addChange(change);
                changeSets.add(changeSet);
            }
        } else {
            ChangeSet changeSet = new ChangeSet(generateId(changes), getChangeSetAuthor(), false, false, this.changeSetPath, csContext, null, false, quotingStrategy, null);
            changeSet.setCreated(created);
            if (diffOutputControl.getLabels() != null) {
                changeSet.setLabels(diffOutputControl.getLabels());
            }
            for (Change change : changes) {
                changeSet.addChange(change);
            }
            changeSets.add(changeSet);
        }
    }
}
Also used : Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet)

Example 23 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class PendingSQLWriter method writeBody.

@Override
protected void writeBody(Writer fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
    Executor oldTemplate = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(oldTemplate, fileWriter, database);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("logging", database, loggingExecutor);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, loggingExecutor);
    try {
        if (changesToRun.isEmpty()) {
            fileWriter.append("<b>NONE</b>");
        }
        fileWriter.append("<code><pre>");
        ChangeSet lastRunChangeSet = null;
        for (Change change : changesToRun) {
            ChangeSet thisChangeSet = change.getChangeSet();
            if (thisChangeSet.equals(lastRunChangeSet)) {
                continue;
            }
            lastRunChangeSet = thisChangeSet;
            String anchor = thisChangeSet.toString(false).replaceAll("\\W", "_");
            fileWriter.append("<a name='").append(anchor).append("'/>");
            try {
                thisChangeSet.execute(databaseChangeLog, null, this.database);
            } catch (MigrationFailedException e) {
                fileWriter.append("EXECUTION ERROR: ").append(Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(change).getDescription()).append(": ").append(e.getMessage()).append("\n\n");
            }
        }
        fileWriter.append("</pre></code>");
    } finally {
        Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, oldTemplate);
    }
}
Also used : LoggingExecutor(liquibase.executor.LoggingExecutor) Executor(liquibase.executor.Executor) MigrationFailedException(liquibase.exception.MigrationFailedException) ChangeFactory(liquibase.change.ChangeFactory) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet)

Example 24 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class MarkChangeSetRanGeneratorTest method generateSqlSecondRunUpdatesLabelsContextsComments.

/**
 * Ensure that upon running an update on a changeset that has been run before, we still update the labels,
 * contexts and comments columns in the DBCL table.
 */
@Test
public void generateSqlSecondRunUpdatesLabelsContextsComments() {
    String changeSetContextExpression = "changeSetContext1 AND changeSetContext2";
    DatabaseChangeLog rootChangeLog = new DatabaseChangeLog();
    rootChangeLog.setContexts(new ContextExpression("rootContext1 OR (rootContext2) AND (rootContext3)"));
    DatabaseChangeLog childChangeLog = new DatabaseChangeLog();
    childChangeLog.setContexts(new ContextExpression("childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3"));
    childChangeLog.setIncludeContexts(new ContextExpression("includeContext1, includeContext2 AND includeContext3"));
    childChangeLog.setParentChangeLog(rootChangeLog);
    ChangeSet changeSet = new ChangeSet("1", "a", false, false, "c", changeSetContextExpression, null, childChangeLog);
    changeSet.setComments("comment12345");
    changeSet.setLabels(new Labels("newlabel123"));
    Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(changeSet, ChangeSet.ExecType.RERAN), new MockDatabase(), new MockSqlGeneratorChain());
    String sql = sqls[0].toSql();
    assertTrue(sql.contains("COMMENTS = 'comment12345'"));
    assertTrue(sql.contains("CONTEXTS = '(childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3) AND (includeContext1, includeContext2 AND includeContext3) AND (rootContext1 OR (rootContext2) AND (rootContext3)) AND (changeSetContext1 AND changeSetContext2)'"));
    assertTrue(sql.contains("LABELS = 'newlabel123'"));
}
Also used : ContextExpression(liquibase.ContextExpression) MarkChangeSetRanStatement(liquibase.statement.core.MarkChangeSetRanStatement) MockDatabase(liquibase.database.core.MockDatabase) Labels(liquibase.Labels) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) Sql(liquibase.sql.Sql) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 25 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class MarkChangeSetRanGeneratorTest method generateSqlWithComplexContext.

@Test
public void generateSqlWithComplexContext() {
    String changeSetContextExpression = "changeSetContext1 AND changeSetContext2";
    DatabaseChangeLog rootChangeLog = new DatabaseChangeLog();
    rootChangeLog.setContexts(new ContextExpression("rootContext1 OR (rootContext2) AND (rootContext3)"));
    DatabaseChangeLog childChangeLog = new DatabaseChangeLog();
    childChangeLog.setContexts(new ContextExpression("childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3"));
    childChangeLog.setIncludeContexts(new ContextExpression("includeContext1, includeContext2 AND includeContext3"));
    childChangeLog.setParentChangeLog(rootChangeLog);
    ChangeSet changeSet = new ChangeSet("1", "a", false, false, "c", changeSetContextExpression, null, childChangeLog);
    Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(changeSet, ChangeSet.ExecType.EXECUTED), new MockDatabase(), new MockSqlGeneratorChain());
    assertTrue(sqls[0].toSql(), sqls[0].toSql().contains("(childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3) AND " + "(includeContext1, includeContext2 AND includeContext3) AND " + "(rootContext1 OR (rootContext2) AND (rootContext3)) AND " + "(changeSetContext1 AND changeSetContext2)"));
}
Also used : ContextExpression(liquibase.ContextExpression) MarkChangeSetRanStatement(liquibase.statement.core.MarkChangeSetRanStatement) MockDatabase(liquibase.database.core.MockDatabase) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) Sql(liquibase.sql.Sql) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Aggregations

ChangeSet (liquibase.changelog.ChangeSet)72 Test (org.junit.Test)41 Contexts (liquibase.Contexts)12 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)11 RanChangeSet (liquibase.changelog.RanChangeSet)10 Liquibase (liquibase.Liquibase)9 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 LiquibaseException (liquibase.exception.LiquibaseException)8 ArrayList (java.util.ArrayList)7 DiffOutputControl (liquibase.diff.output.DiffOutputControl)7 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)7 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)6 DiffResult (liquibase.diff.DiffResult)6 CompareControl (liquibase.diff.compare.CompareControl)6 IOException (java.io.IOException)5 Sql (liquibase.sql.Sql)5 MarkChangeSetRanStatement (liquibase.statement.core.MarkChangeSetRanStatement)5 Date (java.util.Date)4 Labels (liquibase.Labels)4