use of liquibase.structure.core.Index in project liquibase by liquibase.
the class ChangedUniqueConstraintChangeGenerator method fixChanged.
@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
List<Change> returnList = new ArrayList<>();
UniqueConstraint uniqueConstraint = (UniqueConstraint) changedObject;
DropUniqueConstraintChange dropUniqueConstraintChange = createDropUniqueConstraintChange();
dropUniqueConstraintChange.setTableName(uniqueConstraint.getRelation().getName());
dropUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
AddUniqueConstraintChange addUniqueConstraintChange = createAddUniqueConstraintChange();
addUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
addUniqueConstraintChange.setTableName(uniqueConstraint.getRelation().getName());
addUniqueConstraintChange.setColumnNames(uniqueConstraint.getColumnNames());
returnList.add(dropUniqueConstraintChange);
if (control.getIncludeCatalog()) {
dropUniqueConstraintChange.setCatalogName(uniqueConstraint.getSchema().getCatalogName());
addUniqueConstraintChange.setCatalogName(uniqueConstraint.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
dropUniqueConstraintChange.setSchemaName(uniqueConstraint.getSchema().getName());
addUniqueConstraintChange.setSchemaName(uniqueConstraint.getSchema().getName());
}
Index backingIndex = uniqueConstraint.getBackingIndex();
if (comparisonDatabase instanceof OracleDatabase) {
if ((backingIndex != null) && (backingIndex.getName() != null)) {
Change[] missingIndexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
if (missingIndexChanges != null) {
returnList.addAll(Arrays.asList(missingIndexChanges));
}
addUniqueConstraintChange.setForIndexName(backingIndex.getName());
Schema schema = backingIndex.getSchema();
if (schema != null) {
if (control.getIncludeCatalog()) {
addUniqueConstraintChange.setForIndexCatalogName(schema.getCatalogName());
}
if (control.getIncludeSchema()) {
addUniqueConstraintChange.setForIndexSchemaName(schema.getName());
}
}
}
}
control.setAlreadyHandledChanged(backingIndex);
returnList.add(addUniqueConstraintChange);
return returnList.toArray(new Change[returnList.size()]);
}
use of liquibase.structure.core.Index 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 };
}
use of liquibase.structure.core.Index in project liquibase by liquibase.
the class IndexComparator method isSameObject.
@Override
public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, DatabaseObjectComparatorChain chain) {
if (!((databaseObject1 instanceof Index) && (databaseObject2 instanceof Index))) {
return false;
}
Index thisIndex = (Index) databaseObject1;
Index otherIndex = (Index) databaseObject2;
int thisIndexSize = thisIndex.getColumns().size();
int otherIndexSize = otherIndex.getColumns().size();
if ((thisIndex.getRelation() != null) && (otherIndex.getRelation() != null)) {
if (!DatabaseObjectComparatorFactory.getInstance().isSameObject(thisIndex.getRelation(), otherIndex.getRelation(), chain.getSchemaComparisons(), accordingTo)) {
return false;
}
if ((databaseObject1.getSchema() != null) && (databaseObject2.getSchema() != null) && !DatabaseObjectComparatorFactory.getInstance().isSameObject(databaseObject1.getSchema(), databaseObject2.getSchema(), chain.getSchemaComparisons(), accordingTo)) {
return false;
}
if ((databaseObject1.getName() != null) && (databaseObject2.getName() != null) && DefaultDatabaseObjectComparator.nameMatches(databaseObject1, databaseObject2, accordingTo)) {
return true;
} else {
if ((thisIndexSize == 0) || (otherIndexSize == 0)) {
return DefaultDatabaseObjectComparator.nameMatches(databaseObject1, databaseObject2, accordingTo);
}
if ((thisIndexSize > 0) && (otherIndexSize > 0) && (thisIndexSize != otherIndexSize)) {
return false;
}
for (int i = 0; i < otherIndexSize; i++) {
if (!DatabaseObjectComparatorFactory.getInstance().isSameObject(thisIndex.getColumns().get(i), otherIndex.getColumns().get(i), chain.getSchemaComparisons(), accordingTo)) {
return false;
}
}
return true;
}
} else {
if ((thisIndexSize > 0) && (otherIndexSize > 0) && (thisIndexSize != otherIndexSize)) {
return false;
}
if (!DefaultDatabaseObjectComparator.nameMatches(databaseObject1, databaseObject2, accordingTo)) {
return false;
}
if ((databaseObject1.getSchema() != null) && (databaseObject2.getSchema() != null)) {
return DatabaseObjectComparatorFactory.getInstance().isSameObject(databaseObject1.getSchema(), databaseObject2.getSchema(), chain.getSchemaComparisons(), accordingTo);
} else {
return true;
}
}
}
use of liquibase.structure.core.Index in project liquibase by liquibase.
the class AddDefaultValueGeneratorSQLite method generateSql.
@Override
public Sql[] generateSql(final AddDefaultValueStatement statement, final Database database, SqlGeneratorChain sqlGeneratorChain) {
// Workaround implemented by replacing a table with a new one (duplicate)
// with default value set on the specified column
final SQLiteDatabase.AlterTableVisitor alterTableVisitor = new SQLiteDatabase.AlterTableVisitor() {
@Override
public ColumnConfig[] getColumnsToAdd() {
return new ColumnConfig[0];
}
@Override
public boolean copyThisColumn(ColumnConfig column) {
return true;
}
@Override
public boolean createThisColumn(ColumnConfig column) {
// update the column to set Default value while copying
if (column.getName().equals(statement.getColumnName())) {
if (statement.getDefaultValueConstraintName() != null) {
column.setDefaultValueConstraintName(statement.getDefaultValueConstraintName());
}
// could be of string, numeric, boolean and date
Object defaultValue = statement.getDefaultValue();
LiquibaseDataType dataType = DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database);
if (dataType instanceof BooleanType) {
// validation is done before hand so the defaultValue is instance of dataType
if (defaultValue instanceof Boolean) {
column.setDefaultValueBoolean((Boolean) defaultValue);
} else {
column.setDefaultValueBoolean(defaultValue.toString());
}
} else if (dataType instanceof CharType) {
column.setDefaultValue(defaultValue.toString());
} else if (dataType instanceof DateType) {
if (defaultValue instanceof Date) {
column.setDefaultValueDate((Date) defaultValue);
} else {
column.setDefaultValueDate(defaultValue.toString());
}
} else {
// fallback??
column.setDefaultValue(defaultValue.toString());
}
}
return true;
}
@Override
public boolean createThisIndex(Index index) {
return true;
}
};
Sql[] generatedSqls = SQLiteDatabase.getAlterTableSqls(database, alterTableVisitor, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
return generatedSqls;
}
Aggregations