Search in sources :

Example 1 with JdbcDatabaseSnapshot

use of liquibase.snapshot.JdbcDatabaseSnapshot 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 2 with JdbcDatabaseSnapshot

use of liquibase.snapshot.JdbcDatabaseSnapshot in project liquibase by liquibase.

the class PrimaryKeySnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    Database database = snapshot.getDatabase();
    Schema schema = example.getSchema();
    String searchTableName = null;
    if (((PrimaryKey) example).getTable() != null) {
        searchTableName = ((PrimaryKey) example).getTable().getName();
        searchTableName = database.correctObjectName(searchTableName, Table.class);
    }
    List<CachedRow> rs = null;
    try {
        JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
        rs = metaData.getPrimaryKeys(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), searchTableName);
        PrimaryKey returnKey = null;
        for (CachedRow row : rs) {
            if ((example.getName() != null) && !example.getName().equalsIgnoreCase(row.getString("PK_NAME"))) {
                continue;
            }
            String columnName = cleanNameFromDatabase(row.getString("COLUMN_NAME"), database);
            short position = row.getShort("KEY_SEQ");
            if (returnKey == null) {
                returnKey = new PrimaryKey();
                CatalogAndSchema tableSchema = ((AbstractJdbcDatabase) database).getSchemaFromJdbcInfo(row.getString("TABLE_CAT"), row.getString("TABLE_SCHEM"));
                returnKey.setTable((Table) new Table().setName(row.getString("TABLE_NAME")).setSchema(new Schema(tableSchema.getCatalogName(), tableSchema.getSchemaName())));
                returnKey.setName(row.getString("PK_NAME"));
            }
            String ascOrDesc = row.getString("ASC_OR_DESC");
            Boolean descending = "D".equals(ascOrDesc) ? Boolean.TRUE : "A".equals(ascOrDesc) ? Boolean.FALSE : null;
            boolean computed = false;
            if (descending != null && descending) {
                computed = true;
            }
            returnKey.addColumn(position - 1, new Column(columnName).setDescending(descending).setComputed(computed).setRelation(((PrimaryKey) example).getTable()));
            setValidateOptionIfAvailable(database, returnKey, row);
        }
        if (returnKey != null) {
            Index exampleIndex = new Index().setRelation(returnKey.getTable());
            exampleIndex.setColumns(returnKey.getColumns());
            returnKey.setBackingIndex(exampleIndex);
        }
        return returnKey;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) CatalogAndSchema(liquibase.CatalogAndSchema) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with JdbcDatabaseSnapshot

use of liquibase.snapshot.JdbcDatabaseSnapshot in project liquibase by liquibase.

the class PrimaryKeySnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException {
    if (!snapshot.getSnapshotControl().shouldInclude(PrimaryKey.class)) {
        return;
    }
    if (foundObject instanceof Table) {
        Table table = (Table) foundObject;
        Database database = snapshot.getDatabase();
        Schema schema = table.getSchema();
        List<CachedRow> rs = null;
        try {
            JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
            rs = metaData.getPrimaryKeys(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), table.getName());
            if (!rs.isEmpty()) {
                PrimaryKey primaryKey = new PrimaryKey().setName(rs.get(0).getString("PK_NAME"));
                primaryKey.setTable((Table) foundObject);
                if (!database.isSystemObject(primaryKey)) {
                    table.setPrimaryKey(primaryKey.setTable(table));
                }
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 4 with JdbcDatabaseSnapshot

use of liquibase.snapshot.JdbcDatabaseSnapshot in project liquibase by liquibase.

the class TableSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
    Database database = snapshot.getDatabase();
    String objectName = example.getName();
    Schema schema = example.getSchema();
    List<CachedRow> rs = null;
    try {
        JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
        rs = metaData.getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), objectName);
        Table table;
        if (!rs.isEmpty()) {
            table = readTable(rs.get(0), database);
        } else {
            return null;
        }
        return table;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) Table(liquibase.structure.core.Table) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 5 with JdbcDatabaseSnapshot

use of liquibase.snapshot.JdbcDatabaseSnapshot in project liquibase by liquibase.

the class TableSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
        return;
    }
    if (foundObject instanceof Schema) {
        Database database = snapshot.getDatabase();
        Schema schema = (Schema) foundObject;
        List<CachedRow> tableMetaDataRs = null;
        try {
            tableMetaDataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache().getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
            for (CachedRow row : tableMetaDataRs) {
                String tableName = row.getString("TABLE_NAME");
                Table tableExample = (Table) new Table().setName(cleanNameFromDatabase(tableName, database)).setSchema(schema);
                schema.addDatabaseObject(tableExample);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) Table(liquibase.structure.core.Table) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)12 Database (liquibase.database.Database)12 DatabaseException (liquibase.exception.DatabaseException)12 CachedRow (liquibase.snapshot.CachedRow)12 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)12 SQLException (java.sql.SQLException)8 CatalogAndSchema (liquibase.CatalogAndSchema)8 OracleDatabase (liquibase.database.core.OracleDatabase)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 MariaDBDatabase (liquibase.database.core.MariaDBDatabase)4 MySQLDatabase (liquibase.database.core.MySQLDatabase)4 InvalidExampleException (liquibase.snapshot.InvalidExampleException)4 Schema (liquibase.structure.core.Schema)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DB2Database (liquibase.database.core.DB2Database)2 Db2zDatabase (liquibase.database.core.Db2zDatabase)2 InformixDatabase (liquibase.database.core.InformixDatabase)2 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)2 Table (liquibase.structure.core.Table)2