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;
}
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);
}
}
}
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);
}
}
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'"));
}
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)"));
}
Aggregations