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()]);
}
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 };
}
Aggregations