Search in sources :

Example 16 with Schema

use of liquibase.structure.core.Schema 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<Change>();
    UniqueConstraint uniqueConstraint = (UniqueConstraint) changedObject;
    DropUniqueConstraintChange dropUniqueConstraintChange = createDropUniqueConstraintChange();
    dropUniqueConstraintChange.setTableName(uniqueConstraint.getTable().getName());
    dropUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
    AddUniqueConstraintChange addUniqueConstraintChange = createAddUniqueConstraintChange();
    addUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
    addUniqueConstraintChange.setTableName(uniqueConstraint.getTable().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 Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class StandardDiffGenerator method compareObjectType.

protected <T extends DatabaseObject> void compareObjectType(Class<T> type, DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, DiffResult diffResult) {
    Database comparisonDatabase = comparisonSnapshot.getDatabase();
    Database referenceDatabase = referenceSnapshot.getDatabase();
    CompareControl.SchemaComparison[] schemaComparisons = diffResult.getCompareControl().getSchemaComparisons();
    if (schemaComparisons != null) {
        for (CompareControl.SchemaComparison schemaComparison : schemaComparisons) {
            for (T referenceObject : referenceSnapshot.get(type)) {
                //                if (referenceObject instanceof Table && referenceSnapshot.getDatabase().isLiquibaseTable(referenceSchema, referenceObject.getName())) {
                //                    continue;
                //                }
                Schema referenceObjectSchema = referenceObject.getSchema();
                if (referenceObjectSchema != null && referenceObjectSchema.getName() != null) {
                    //don't filter out null-named schemas. May actually be catalog-level objects that should be included
                    if (!StringUtils.trimToEmpty(referenceObjectSchema.toCatalogAndSchema().standardize(referenceDatabase).getSchemaName()).equalsIgnoreCase(StringUtils.trimToEmpty(schemaComparison.getReferenceSchema().standardize(referenceDatabase).getSchemaName()))) {
                        continue;
                    }
                }
                T comparisonObject = comparisonSnapshot.get(referenceObject);
                if (comparisonObject == null) {
                    diffResult.addMissingObject(referenceObject);
                } else {
                    ObjectDifferences differences = DatabaseObjectComparatorFactory.getInstance().findDifferences(referenceObject, comparisonObject, comparisonDatabase, diffResult.getCompareControl());
                    if (differences.hasDifferences()) {
                        diffResult.addChangedObject(referenceObject, differences);
                    }
                }
            }
            //
            for (T comparisonObject : comparisonSnapshot.get(type)) {
                //                if (targetObject instanceof Table && comparisonSnapshot.getDatabase().isLiquibaseTable(comparisonSchema, targetObject.getName())) {
                //                    continue;
                //                }
                Schema comparisonObjectSchema = comparisonObject.getSchema();
                if (comparisonObjectSchema != null) {
                    String comparisonObjectSchemaName = StringUtils.trimToEmpty(comparisonObjectSchema.toCatalogAndSchema().standardize(comparisonDatabase).getSchemaName());
                    String schemaComparisonName1 = StringUtils.trimToEmpty(schemaComparison.getComparisonSchema().standardize(comparisonDatabase).getSchemaName());
                    String schemaComparisonName2 = StringUtils.trimToEmpty(schemaComparison.getReferenceSchema().standardize(comparisonDatabase).getSchemaName());
                    if (comparisonObjectSchemaName.equals("") && !schemaComparisonName1.equals("") && !schemaComparisonName2.equals("")) {
                        comparisonObjectSchemaName = StringUtils.trimToEmpty(comparisonObjectSchema.getName());
                    }
                    if (!(comparisonObjectSchemaName.equalsIgnoreCase(schemaComparisonName1) || comparisonObjectSchemaName.equals(schemaComparisonName2))) {
                        continue;
                    }
                }
                if (referenceSnapshot.get(comparisonObject) == null) {
                    diffResult.addUnexpectedObject(comparisonObject);
                }
            //            }
            }
        }
    //todo: add logic for when container is missing or unexpected also
    }
//    /**
//     * Removes duplicate Indexes from the DiffResult object.
//     *
//     * @param indexes [IN/OUT] - A set of Indexes to be updated.
//     */
//    private void removeDuplicateIndexes( SortedSet<Index> indexes )
//    {
//        SortedSet<Index> combinedIndexes = new TreeSet<Index>();
//        SortedSet<Index> indexesToRemove = new TreeSet<Index>();
//
//        // Find Indexes with the same name, copy their columns into the first one,
//        // then remove the duplicate Indexes.
//        for ( Index idx1 : indexes )
//        {
//            if ( !combinedIndexes.contains( idx1 ) )
//            {
//                for ( Index idx2 : indexes.tailSet( idx1 ) )
//                {
//                    if ( idx1 == idx2 ) {
//                        continue;
//                    }
//
//                    String index1Name = StringUtils.trimToEmpty(idx1.getName());
//                    String index2Name = StringUtils.trimToEmpty(idx2.getName());
//                    if ( index1Name.equalsIgnoreCase(index2Name)
//                            && idx1.getTable().getName().equalsIgnoreCase( idx2.getTable().getName() ) )
//                    {
//                        for ( String column : idx2.getColumns() )
//                        {
//                            if ( !idx1.getColumns().contains( column ) ) {
//                                idx1.getColumns().add( column );
//                            }
//                        }
//
//                        indexesToRemove.add( idx2 );
//                    }
//                }
//
//                combinedIndexes.add( idx1 );
//            }
//        }
//
//        indexes.removeAll( indexesToRemove );
//    }
//
//    /**
//     * Removes duplicate Unique Constraints from the DiffResult object.
//     *
//     * @param uniqueConstraints [IN/OUT] - A set of Unique Constraints to be updated.
//     */
//    private void removeDuplicateUniqueConstraints( SortedSet<UniqueConstraint> uniqueConstraints ) {
//        SortedSet<UniqueConstraint> combinedConstraints = new TreeSet<UniqueConstraint>();
//        SortedSet<UniqueConstraint> constraintsToRemove = new TreeSet<UniqueConstraint>();
//
//        // Find UniqueConstraints with the same name, copy their columns into the first one,
//        // then remove the duplicate UniqueConstraints.
//        for ( UniqueConstraint uc1 : uniqueConstraints )
//        {
//            if ( !combinedConstraints.contains( uc1 ) )
//            {
//                for ( UniqueConstraint uc2 : uniqueConstraints.tailSet( uc1 ) )
//                {
//                    if ( uc1 == uc2 ) {
//                        continue;
//                    }
//
//                    if ( uc1.getName().equalsIgnoreCase( uc2.getName() )
//                            && uc1.getTable().getName().equalsIgnoreCase( uc2.getTable().getName() ) )
//                    {
//                        for ( String column : uc2.getColumns() )
//                        {
//                            if ( !uc1.getColumns().contains( column ) ) {
//                                uc1.getColumns().add( column );
//                            }
//                        }
//
//                        constraintsToRemove.add( uc2 );
//                    }
//                }
//
//                combinedConstraints.add( uc1 );
//            }
//        }
//
//        uniqueConstraints.removeAll( constraintsToRemove );
//    }
}
Also used : Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl)

