use of liquibase.snapshot.InvalidExampleException in project liquibase by liquibase.
the class Liquibase method generateChangeLog.
public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, ChangeLogSerializer changeLogSerializer, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, IOException, ParserConfigurationException {
Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
if (snapshotTypes != null && snapshotTypes.length > 0) {
finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
}
SnapshotControl snapshotControl = new SnapshotControl(this.getDatabase(), snapshotTypes);
CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, finalCompareTypes);
// compareControl.addStatusListener(new OutDiffStatusListener());
DatabaseSnapshot originalDatabaseSnapshot = null;
try {
originalDatabaseSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), getDatabase(), snapshotControl);
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(originalDatabaseSnapshot, SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), null, snapshotControl), compareControl);
changeLogWriter.setDiffResult(diffResult);
if (changeLogSerializer != null) {
changeLogWriter.print(outputStream, changeLogSerializer);
} else {
changeLogWriter.print(outputStream);
}
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.snapshot.InvalidExampleException in project liquibase by liquibase.
the class DataSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Data.class)) {
return;
}
if (foundObject instanceof Table) {
Table table = (Table) foundObject;
try {
Data exampleData = new Data().setTable(table);
table.setAttribute("data", exampleData);
} catch (Exception e) {
throw new DatabaseException(e);
}
}
}
use of liquibase.snapshot.InvalidExampleException in project liquibase by liquibase.
the class IndexSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Index.class)) {
return;
}
if (foundObject instanceof Table || foundObject instanceof View) {
if (foundObject instanceof View && !addToViews(snapshot.getDatabase())) {
return;
}
Relation relation = (Relation) foundObject;
Database database = snapshot.getDatabase();
Schema schema;
schema = relation.getSchema();
List<CachedRow> rs = null;
JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = null;
try {
databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
rs = databaseMetaData.getIndexInfo(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), relation.getName(), null);
Map<String, Index> foundIndexes = new HashMap<>();
for (CachedRow row : rs) {
String indexName = row.getString("INDEX_NAME");
if (indexName == null) {
continue;
}
if ((database instanceof AbstractDb2Database) && "SYSIBM".equals(row.getString("INDEX_QUALIFIER"))) {
continue;
}
Index index = foundIndexes.get(indexName);
if (index == null) {
index = new Index();
index.setName(indexName);
index.setRelation(relation);
short type = row.getShort("TYPE");
if (type == DatabaseMetaData.tableIndexClustered) {
index.setClustered(true);
} else if (database instanceof MSSQLDatabase) {
index.setClustered(false);
}
foundIndexes.put(indexName, index);
}
String ascOrDesc;
if (database instanceof Db2zDatabase) {
ascOrDesc = row.getString("ORDER");
} else {
ascOrDesc = row.getString("ASC_OR_DESC");
}
Boolean descending = "D".equals(ascOrDesc) ? Boolean.TRUE : ("A".equals(ascOrDesc) ? Boolean.FALSE : null);
index.addColumn(new Column(row.getString("COLUMN_NAME")).setComputed(false).setDescending(descending).setRelation(index.getRelation()));
}
// add clustered indexes first, than all others in case there is a clustered and non-clustered version of the same index. Prefer the clustered version
List<Index> stillToAdd = new ArrayList<>();
for (Index exampleIndex : foundIndexes.values()) {
if ((exampleIndex.getClustered() != null) && exampleIndex.getClustered()) {
relation.getIndexes().add(exampleIndex);
} else {
stillToAdd.add(exampleIndex);
}
}
for (Index exampleIndex : stillToAdd) {
boolean alreadyAddedSimilar = false;
for (Index index : relation.getIndexes()) {
if (DatabaseObjectComparatorFactory.getInstance().isSameObject(index, exampleIndex, null, database)) {
alreadyAddedSimilar = true;
}
}
if (!alreadyAddedSimilar) {
relation.getIndexes().add(exampleIndex);
}
}
} catch (Exception e) {
throw new DatabaseException(e);
}
}
if ((foundObject instanceof UniqueConstraint) && (((UniqueConstraint) foundObject).getBackingIndex() == null) && !(snapshot.getDatabase() instanceof DB2Database) && !(snapshot.getDatabase() instanceof DerbyDatabase)) {
Index exampleIndex = new Index().setRelation(((UniqueConstraint) foundObject).getRelation());
exampleIndex.getColumns().addAll(((UniqueConstraint) foundObject).getColumns());
((UniqueConstraint) foundObject).setBackingIndex(exampleIndex);
}
if ((foundObject instanceof ForeignKey) && (((ForeignKey) foundObject).getBackingIndex() == null)) {
Index exampleIndex = new Index().setRelation(((ForeignKey) foundObject).getForeignKeyTable());
exampleIndex.getColumns().addAll(((ForeignKey) foundObject).getForeignKeyColumns());
((ForeignKey) foundObject).setBackingIndex(exampleIndex);
}
}
use of liquibase.snapshot.InvalidExampleException 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 {
// name as equal to the catalog name.
if (((Schema) example).getCatalog().isDefault()) {
match = new Schema(((Schema) example).getCatalog(), catalogName);
} else {
/* Before we confirm the schema/catalog existence, we must first check if the catalog exists. */
Catalog catalog = ((Schema) example).getCatalog();
String[] dbCatalogNames = getDatabaseCatalogNames(database);
for (String candidateCatalogName : dbCatalogNames) {
if (catalog.equals(new Catalog(candidateCatalogName))) {
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.snapshot.InvalidExampleException in project liquibase by liquibase.
the class StandardDiffGenerator method compare.
@Override
public DiffResult compare(DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, CompareControl compareControl) throws DatabaseException {
if (comparisonSnapshot == null) {
try {
// , compareControl.toSnapshotControl(CompareControl.DatabaseRole.REFERENCE));
comparisonSnapshot = new EmptyDatabaseSnapshot(referenceSnapshot.getDatabase());
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
DiffResult diffResult = new DiffResult(referenceSnapshot, comparisonSnapshot, compareControl);
checkVersionInfo(referenceSnapshot, comparisonSnapshot, diffResult);
Set<Class<? extends DatabaseObject>> typesToCompare = compareControl.getComparedTypes();
typesToCompare.retainAll(referenceSnapshot.getSnapshotControl().getTypesToInclude());
typesToCompare.retainAll(comparisonSnapshot.getSnapshotControl().getTypesToInclude());
for (Class<? extends DatabaseObject> typeToCompare : typesToCompare) {
compareObjectType(typeToCompare, referenceSnapshot, comparisonSnapshot, diffResult);
}
return diffResult;
}
Aggregations