Search in sources :

Example 16 with Index

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()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) Schema(liquibase.structure.core.Schema) ArrayList(java.util.ArrayList) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change)

Example 17 with Index

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 };
}
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 18 with Index

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;
        }
    }
}
Also used : Index(liquibase.structure.core.Index)

Example 19 with Index

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;
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) BooleanType(liquibase.datatype.core.BooleanType) Index(liquibase.structure.core.Index) Date(java.util.Date) Sql(liquibase.sql.Sql) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) CharType(liquibase.datatype.core.CharType) DateType(liquibase.datatype.core.DateType)

Aggregations

Index (liquibase.structure.core.Index)19 Column (liquibase.structure.core.Column)8 ArrayList (java.util.ArrayList)7 Change (liquibase.change.Change)6 SqlStatement (liquibase.statement.SqlStatement)5 Table (liquibase.structure.core.Table)5 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)4 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)4 Schema (liquibase.structure.core.Schema)4 UniqueConstraint (liquibase.structure.core.UniqueConstraint)4 DatabaseException (liquibase.exception.DatabaseException)3 ColumnConfig (liquibase.change.ColumnConfig)2 DropIndexChange (liquibase.change.core.DropIndexChange)2 DropUniqueConstraintChange (liquibase.change.core.DropUniqueConstraintChange)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 Sql (liquibase.sql.Sql)2 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1