Search in sources :

Example 26 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 27 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 28 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 29 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class LiquibaseDatabaseDiff method performLiquibaseTask.

@Override
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
    ClassLoader cl = null;
    ResourceAccessor fileOpener;
    try {
        cl = getClassLoaderIncludingProjectClasspath();
        Thread.currentThread().setContextClassLoader(cl);
        ClassLoader artifactClassLoader = getMavenArtifactClassLoader();
        fileOpener = getFileOpener(artifactClassLoader);
    } catch (MojoExecutionException e) {
        throw new LiquibaseException("Could not create the class loader, " + e, e);
    }
    Database db = liquibase.getDatabase();
    Database referenceDatabase = CommandLineUtils.createDatabaseObject(fileOpener, referenceUrl, referenceUsername, referencePassword, referenceDriver, referenceDefaultCatalogName, referenceDefaultSchemaName, outputDefaultCatalog, outputDefaultSchema, null, null, propertyProviderClass, null, null, databaseChangeLogTableName, databaseChangeLogLockTableName);
    getLog().info("Performing Diff on database " + db.toString());
    if (diffChangeLogFile != null) {
        try {
            DiffOutputControl diffOutputControl = new DiffOutputControl(diffIncludeCatalog, diffIncludeSchema, diffIncludeTablespace, null).addIncludedSchema(new CatalogAndSchema(referenceDefaultCatalogName, referenceDefaultSchemaName));
            if (diffExcludeObjects != null && diffIncludeObjects != null) {
                throw new UnexpectedLiquibaseException("Cannot specify both excludeObjects and includeObjects");
            }
            if (diffExcludeObjects != null) {
                diffOutputControl.setObjectChangeFilter(new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.EXCLUDE, diffExcludeObjects));
            }
            if (diffIncludeObjects != null) {
                diffOutputControl.setObjectChangeFilter(new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.INCLUDE, diffIncludeObjects));
            }
            CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db, diffOutputControl, StringUtils.trimToNull(diffTypes));
            getLog().info("Differences written to Change Log File, " + diffChangeLogFile);
        } catch (IOException e) {
            throw new LiquibaseException(e);
        } catch (ParserConfigurationException e) {
            throw new LiquibaseException(e);
        }
    } else {
        CommandLineUtils.doDiff(referenceDatabase, db, StringUtils.trimToNull(diffTypes));
    }
}
Also used : ResourceAccessor(liquibase.resource.ResourceAccessor) StandardObjectChangeFilter(liquibase.diff.output.StandardObjectChangeFilter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Database(liquibase.database.Database) DiffOutputControl(liquibase.diff.output.DiffOutputControl) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 30 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class LiquibaseDropAll method performLiquibaseTask.

@Override
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
    if (schemas != null) {
        List<CatalogAndSchema> schemaObjs = new ArrayList<CatalogAndSchema>();
        for (String name : schemas.split(",")) {
            schemaObjs.add(new CatalogAndSchema(catalog, name));
        }
        liquibase.dropAll(schemaObjs.toArray(new CatalogAndSchema[schemaObjs.size()]));
    } else {
        liquibase.dropAll();
    }
}
Also used : ArrayList(java.util.ArrayList) CatalogAndSchema(liquibase.CatalogAndSchema)

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