Search in sources :

Example 6 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy 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 7 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy in project liquibase by liquibase.

the class DiffCommand method createTargetSnapshot.

protected DatabaseSnapshot createTargetSnapshot() throws DatabaseException, InvalidExampleException {
    CatalogAndSchema[] schemas;
    if (compareControl == null || compareControl.getSchemaComparisons() == null) {
        schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
    } else {
        schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
        int i = 0;
        for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
            CatalogAndSchema schema;
            if (targetDatabase.supportsSchemas()) {
                schema = new CatalogAndSchema(targetDatabase.getDefaultCatalogName(), comparison.getComparisonSchema().getSchemaName());
            } else {
                schema = new CatalogAndSchema(comparison.getComparisonSchema().getSchemaName(), comparison.getComparisonSchema().getSchemaName());
            }
            schemas[i++] = schema;
        }
    }
    SnapshotControl snapshotControl = getTargetSnapshotControl();
    if (snapshotControl == null) {
        snapshotControl = new SnapshotControl(targetDatabase, snapshotTypes);
    }
    if (getSnapshotListener() != null) {
        snapshotControl.setSnapshotListener(getSnapshotListener());
    }
    ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
    try {
        referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, targetDatabase, snapshotControl);
    } finally {
        referenceDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) CatalogAndSchema(liquibase.CatalogAndSchema) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 8 with ObjectQuotingStrategy

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

Aggregations

ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)8 CatalogAndSchema (liquibase.CatalogAndSchema)4 ChangeSet (liquibase.changelog.ChangeSet)3 CompareControl (liquibase.diff.compare.CompareControl)2 DatabaseException (liquibase.exception.DatabaseException)2 InvalidExampleException (liquibase.snapshot.InvalidExampleException)2 SQLException (java.sql.SQLException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Change (liquibase.change.Change)1 ExecType (liquibase.changelog.ChangeSet.ExecType)1 RunStatus (liquibase.changelog.ChangeSet.RunStatus)1 RanChangeSet (liquibase.changelog.RanChangeSet)1 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)1 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)1 Database (liquibase.database.Database)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 ObjectDifferences (liquibase.diff.ObjectDifferences)1 LiquibaseException (liquibase.exception.LiquibaseException)1 LockException (liquibase.exception.LockException)1 MigrationFailedException (liquibase.exception.MigrationFailedException)1