Search in sources :

Example 16 with Database

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

the class DiffToChangeLog method getOrderedOutputTypes.

protected List<Class<? extends DatabaseObject>> getOrderedOutputTypes(Class<? extends ChangeGenerator> generatorType) {
    Database comparisonDatabase = diffResult.getComparisonSnapshot().getDatabase();
    DependencyGraph graph = new DependencyGraph();
    for (Class<? extends DatabaseObject> type : diffResult.getReferenceSnapshot().getSnapshotControl().getTypesToInclude()) {
        graph.addType(type);
    }
    List<Class<? extends DatabaseObject>> types = graph.sort(comparisonDatabase, generatorType);
    if (!loggedOrderFor.contains(generatorType)) {
        String log = generatorType.getSimpleName() + " type order: ";
        for (Class<? extends DatabaseObject> type : types) {
            log += "    " + type.getName();
        }
        LogFactory.getLogger().debug(log);
        loggedOrderFor.add(generatorType);
    }
    return types;
}
Also used : DB2Database(liquibase.database.core.DB2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) Database(liquibase.database.Database) DatabaseObject(liquibase.structure.DatabaseObject)

Example 17 with Database

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

the class DiffGeneratorFactory method compare.

public DiffResult compare(DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, CompareControl compareControl) throws DatabaseException {
    Database referenceDatabase = referenceSnapshot.getDatabase();
    Database comparisonDatabase;
    if (comparisonSnapshot == null) {
        comparisonDatabase = referenceSnapshot.getDatabase();
        try {
            comparisonSnapshot = new EmptyDatabaseSnapshot(referenceDatabase, referenceSnapshot.getSnapshotControl());
        } catch (InvalidExampleException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    } else {
        comparisonDatabase = comparisonSnapshot.getDatabase();
    }
    return getGenerator(referenceDatabase, comparisonDatabase).compare(referenceSnapshot, comparisonSnapshot, compareControl);
}
Also used : Database(liquibase.database.Database) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 18 with Database

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

the class DiffResult method getChangedObject.

public ObjectDifferences getChangedObject(DatabaseObject example, CompareControl.SchemaComparison[] schemaComparisons) {
    Database accordingTo = this.getComparisonSnapshot().getDatabase();
    DatabaseObjectComparatorFactory comparator = DatabaseObjectComparatorFactory.getInstance();
    for (Map.Entry<? extends DatabaseObject, ObjectDifferences> entry : getChangedObjects(example.getClass()).entrySet()) {
        if (comparator.isSameObject(entry.getKey(), example, schemaComparisons, accordingTo)) {
            return entry.getValue();
        }
    }
    return null;
}
Also used : Database(liquibase.database.Database) DatabaseObjectComparatorFactory(liquibase.diff.compare.DatabaseObjectComparatorFactory)

Example 19 with Database

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

the class ColumnSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(Column.class)) {
        return;
    }
    if (foundObject instanceof Relation) {
        Database database = snapshot.getDatabase();
        Relation relation = (Relation) foundObject;
        List<CachedRow> allColumnsMetadataRs = null;
        try {
            JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaData();
            Schema schema;
            schema = relation.getSchema();
            allColumnsMetadataRs = databaseMetaData.getColumns(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), relation.getName(), null);
            for (CachedRow row : allColumnsMetadataRs) {
                Column column = readColumn(row, relation, database);
                setAutoIncrementDetails(column, database, snapshot);
                column.setAttribute(LIQUIBASE_COMPLETE, true);
                relation.getColumns().add(column);
            }
        } catch (Exception e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 20 with Database

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

the class IndexSnapshotGenerator method addTo.

//    public Boolean has(DatabaseObject example, DatabaseSnapshot snapshot, SnapshotGeneratorChain chain) throws DatabaseException {
//        Database database = snapshot.getDatabase();
//        if (!(example instanceof Index)) {
//            return chain.has(example, snapshot);
//        }
//        String tableName = ((Index) example).getTable().getName();
//        Schema schema = example.getSchema();
//
//        String indexName = example.getName();
//        String columnNames = ((Index) example).getColumnNames();
//
//        try {
//            if (tableName == null) {
//                Index newExample = new Index();
//                newExample.setName(indexName);
//                if (columnNames != null) {
//                    for (String column : columnNames.split("\\s*,\\s*")) {
//                        newExample.getColumns().add(column);
//                    }
//                }
//
//                ResultSet rs = getMetaData(database).getTables(database.getJdbcCatalogName(schema), database.getJdbcSchemaName(schema), null, new String[]{"TABLE"});
//                try {
//                    while (rs.next()) {
//                        String foundTable = rs.getString("TABLE_NAME");
//                        newExample.setTable((Table) new Table().setName(foundTable).setSchema(schema));
//                        if (has(newExample, snapshot, chain)) {
//                            return true;
//                        }
//                    }
//                    return false;
//                } finally {
//                    rs.close();
//                }
//            }
//
//            Index index = new Index();
//            index.setTable((Table) new Table().setName(tableName).setSchema(schema));
//            index.setName(indexName);
//            if (columnNames != null) {
//                for (String column : columnNames.split("\\s*,\\s*")) {
//                    index.getColumns().add(column);
//                }
//            }
//
//            if (columnNames != null) {
//                Map<String, TreeMap<Short, String>> columnsByIndexName = new HashMap<String, TreeMap<Short, String>>();
//                ResultSet rs = getMetaData(database).getIndexInfo(database.getJdbcCatalogName(schema), database.getJdbcSchemaName(schema), database.correctObjectName(tableName, Table.class), false, true);
//                try {
//                    while (rs.next()) {
//                        String foundIndexName = rs.getString("INDEX_NAME");
//                        if (indexName != null && indexName.equalsIgnoreCase(foundIndexName)) { //ok to use equalsIgnoreCase because we will check case later
//                            continue;
//                        }
//                        short ordinalPosition = rs.getShort("ORDINAL_POSITION");
//
//                        if (!columnsByIndexName.containsKey(foundIndexName)) {
//                            columnsByIndexName.put(foundIndexName, new TreeMap<Short, String>());
//                        }
//                        String columnName = rs.getString("COLUMN_NAME");
//                        Map<Short, String> columns = columnsByIndexName.get(foundIndexName);
//                        columns.put(ordinalPosition, columnName);
//                    }
//
//                    for (Map.Entry<String, TreeMap<Short, String>> foundIndexData : columnsByIndexName.entrySet()) {
//                        Index foundIndex = new Index()
//                                .setName(foundIndexData.getKey())
//                                .setTable(((Table) new Table().setName(tableName).setSchema(schema)));
//                        foundIndex.getColumns().addAll(foundIndexData.getValue().values());
//
//                        if (foundIndex.equals(index, database)) {
//                            return true;
//                        }
//                        return false;
//                    }
//                    return false;
//                } finally {
//                    rs.close();
//                }
//            } else if (indexName != null) {
//                ResultSet rs = getMetaData(database).getIndexInfo(database.getJdbcCatalogName(schema), database.getJdbcSchemaName(schema), database.correctObjectName(tableName, Table.class), false, true);
//                try {
//                    while (rs.next()) {
//                        Index foundIndex = new Index()
//                                .setName(rs.getString("INDEX_NAME"))
//                                .setTable(((Table) new Table().setName(tableName).setSchema(schema)));
//                        if (foundIndex.getName() == null) {
//                            continue;
//                        }
//                        if (foundIndex.equals(index, database)) {
//                            return true;
//                        }
//                    }
//                    return false;
//                } finally {
//                    try {
//                        rs.close();
//                    } catch (SQLException ignore) {
//                    }
//                }
//            } else {
//                throw new UnexpectedLiquibaseException("Either indexName or columnNames must be set");
//            }
//        } catch (SQLException e) {
//            throw new DatabaseException(e);
//        }
//
//    }
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(Index.class)) {
        return;
    }
    if (foundObject instanceof Table) {
        Table table = (Table) foundObject;
        Database database = snapshot.getDatabase();
        Schema schema;
        schema = table.getSchema();
        List<CachedRow> rs = null;
        JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = null;
        try {
            databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaData();
            rs = databaseMetaData.getIndexInfo(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), table.getName(), null);
            Map<String, Index> foundIndexes = new HashMap<String, Index>();
            for (CachedRow row : rs) {
                String indexName = row.getString("INDEX_NAME");
                if (indexName == null) {
                    continue;
                }
                if (database instanceof DB2Database && "SYSIBM".equals(row.getString("INDEX_QUALIFIER"))) {
                    continue;
                }
                Index index = foundIndexes.get(indexName);
                if (index == null) {
                    index = new Index();
                    index.setName(indexName);
                    index.setTable(table);
                    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 = 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.getTable()));
            }
            //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<Index>();
            for (Index exampleIndex : foundIndexes.values()) {
                if (exampleIndex.getClustered() != null && exampleIndex.getClustered()) {
                    table.getIndexes().add(exampleIndex);
                } else {
                    stillToAdd.add(exampleIndex);
                }
            }
            for (Index exampleIndex : stillToAdd) {
                boolean alreadyAddedSimilar = false;
                for (Index index : table.getIndexes()) {
                    if (DatabaseObjectComparatorFactory.getInstance().isSameObject(index, exampleIndex, null, database)) {
                        alreadyAddedSimilar = true;
                    }
                }
                if (!alreadyAddedSimilar) {
                    table.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().setTable(((UniqueConstraint) foundObject).getTable());
        exampleIndex.getColumns().addAll(((UniqueConstraint) foundObject).getColumns());
        ((UniqueConstraint) foundObject).setBackingIndex(exampleIndex);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

Database (liquibase.database.Database)146 Test (org.junit.Test)74 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)50 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)43 Sql (liquibase.sql.Sql)42 DB2Database (liquibase.database.core.DB2Database)41 OracleDatabase (liquibase.database.core.OracleDatabase)39 PostgresDatabase (liquibase.database.core.PostgresDatabase)37 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DerbyDatabase (liquibase.database.core.DerbyDatabase)33 H2Database (liquibase.database.core.H2Database)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)33 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 CreateTableStatement (liquibase.statement.core.CreateTableStatement)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)30 DatabaseException (liquibase.exception.DatabaseException)25 AbstractJdbcDatabaseTest (liquibase.database.AbstractJdbcDatabaseTest)21 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)17