Example 18 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class DiffOutputControl method shouldOutput.

public boolean shouldOutput(DatabaseObject object, Database accordingTo) {
    if (includeSchemas.size() > 0) {
        Schema schema = object.getSchema();
        if (schema == null) {
            return true;
        }
        CatalogAndSchema objectCatalogAndSchema = schema.toCatalogAndSchema().standardize(accordingTo);
        for (CatalogAndSchema catalogAndSchema : includeSchemas) {
            catalogAndSchema = schema.toCatalogAndSchema().standardize(accordingTo);
            if (objectCatalogAndSchema.equals(catalogAndSchema, accordingTo)) {
                return true;
            }
        }
        return false;
    } else {
        return true;
    }
}
Also used : Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) CatalogAndSchema(liquibase.CatalogAndSchema)

Example 19 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class DatabaseObjectComparatorFactory method isSameObject.

public boolean isSameObject(DatabaseObject object1, DatabaseObject object2, CompareControl.SchemaComparison[] schemaComparisons, Database accordingTo) {
    if (object1 == null && object2 == null) {
        return true;
    }
    if (object1 instanceof Schema || object2 instanceof Schema) {
        if (object1 == null) {
            object1 = new Schema();
        }
        if (object2 == null) {
            object2 = new Schema();
        }
    } else if (object1 instanceof Catalog || object2 instanceof Catalog) {
        if (object1 == null) {
            object1 = new Catalog();
        }
        if (object2 == null) {
            object2 = new Catalog();
        }
    }
    if (object1 == null || object2 == null) {
        return false;
    }
    String snapshotId1 = object1.getSnapshotId();
    String snapshotId2 = object2.getSnapshotId();
    if (snapshotId1 != null && snapshotId2 != null) {
        if (snapshotId1.equals(snapshotId2)) {
            return true;
        }
    }
    boolean aHashMatches = false;
    List<String> hash1 = Arrays.asList(hash(object1, schemaComparisons, accordingTo));
    List<String> hash2 = Arrays.asList(hash(object2, schemaComparisons, accordingTo));
    for (String hash : hash1) {
        if (hash2.contains(hash)) {
            aHashMatches = true;
            break;
        }
    }
    if (!aHashMatches) {
        return false;
    }
    return createComparatorChain(object1.getClass(), schemaComparisons, accordingTo).isSameObject(object1, object2, accordingTo);
}
Also used : Schema(liquibase.structure.core.Schema) Catalog(liquibase.structure.core.Catalog)

Example 20 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class ViewExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    String currentSchemaName;
    String currentCatalogName;
    try {
        currentCatalogName = getCatalogName();
        currentSchemaName = getSchemaName();
        if (!SnapshotGeneratorFactory.getInstance().has(new View().setName(database.correctObjectName(getViewName(), View.class)).setSchema(new Schema(currentCatalogName, currentSchemaName)), database)) {
            throw new PreconditionFailedException("View " + database.escapeTableName(currentCatalogName, currentSchemaName, getViewName()) + " does not exist", changeLog, this);
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Schema(liquibase.structure.core.Schema) View(liquibase.structure.core.View)

Aggregations

Schema (liquibase.structure.core.Schema)23 CatalogAndSchema (liquibase.CatalogAndSchema)9 Table (liquibase.structure.core.Table)6 Database (liquibase.database.Database)5 OracleDatabase (liquibase.database.core.OracleDatabase)4 SQLException (java.sql.SQLException)3 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)3 DatabaseException (liquibase.exception.DatabaseException)3 RawSqlStatement (liquibase.statement.core.RawSqlStatement)3 Column (liquibase.structure.core.Column)3 View (liquibase.structure.core.View)3 InformixDatabase (liquibase.database.core.InformixDatabase)2 CachedRow (liquibase.snapshot.CachedRow)2 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)2 UnparsedSql (liquibase.sql.UnparsedSql)2 Catalog (liquibase.structure.core.Catalog)2 Index (liquibase.structure.core.Index)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1