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;
}
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;
}
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);
}
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;
}
Aggregations