Search in sources :

Example 1 with Catalog

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

the class SchemaSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    Database database = snapshot.getDatabase();
    Schema match = null;
    String catalogName = ((Schema) example).getCatalogName();
    String schemaName = example.getName();
    if (database.supportsSchemas()) {
        if (catalogName == null) {
            catalogName = database.getDefaultCatalogName();
        }
        if (schemaName == null) {
            schemaName = database.getDefaultSchemaName();
        }
    } else {
        if (database.supportsCatalogs()) {
            if (catalogName == null && schemaName != null) {
                catalogName = schemaName;
                schemaName = null;
            }
        } else {
            catalogName = null;
            schemaName = null;
        }
    }
    example = new Schema(catalogName, schemaName);
    // use LEGACY quoting since we're dealing with system objects
    ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
    try {
        if (database.supportsSchemas()) {
            for (String tableSchema : getDatabaseSchemaNames(database)) {
                CatalogAndSchema schemaFromJdbcInfo = toCatalogAndSchema(tableSchema, database);
                Catalog catalog = new Catalog(schemaFromJdbcInfo.getCatalogName());
                Schema schema = new Schema(catalog, tableSchema);
                if (DatabaseObjectComparatorFactory.getInstance().isSameObject(schema, example, snapshot.getSchemaComparisons(), database)) {
                    if (match == null) {
                        match = schema;
                    } else {
                        throw new InvalidExampleException("Found multiple catalog/schemas matching " + ((Schema) example).getCatalogName() + "." + example.getName());
                    }
                }
            }
        } else {
            Catalog catalog = new Catalog(catalogName);
            match = new Schema(catalog, catalogName);
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        database.setObjectQuotingStrategy(currentStrategy);
    }
    if (match != null && (match.getName() == null || match.getName().equalsIgnoreCase(database.getDefaultSchemaName()))) {
        match.setDefault(true);
    }
    return match;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseException(liquibase.exception.DatabaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) Catalog(liquibase.structure.core.Catalog)

Example 2 with Catalog

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

the class CatalogSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!(example instanceof Catalog)) {
        throw new UnexpectedLiquibaseException("Unexpected example type: " + example.getClass().getName());
    }
    Database database = snapshot.getDatabase();
    Catalog match = null;
    String catalogName = example.getName();
    if (catalogName == null && database.supportsCatalogs()) {
        catalogName = database.getDefaultCatalogName();
    }
    example = new Catalog(catalogName);
    try {
        for (String potentialCatalogName : getDatabaseCatalogNames(database)) {
            Catalog catalog = new Catalog(potentialCatalogName);
            if (DatabaseObjectComparatorFactory.getInstance().isSameObject(catalog, example, snapshot.getSchemaComparisons(), database)) {
                if (match == null) {
                    match = catalog;
                } else {
                    throw new InvalidExampleException("Found multiple catalogs matching " + example.getName());
                }
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
    if (match != null && isDefaultCatalog(match, database)) {
        match.setDefault(true);
    }
    return match;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) SQLException(java.sql.SQLException) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) Catalog(liquibase.structure.core.Catalog)

Example 3 with Catalog

use of liquibase.structure.core.Catalog 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 4 with Catalog

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

the class CatalogComparator method isSameObject.

@Override
public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, DatabaseObjectComparatorChain chain) {
    if (!(databaseObject1 instanceof Catalog && databaseObject2 instanceof Catalog)) {
        return false;
    }
    if (!accordingTo.supportsCatalogs()) {
        return true;
    }
    String object1Name;
    if (((Catalog) databaseObject1).isDefault()) {
        object1Name = null;
    } else {
        object1Name = databaseObject1.getName();
    }
    String object2Name;
    if (((Catalog) databaseObject2).isDefault()) {
        object2Name = null;
    } else {
        object2Name = databaseObject2.getName();
    }
    CatalogAndSchema thisSchema = new CatalogAndSchema(object1Name, null).standardize(accordingTo);
    CatalogAndSchema otherSchema = new CatalogAndSchema(object2Name, null).standardize(accordingTo);
    if (thisSchema.getCatalogName() == null) {
        return otherSchema.getCatalogName() == null;
    }
    if (thisSchema.getCatalogName().equalsIgnoreCase(otherSchema.getCatalogName())) {
        return true;
    }
    if (accordingTo.supportsSchemas()) {
        //no need to check schema mappings
        return false;
    }
    //check with schemaComparisons
    if (chain.getSchemaComparisons() != null && chain.getSchemaComparisons().length > 0) {
        for (CompareControl.SchemaComparison comparison : chain.getSchemaComparisons()) {
            String comparisonCatalog1;
            String comparisonCatalog2;
            if (accordingTo.supportsSchemas()) {
                comparisonCatalog1 = comparison.getComparisonSchema().getSchemaName();
                comparisonCatalog2 = comparison.getReferenceSchema().getSchemaName();
            } else if (accordingTo.supportsCatalogs()) {
                comparisonCatalog1 = comparison.getComparisonSchema().getCatalogName();
                comparisonCatalog2 = comparison.getReferenceSchema().getCatalogName();
            } else {
                break;
            }
            String finalCatalog1 = thisSchema.getCatalogName();
            String finalCatalog2 = otherSchema.getCatalogName();
            if (comparisonCatalog1 != null && comparisonCatalog1.equalsIgnoreCase(finalCatalog1)) {
                finalCatalog1 = comparisonCatalog2;
            } else if (comparisonCatalog2 != null && comparisonCatalog2.equalsIgnoreCase(finalCatalog1)) {
                finalCatalog1 = comparisonCatalog1;
            }
            if (StringUtils.trimToEmpty(finalCatalog1).equalsIgnoreCase(StringUtils.trimToEmpty(finalCatalog2))) {
                return true;
            }
            if (comparisonCatalog1 != null && comparisonCatalog1.equalsIgnoreCase(finalCatalog2)) {
                finalCatalog2 = comparisonCatalog2;
            } else if (comparisonCatalog2 != null && comparisonCatalog2.equalsIgnoreCase(finalCatalog2)) {
                finalCatalog2 = comparisonCatalog1;
            }
            if (StringUtils.trimToEmpty(finalCatalog1).equalsIgnoreCase(StringUtils.trimToEmpty(finalCatalog2))) {
                return true;
            }
        }
    }
    return false;
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) CatalogAndSchema(liquibase.CatalogAndSchema) Catalog(liquibase.structure.core.Catalog)

Aggregations

Catalog (liquibase.structure.core.Catalog)4 SQLException (java.sql.SQLException)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)2 Database (liquibase.database.Database)2 DatabaseException (liquibase.exception.DatabaseException)2 InvalidExampleException (liquibase.snapshot.InvalidExampleException)2 Schema (liquibase.structure.core.Schema)2 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)1 CompareControl (liquibase.diff.compare.CompareControl)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1