Search in sources :

Example 21 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class SnapshotCommand method run.

@Override
protected SnapshotCommandResult run() throws Exception {
    SnapshotControl snapshotControl = new SnapshotControl(database);
    snapshotControl.setSnapshotListener(snapshotListener);
    CatalogAndSchema[] schemas = this.schemas;
    if (schemas == null) {
        schemas = new CatalogAndSchema[] { database.getDefaultSchema() };
    }
    ObjectQuotingStrategy originalQuotingStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
    DatabaseSnapshot snapshot;
    try {
        snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, database, snapshotControl);
    } finally {
        database.setObjectQuotingStrategy(originalQuotingStrategy);
    }
    snapshot.setMetadata(this.getSnapshotMetadata());
    return new SnapshotCommandResult(snapshot);
}
Also used : CatalogAndSchema(liquibase.CatalogAndSchema) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 22 with CatalogAndSchema

use of liquibase.CatalogAndSchema 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 23 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class CompareControl method computeSchemas.

public static ComputedSchemas computeSchemas(String schemaNames, String referenceSchemaNames, String outputSchemaNames, String defaultCatalogName, String defaultSchemaName, String referenceDefaultCatalogName, String referenceDefaultSchemaName, Database database) {
    //Make sure either both schemaNames and referenceSchemaNames are set or both are null. If only one is set, make them equal
    if (schemaNames == null && referenceSchemaNames == null) {
        //they will be set to the defaults
        ;
    } else if (schemaNames == null && referenceSchemaNames != null) {
        schemaNames = referenceSchemaNames;
    } else if (schemaNames != null && referenceSchemaNames == null) {
        referenceSchemaNames = schemaNames;
    }
    if (schemaNames == null && outputSchemaNames != null) {
        if (defaultSchemaName == null) {
            schemaNames = database.getDefaultSchemaName();
        } else {
            schemaNames = defaultSchemaName;
        }
        referenceSchemaNames = schemaNames;
    }
    ComputedSchemas returnObj = new ComputedSchemas();
    if (referenceSchemaNames == null) {
        returnObj.finalSchemaComparisons = new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(new CatalogAndSchema(referenceDefaultCatalogName, referenceDefaultSchemaName), new CatalogAndSchema(defaultCatalogName, defaultSchemaName)) };
        returnObj.finalTargetSchemas = new CatalogAndSchema[] { new CatalogAndSchema(defaultCatalogName, defaultSchemaName) };
    } else {
        List<SchemaComparison> schemaComparisons = new ArrayList<CompareControl.SchemaComparison>();
        List<CatalogAndSchema> referenceSchemas = new ArrayList<CatalogAndSchema>();
        List<CatalogAndSchema> targetSchemas = new ArrayList<CatalogAndSchema>();
        List<String> splitReferenceSchemaNames = StringUtils.splitAndTrim(referenceSchemaNames, ",");
        List<String> splitSchemaNames = StringUtils.splitAndTrim(schemaNames, ",");
        List<String> splitOutputSchemaNames = StringUtils.splitAndTrim(StringUtils.trimToNull(outputSchemaNames), ",");
        if (splitReferenceSchemaNames.size() != splitSchemaNames.size()) {
            throw new UnexpectedLiquibaseException("You must specify the same number of schemas in --schemas and --referenceSchemas");
        }
        if (splitOutputSchemaNames != null && splitOutputSchemaNames.size() != splitSchemaNames.size()) {
            throw new UnexpectedLiquibaseException("You must specify the same number of schemas in --schemas and --outputSchemasAs");
        }
        for (int i = 0; i < splitReferenceSchemaNames.size(); i++) {
            String referenceSchema = splitReferenceSchemaNames.get(i);
            String targetSchema = splitSchemaNames.get(i);
            String outputSchema = null;
            if (splitOutputSchemaNames != null) {
                outputSchema = splitOutputSchemaNames.get(i);
            }
            CatalogAndSchema correctedTargetSchema = new CatalogAndSchema(null, targetSchema).customize(database);
            CatalogAndSchema correctedReferenceSchema = new CatalogAndSchema(null, referenceSchema).customize(database);
            SchemaComparison comparison = new SchemaComparison(correctedReferenceSchema, correctedTargetSchema);
            comparison.setOutputSchemaAs(outputSchema);
            schemaComparisons.add(comparison);
            referenceSchemas.add(correctedReferenceSchema);
            targetSchemas.add(correctedTargetSchema);
        }
        returnObj.finalSchemaComparisons = schemaComparisons.toArray(new CompareControl.SchemaComparison[schemaComparisons.size()]);
        returnObj.finalTargetSchemas = targetSchemas.toArray(new CatalogAndSchema[targetSchemas.size()]);
    }
    return returnObj;
}
Also used : CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 24 with CatalogAndSchema

use of liquibase.CatalogAndSchema 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)

Example 25 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class FindForeignKeyConstraintsGeneratorMySQL method generateSql.

@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema schema = new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()).customize(database);
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT ");
    sb.append("RC.TABLE_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
    sb.append("KCU.COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
    sb.append("RC.REFERENCED_TABLE_NAME ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
    sb.append("KCU.REFERENCED_COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(", ");
    sb.append("RC.CONSTRAINT_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
    sb.append("FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,");
    sb.append("     INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ");
    sb.append("WHERE RC.TABLE_NAME = KCU.TABLE_NAME ");
    sb.append("AND RC.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA ");
    sb.append("AND RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME ");
    sb.append("AND RC.TABLE_NAME = '").append(statement.getBaseTableName()).append("' ");
    sb.append("AND RC.CONSTRAINT_SCHEMA = '").append(schema.getCatalogName()).append("'");
    sb.append("AND KCU.TABLE_SCHEMA = '").append(schema.getCatalogName()).append("'");
    return new Sql[] { new UnparsedSql(sb.toString()) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) CatalogAndSchema(liquibase.CatalogAndSchema) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Aggregations

CatalogAndSchema (liquibase.CatalogAndSchema)36 Database (liquibase.database.Database)9 DatabaseException (liquibase.exception.DatabaseException)9 CompareControl (liquibase.diff.compare.CompareControl)8 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 Sql (liquibase.sql.Sql)7 UnparsedSql (liquibase.sql.UnparsedSql)7 Liquibase (liquibase.Liquibase)5 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)5 InvalidExampleException (liquibase.snapshot.InvalidExampleException)5 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 LiquibaseException (liquibase.exception.LiquibaseException)4 LockService (liquibase.lockservice.LockService)3 SnapshotControl (liquibase.snapshot.SnapshotControl)3 Schema (liquibase.structure.core.Schema)3 IOException (java.io.IOException)2