Search in sources :

Example 46 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class ChangedIndexChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    // don't try to recreate indexes that differ in just clustered
    Difference clusteredDiff = differences.getDifference("clustered");
    if (clusteredDiff != null) {
        if ((clusteredDiff.getReferenceValue() == null) || (clusteredDiff.getComparedValue() == null)) {
            differences.removeDifference("clustered");
        }
    }
    for (String field : getIgnoredFields()) {
        differences.removeDifference(field);
    }
    if (!differences.hasDifferences()) {
        return new Change[0];
    }
    Index index = (Index) changedObject;
    if (index.getRelation() != null && index.getRelation() instanceof Table) {
        if ((((Table) index.getRelation()).getPrimaryKey() != null) && DatabaseObjectComparatorFactory.getInstance().isSameObject(((Table) index.getRelation()).getPrimaryKey().getBackingIndex(), changedObject, differences.getSchemaComparisons(), comparisonDatabase)) {
            return ChangeGeneratorFactory.getInstance().fixChanged(((Table) index.getRelation()).getPrimaryKey(), differences, control, referenceDatabase, comparisonDatabase);
        }
        List<UniqueConstraint> uniqueConstraints = ((Table) index.getRelation()).getUniqueConstraints();
        if (uniqueConstraints != null) {
            for (UniqueConstraint constraint : uniqueConstraints) {
                if ((constraint.getBackingIndex() != null) && DatabaseObjectComparatorFactory.getInstance().isSameObject(constraint.getBackingIndex(), changedObject, differences.getSchemaComparisons(), comparisonDatabase)) {
                    return ChangeGeneratorFactory.getInstance().fixChanged(constraint, differences, control, referenceDatabase, comparisonDatabase);
                }
            }
        }
    }
    DropIndexChange dropIndexChange = createDropIndexChange();
    dropIndexChange.setTableName(index.getRelation().getName());
    dropIndexChange.setIndexName(index.getName());
    CreateIndexChange addIndexChange = createCreateIndexChange();
    addIndexChange.setTableName(index.getRelation().getName());
    List<AddColumnConfig> columns = new ArrayList<>();
    for (Column col : index.getColumns()) {
        columns.add(new AddColumnConfig(col));
    }
    addIndexChange.setColumns(columns);
    addIndexChange.setIndexName(index.getName());
    addIndexChange.setUnique(index.isUnique());
    if (control.getIncludeCatalog()) {
        dropIndexChange.setCatalogName(index.getSchema().getCatalogName());
        addIndexChange.setCatalogName(index.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropIndexChange.setSchemaName(index.getSchema().getName());
        addIndexChange.setSchemaName(index.getSchema().getName());
    }
    Difference columnsDifference = differences.getDifference("columns");
    if (columnsDifference != null) {
        List<Column> referenceColumns = (List<Column>) columnsDifference.getReferenceValue();
        List<Column> comparedColumns = (List<Column>) columnsDifference.getComparedValue();
        StringUtil.StringUtilFormatter<Column> formatter = new StringUtil.StringUtilFormatter<Column>() {

            @Override
            public String toString(Column obj) {
                return obj.toString(false);
            }
        };
        control.setAlreadyHandledChanged(new Index().setRelation(index.getRelation()).setColumns(referenceColumns));
        if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
            control.setAlreadyHandledChanged(new Index().setRelation(index.getRelation()).setColumns(comparedColumns));
        }
        if ((index.isUnique() != null) && index.isUnique()) {
            control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(index.getRelation()).setColumns(referenceColumns));
            if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
                control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(index.getRelation()).setColumns(comparedColumns));
            }
        }
    }
    return new Change[] { dropIndexChange, addIndexChange };
}
Also used : DropIndexChange(liquibase.change.core.DropIndexChange) Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) Difference(liquibase.diff.Difference) DropIndexChange(liquibase.change.core.DropIndexChange) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) Column(liquibase.structure.core.Column) AddColumnConfig(liquibase.change.AddColumnConfig) ArrayList(java.util.ArrayList) List(java.util.List) StringUtil(liquibase.util.StringUtil) CreateIndexChange(liquibase.change.core.CreateIndexChange)

