Search in sources :

Example 1 with InvalidExampleException

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);
    }
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseObject(liquibase.structure.DatabaseObject) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with InvalidExampleException

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);
        }
    }
}
Also used : Table(liquibase.structure.core.Table) Data(liquibase.structure.core.Data) DatabaseException(liquibase.exception.DatabaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with InvalidExampleException

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);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Example 4 with InvalidExampleException

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;
}
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 5 with InvalidExampleException

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;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

InvalidExampleException (liquibase.snapshot.InvalidExampleException)16 DatabaseException (liquibase.exception.DatabaseException)9 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)8 CatalogAndSchema (liquibase.CatalogAndSchema)7 Database (liquibase.database.Database)7 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)5 SnapshotControl (liquibase.snapshot.SnapshotControl)5 Table (liquibase.structure.core.Table)5 SQLException (java.sql.SQLException)4 CachedRow (liquibase.snapshot.CachedRow)4 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)4 DatabaseObject (liquibase.structure.DatabaseObject)4 DB2Database (liquibase.database.core.DB2Database)3 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)3 DiffResult (liquibase.diff.DiffResult)3 SqlStatement (liquibase.statement.SqlStatement)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Change (liquibase.change.Change)2 Db2zDatabase (liquibase.database.core.Db2zDatabase)2