Search in sources :

Example 6 with Change

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

the class DBDocVisitor method visit.

@Override
public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
    ChangeSet.RunStatus runStatus = this.database.getRunStatus(changeSet);
    if (rootChangeLogName == null) {
        rootChangeLogName = changeSet.getFilePath();
    }
    if (rootChangeLog == null) {
        this.rootChangeLog = databaseChangeLog;
    }
    if (!changesByAuthor.containsKey(changeSet.getAuthor())) {
        changesByAuthor.put(changeSet.getAuthor(), new ArrayList<Change>());
    }
    if (!changesToRunByAuthor.containsKey(changeSet.getAuthor())) {
        changesToRunByAuthor.put(changeSet.getAuthor(), new ArrayList<Change>());
    }
    boolean toRun = runStatus.equals(ChangeSet.RunStatus.NOT_RAN) || runStatus.equals(ChangeSet.RunStatus.RUN_AGAIN);
    for (Change change : changeSet.getChanges()) {
        if (toRun) {
            changesToRunByAuthor.get(changeSet.getAuthor()).add(change);
            changesToRun.add(change);
        } else {
            changesByAuthor.get(changeSet.getAuthor()).add(change);
            recentChanges.add(0, change);
        }
    }
    ChangeLogInfo changeLogInfo = new ChangeLogInfo(changeSet.getChangeLog().getLogicalFilePath(), changeSet.getChangeLog().getPhysicalFilePath());
    if (!changeLogs.contains(changeLogInfo)) {
        changeLogs.add(changeLogInfo);
    }
    for (Change change : changeSet.getChanges()) {
        Set<DatabaseObject> affectedDatabaseObjects = change.getAffectedDatabaseObjects(database);
        if (affectedDatabaseObjects != null) {
            for (DatabaseObject dbObject : affectedDatabaseObjects) {
                if (toRun) {
                    if (!changesToRunByObject.containsKey(dbObject)) {
                        changesToRunByObject.put(dbObject, new ArrayList<Change>());
                    }
                    changesToRunByObject.get(dbObject).add(change);
                } else {
                    if (!changesByObject.containsKey(dbObject)) {
                        changesByObject.put(dbObject, new ArrayList<Change>());
                    }
                    changesByObject.get(dbObject).add(change);
                }
            }
        }
    }
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet)

Example 7 with Change

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

the class MissingUniqueConstraintChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();
    UniqueConstraint uc = (UniqueConstraint) missingObject;
    if (uc.getTable() == null) {
        return null;
    }
    AddUniqueConstraintChange change = new AddUniqueConstraintChange();
    change.setTableName(uc.getTable().getName());
    if (uc.getBackingIndex() != null && control.getIncludeTablespace()) {
        change.setTablespace(uc.getBackingIndex().getTablespace());
    }
    if (control.getIncludeCatalog()) {
        change.setCatalogName(uc.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(uc.getTable().getSchema().getName());
    }
    change.setConstraintName(uc.getName());
    change.setColumnNames(uc.getColumnNames());
    change.setDeferrable(uc.isDeferrable() ? Boolean.TRUE : null);
    change.setInitiallyDeferred(uc.isInitiallyDeferred() ? Boolean.TRUE : null);
    change.setDisabled(uc.isDisabled() ? Boolean.TRUE : null);
    if (referenceDatabase instanceof MSSQLDatabase) {
        change.setClustered(uc.isClustered() ? Boolean.TRUE : null);
    }
    if (comparisonDatabase instanceof OracleDatabase) {
        Index backingIndex = uc.getBackingIndex();
        if (backingIndex != null && backingIndex.getName() != null) {
            Change[] changes = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (changes != null) {
                returnList.addAll(Arrays.asList(changes));
                change.setForIndexName(backingIndex.getName());
                Schema schema = backingIndex.getSchema();
                if (schema != null) {
                    if (control.getIncludeCatalog()) {
                        change.setForIndexCatalogName(schema.getCatalogName());
                    }
                    if (control.getIncludeSchema()) {
                        change.setForIndexSchemaName(schema.getName());
                    }
                }
            }
        }
    }
    Index backingIndex = uc.getBackingIndex();
    //        if (backingIndex == null) {
    //            Index exampleIndex = new Index().setTable(uc.getTable());
    //            for (String col : uc.getColumns()) {
    //                exampleIndex.getColumns().add(col);
    //            }
    //            control.setAlreadyHandledMissing(exampleIndex);
    //        } else {
    control.setAlreadyHandledMissing(backingIndex);
    //        }
    returnList.add(change);
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) ArrayList(java.util.ArrayList) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 8 with Change

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

