Search in sources :

Example 51 with Database

use of liquibase.database.Database 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 52 with Database

use of liquibase.database.Database 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 53 with Database

use of liquibase.database.Database 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)

Example 54 with Database

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

the class UniqueConstraintSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException {
    if (!snapshot.getSnapshotControl().shouldInclude(UniqueConstraint.class)) {
        return;
    }
    if (foundObject instanceof Table) {
        Table table = (Table) foundObject;
        Database database = snapshot.getDatabase();
        Schema schema;
        schema = table.getSchema();
        List<CachedRow> metadata;
        try {
            metadata = listConstraints(table, snapshot, schema);
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
        Set<String> seenConstraints = new HashSet<>();
        for (CachedRow constraint : metadata) {
            UniqueConstraint uq = new UniqueConstraint().setName(cleanNameFromDatabase((String) constraint.get("CONSTRAINT_NAME"), database)).setRelation(table);
            if (constraint.containsColumn("INDEX_NAME")) {
                uq.setBackingIndex(new Index((String) constraint.get("INDEX_NAME"), (String) constraint.get("INDEX_CATALOG"), (String) constraint.get("INDEX_SCHEMA"), table.getName()));
            }
            if ("CLUSTERED".equals(constraint.get("TYPE_DESC"))) {
                uq.setClustered(true);
            }
            if (seenConstraints.add(uq.getName())) {
                table.getUniqueConstraints().add(uq);
            }
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) Table(liquibase.structure.core.Table) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) Database(liquibase.database.Database) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) DatabaseException(liquibase.exception.DatabaseException) HashSet(java.util.HashSet)

Example 55 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 {
    if (!snapshot.getSnapshotControl().shouldInclude(Column.class)) {
        return;
    }
    if (foundObject instanceof Relation) {
        Database database = snapshot.getDatabase();
        Relation relation = (Relation) foundObject;
        List<CachedRow> allColumnsMetadataRs;
        try {
            JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
            Schema schema;
            schema = relation.getSchema();
            allColumnsMetadataRs = databaseMetaData.getColumns(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), relation.getName(), null);
            List<CachedRow> metaDataNotNullConst = databaseMetaData.getNotNullConst(schema.getCatalogName(), schema.getName(), relation.getName());
            /*
                 * Microsoft SQL Server, SAP SQL Anywhere and probably other RDBMS guarantee non-duplicate
                 * ORDINAL_POSITIONs for the columns of a single table. But they do not guarantee there are no gaps
                 * in that integers (e.g. if columns have been deleted). So we need to check for that and renumber
                 * if needed.
                 */
            TreeMap<Integer, CachedRow> treeSet = new TreeMap<>();
            for (CachedRow row : allColumnsMetadataRs) {
                treeSet.put(row.getInt("ORDINAL_POSITION"), row);
            }
            Logger log = Scope.getCurrentScope().getLog(getClass());
            // Now we can iterate through the sorted list and repair if needed.
            int currentOrdinal = 0;
            for (CachedRow row : treeSet.values()) {
                currentOrdinal++;
                int rsOrdinal = row.getInt("ORDINAL_POSITION");
                if (rsOrdinal != currentOrdinal) {
                    log.fine(String.format("Repairing ORDINAL_POSITION with gaps for table=%s, column name=%s, " + "bad ordinal=%d, new ordinal=%d", relation.getName(), row.getString("COLUMN_NAME"), rsOrdinal, currentOrdinal));
                    row.set("ORDINAL_POSITION", currentOrdinal);
                }
            }
            // Iterate through all (repaired) rows and add the columns to our result.
            for (CachedRow row : allColumnsMetadataRs) {
                Column column = readColumn(row, relation, database);
                setAutoIncrementDetails(column, database, snapshot);
                populateValidateNullableIfNeeded(column, metaDataNotNullConst, database);
                column.setAttribute(LIQUIBASE_COMPLETE, !column.isNullable());
                relation.getColumns().add(column);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) TreeMap(java.util.TreeMap) Logger(liquibase.logging.Logger) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) Database(liquibase.database.Database) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

Database (liquibase.database.Database)220 Test (org.junit.Test)99 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)60 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)57 Sql (liquibase.sql.Sql)57 OracleDatabase (liquibase.database.core.OracleDatabase)55 MySQLDatabase (liquibase.database.core.MySQLDatabase)53 PostgresDatabase (liquibase.database.core.PostgresDatabase)51 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)48 H2Database (liquibase.database.core.H2Database)47 DerbyDatabase (liquibase.database.core.DerbyDatabase)45 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)44 HsqlDatabase (liquibase.database.core.HsqlDatabase)44 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)44 SybaseDatabase (liquibase.database.core.SybaseDatabase)44 CreateTableStatement (liquibase.statement.core.CreateTableStatement)44 DatabaseException (liquibase.exception.DatabaseException)28 SQLException (java.sql.SQLException)25 AbstractJdbcDatabaseTest (liquibase.database.AbstractJdbcDatabaseTest)23 Liquibase (liquibase.Liquibase)22