Example 47 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class ChangedPrimaryKeyChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    // don't try to recreate PKs that differ in just clustered
    Difference clusteredDiff = differences.getDifference("clustered");
    if (clusteredDiff != null) {
        if ((clusteredDiff.getReferenceValue() == null) || (clusteredDiff.getComparedValue() == null)) {
            differences.removeDifference("clustered");
        }
    }
    if (!differences.hasDifferences()) {
        return new Change[0];
    }
    PrimaryKey pk = (PrimaryKey) changedObject;
    List<Change> returnList = new ArrayList<>();
    DropPrimaryKeyChange dropPkChange = new DropPrimaryKeyChange();
    dropPkChange.setTableName(pk.getTable().getName());
    returnList.add(dropPkChange);
    AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange();
    addPkChange.setTableName(pk.getTable().getName());
    addPkChange.setColumnNames(pk.getColumnNames());
    addPkChange.setConstraintName(pk.getName());
    if (comparisonDatabase instanceof OracleDatabase) {
        Index backingIndex = pk.getBackingIndex();
        if ((backingIndex != null) && (backingIndex.getName() != null)) {
            Change[] indexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (indexChanges != null) {
                returnList.addAll(Arrays.asList(indexChanges));
            }
            addPkChange.setForIndexName(backingIndex.getName());
            Schema schema = backingIndex.getSchema();
            if (schema != null) {
                if (control.getIncludeCatalog()) {
                    addPkChange.setForIndexCatalogName(schema.getCatalogName());
                }
                if (control.getIncludeSchema()) {
                    addPkChange.setForIndexSchemaName(schema.getName());
                }
            }
        }
    }
    returnList.add(addPkChange);
    if (control.getIncludeCatalog()) {
        dropPkChange.setCatalogName(pk.getSchema().getCatalogName());
        addPkChange.setCatalogName(pk.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropPkChange.setSchemaName(pk.getSchema().getName());
        addPkChange.setSchemaName(pk.getSchema().getName());
    }
    Difference columnDifferences = differences.getDifference("columns");
    List<Column> referenceColumns;
    List<Column> comparedColumns;
    if (columnDifferences == null) {
        referenceColumns = pk.getColumns();
        comparedColumns = pk.getColumns();
    } else {
        referenceColumns = (List<Column>) columnDifferences.getReferenceValue();
        comparedColumns = (List<Column>) columnDifferences.getComparedValue();
    }
    StringUtil.ToStringFormatter formatter = new StringUtil.ToStringFormatter();
    control.setAlreadyHandledChanged(new Index().setRelation(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new Index().setRelation(pk.getTable()).setColumns(comparedColumns));
    }
    control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(pk.getTable()).setColumns(comparedColumns));
    }
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : ArrayList(java.util.ArrayList) Difference(liquibase.diff.Difference) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) Change(liquibase.change.Change) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) OracleDatabase(liquibase.database.core.OracleDatabase) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) StringUtil(liquibase.util.StringUtil)

Example 48 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class HTMLWriter method writeChanges.