the class MissingViewChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) missingObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if (comparisonDatabase instanceof OracleDatabase && view.getColumns() != null && view.getColumns().size() > 0) {
        String viewName;
        if (change.getCatalogName() == null && change.getSchemaName() == null) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtils.join(view.getColumns(), ", ", new StringUtils.StringUtilsFormatter() {

            @Override
            public String toString(Object obj) {
                if (((Column) obj).getComputed() != null && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    return new Change[] { change };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) CreateViewChange(liquibase.change.core.CreateViewChange) DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) View(liquibase.structure.core.View)

Example 9 with Change

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

the class UnexpectedColumnChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Column column = (Column) unexpectedObject;
    if (column.getComputed() != null && column.getComputed()) {
        //not really a column to drop, probably part of an index or something
        return null;
    }
    if (column.getRelation() instanceof View) {
        return null;
    }
    if (column.getRelation().getSnapshotId() == null) {
        //not an actual table, maybe an alias, maybe in a different schema. Don't fix it.
        return null;
    }
    DropColumnChange change = new DropColumnChange();
    change.setTableName(column.getRelation().getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(column.getRelation().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(column.getRelation().getSchema().getName());
    }
    change.setColumnName(column.getName());
    return new Change[] { change };
}
Also used : DropColumnChange(liquibase.change.core.DropColumnChange) DropColumnChange(liquibase.change.core.DropColumnChange) Change(liquibase.change.Change)

Example 10 with Change

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

the class UnexpectedForeignKeyChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    ForeignKey fk = (ForeignKey) unexpectedObject;
    DropForeignKeyConstraintChange change = new DropForeignKeyConstraintChange();
    change.setConstraintName(fk.getName());
    change.setBaseTableName(fk.getForeignKeyTable().getName());
    if (control.getIncludeCatalog()) {
        change.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
    }
    Index backingIndex = fk.getBackingIndex();
    //        if (backingIndex == null) {
    //            Index exampleIndex = new Index().setTable(fk.getForeignKeyTable());
    //            for (String col : fk.getForeignKeyColumns().split("\\s*,\\s*")) {
    //                exampleIndex.getColumns().add(col);
    //            }
    //            control.setAlreadyHandledUnexpected(exampleIndex);
    //        } else {
    control.setAlreadyHandledUnexpected(backingIndex);
    return new Change[] { change };
}
Also used : DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange) Change(liquibase.change.Change) DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange)

Aggregations

Change (liquibase.change.Change)44 ArrayList (java.util.ArrayList)9 Column (liquibase.structure.core.Column)9 ChangeSet (liquibase.changelog.ChangeSet)7 DatabaseObject (liquibase.structure.DatabaseObject)7 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)6 EmptyChange (liquibase.change.core.EmptyChange)6 RawSQLChange (liquibase.change.core.RawSQLChange)6 OracleDatabase (liquibase.database.core.OracleDatabase)6 Database (liquibase.database.Database)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 AddColumnConfig (liquibase.change.AddColumnConfig)3 ChangeFactory (liquibase.change.ChangeFactory)3 ChangeMetaData (liquibase.change.ChangeMetaData)3 ChangeParameterMetaData (liquibase.change.ChangeParameterMetaData)3 CreateIndexChange (liquibase.change.core.CreateIndexChange)3 Difference (liquibase.diff.Difference)3 Executor (liquibase.executor.Executor)3 ParsedNodeException (liquibase.parser.core.ParsedNodeException)3