Search in sources :

Example 1 with CreateViewChange

use of liquibase.change.core.CreateViewChange in project liquibase by liquibase.

the class ChangedViewChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) changedObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    change.setReplaceIfExists(true);
    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().isEmpty()) {
        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 + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {

            @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());
    }
    List<Change> changes = new ArrayList<>();
    changes.add(change);
    Difference changedRemarks = differences.getDifference("remarks");
    if (changedRemarks != null) {
        SetTableRemarksChange setRemarksChange = new SetTableRemarksChange();
        if (control.getIncludeCatalog()) {
            setRemarksChange.setCatalogName(view.getSchema().getCatalogName());
        }
        if (control.getIncludeSchema()) {
            setRemarksChange.setSchemaName(view.getSchema().getName());
        }
        setRemarksChange.setTableName(view.getName());
        setRemarksChange.setRemarks(view.getRemarks());
        changes.add(setRemarksChange);
    }
    return changes.toArray(new Change[changes.size()]);
}
Also used : CreateViewChange(liquibase.change.core.CreateViewChange) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange) Difference(liquibase.diff.Difference) View(liquibase.structure.core.View) OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange)

Example 2 with CreateViewChange

use of liquibase.change.core.CreateViewChange 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());
    }
    if (view.getRemarks() != null) {
        change.setRemarks(view.getRemarks());
    }
    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().isEmpty()) {
        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 + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {

            @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)

Aggregations

Change (liquibase.change.Change)2 CreateViewChange (liquibase.change.core.CreateViewChange)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 DatabaseObject (liquibase.structure.DatabaseObject)2 Column (liquibase.structure.core.Column)2 View (liquibase.structure.core.View)2 ArrayList (java.util.ArrayList)1 SetTableRemarksChange (liquibase.change.core.SetTableRemarksChange)1 Difference (liquibase.diff.Difference)1