protected void writeChanges(String title, Writer fileWriter, List<Change> changes) throws IOException, DatabaseHistoryException, DatabaseException {
    fileWriter.append("<p><TABLE BORDER=\"1\" WIDTH=\"100%\" CELLPADDING=\"3\" CELLSPACING=\"0\" SUMMARY=\"\">\n");
    fileWriter.append("<TR BGCOLOR=\"#CCCCFF\" CLASS=\"TableHeadingColor\">\n");
    fileWriter.append("<TD COLSPAN='4'><FONT SIZE=\"+2\">\n");
    fileWriter.append("<B>");
    fileWriter.append(title);
    fileWriter.append("</B></FONT></TD>\n");
    fileWriter.append("</TR>\n");
    ChangeSet lastChangeSet = null;
    if ((changes == null) || changes.isEmpty()) {
        fileWriter.append("<tr><td>None Found</td></tr>");
    } else {
        for (Change change : changes) {
            if (!change.getChangeSet().equals(lastChangeSet)) {
                lastChangeSet = change.getChangeSet();
                fileWriter.append("<TR BGCOLOR=\"#EEEEFF\" CLASS=\"TableSubHeadingColor\">\n");
                writeTD(fileWriter, "<a href='../changelogs/" + DBDocUtil.toFileName(change.getChangeSet().getFilePath().toLowerCase()) + ".html'>" + change.getChangeSet().getFilePath() + "</a>");
                writeTD(fileWriter, change.getChangeSet().getId());
                writeTD(fileWriter, "<a href='../authors/" + DBDocUtil.toFileName(change.getChangeSet().getAuthor().toLowerCase()) + ".html'>" + StringUtil.escapeHtml(change.getChangeSet().getAuthor().toLowerCase()) + "</a>");
                ChangeSet.RunStatus runStatus = database.getRunStatus(change.getChangeSet());
                if (runStatus.equals(ChangeSet.RunStatus.NOT_RAN)) {
                    String anchor = change.getChangeSet().toString(false).replaceAll("\\W", "_");
                    writeTD(fileWriter, "NOT YET RAN [<a href='../pending/sql.html#" + anchor + "'>SQL</a>]");
                } else if (runStatus.equals(ChangeSet.RunStatus.INVALID_MD5SUM)) {
                    writeTD(fileWriter, "INVALID MD5SUM");
                } else if (runStatus.equals(ChangeSet.RunStatus.ALREADY_RAN)) {
                    writeTD(fileWriter, "Executed " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(database.getRanDate(change.getChangeSet())));
                } else if (runStatus.equals(ChangeSet.RunStatus.RUN_AGAIN)) {
                    writeTD(fileWriter, "Executed, WILL RUN AGAIN");
                } else {
                    throw new RuntimeException("Unknown run status: " + runStatus);
                }
                fileWriter.append("</TR>");
                if (StringUtil.trimToNull(change.getChangeSet().getComments()) != null) {
                    fileWriter.append("<TR><TD BGCOLOR='#EEEEFF' CLASS='TableSubHeadingColor' colspan='4'>").append(change.getChangeSet().getComments()).append("</TD></TR>");
                }
            }
            fileWriter.append("<TR BGCOLOR=\"white\" CLASS=\"TableRowColor\">\n");
            fileWriter.append("<td colspan='4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;").append(change.getConfirmationMessage()).append("</td></TR>");
        }
    }
    fileWriter.append("</TABLE>");
    fileWriter.append("&nbsp;</P>");
}
Also used : Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet)

Example 49 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class MissingIndexChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Index index = (Index) missingObject;
    if (comparisonDatabase instanceof MSSQLDatabase && index.getRelation() instanceof Table) {
        PrimaryKey primaryKey = ((Table) index.getRelation()).getPrimaryKey();
        if ((primaryKey != null) && DatabaseObjectComparatorFactory.getInstance().isSameObject(missingObject, primaryKey.getBackingIndex(), control.getSchemaComparisons(), referenceDatabase)) {
            // will be handled by the PK
            return new Change[0];
        }
    }
    CreateIndexChange change = createCreateIndexChange();
    change.setTableName(index.getRelation().getName());
    if (control.getIncludeTablespace()) {
        change.setTablespace(index.getTablespace());
    }
    if (control.getIncludeCatalog()) {
        change.setCatalogName(index.getRelation().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(index.getRelation().getSchema().getName());
    }
    change.setIndexName(index.getName());
    change.setUnique(((index.isUnique() != null) && index.isUnique()) ? Boolean.TRUE : null);
    change.setClustered(((index.getClustered() != null) && index.getClustered()) ? Boolean.TRUE : null);
    if (referenceDatabase.createsIndexesForForeignKeys()) {
        change.setAssociatedWith(index.getAssociatedWithAsString());
    }
    for (Column column : index.getColumns()) {
        change.addColumn(new AddColumnConfig(column));
    }
    return new Change[] { change };
}
Also used : AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) CreateIndexChange(liquibase.change.core.CreateIndexChange)

Aggregations

Change (liquibase.change.Change)49 ArrayList (java.util.ArrayList)10 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)10 ChangeSet (liquibase.changelog.ChangeSet)9 DatabaseObject (liquibase.structure.DatabaseObject)9 SqlStatement (liquibase.statement.SqlStatement)8 Column (liquibase.structure.core.Column)8 Table (liquibase.structure.core.Table)7 Database (liquibase.database.Database)6 Sql (liquibase.sql.Sql)6 Index (liquibase.structure.core.Index)6 OracleDatabase (liquibase.database.core.OracleDatabase)5 Difference (liquibase.diff.Difference)5 LiquibaseException (liquibase.exception.LiquibaseException)5 ChangeFactory (liquibase.change.ChangeFactory)4 ChangeMetaData (liquibase.change.ChangeMetaData)4 ChangeParameterMetaData (liquibase.change.ChangeParameterMetaData)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 ValidationErrors (liquibase.exception.ValidationErrors)4 AbstractVerifyTest (liquibase.verify.AbstractVerifyTest)4