Search in sources :

Example 11 with JdbcDatabaseSnapshot

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

the class ViewSnapshotGenerator method snapshotObject.

// public Boolean has(DatabaseObject example, DatabaseSnapshot snapshot, SnapshotGeneratorChain chain) throws DatabaseException {
// Database database = snapshot.getDatabase();
// if (!(example instanceof View)) {
// return chain.has(example, snapshot);
// }
// String viewName = example.getName();
// Schema schema = example.getSchema();
// try {
// ResultSet rs = getMetaDataFromCache(database).getTables(database.getJdbcCatalogName(schema), database.getJdbcSchemaName(schema), database.correctObjectName(viewName, View.class), new String[]{"VIEW"});
// try {
// return rs.next();
// } finally {
// try {
// rs.close();
// } catch (SQLException ignore) {
// }
// }
// } catch (SQLException e) {
// throw new DatabaseException(e);
// }
// }
@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
    if (((View) example).getDefinition() != null) {
        return example;
    }
    Database database = snapshot.getDatabase();
    Schema schema = example.getSchema();
    List<CachedRow> viewsMetadataRs = null;
    try {
        viewsMetadataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache().getViews(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), example.getName());
        if (!viewsMetadataRs.isEmpty()) {
            CachedRow row = viewsMetadataRs.get(0);
            String rawViewName = row.getString("TABLE_NAME");
            String rawSchemaName = StringUtil.trimToNull(row.getString("TABLE_SCHEM"));
            String rawCatalogName = StringUtil.trimToNull(row.getString("TABLE_CAT"));
            String remarks = row.getString("REMARKS");
            if (remarks != null) {
                // come back escaped sometimes
                remarks = remarks.replace("''", "'");
            }
            View view = new View().setName(cleanNameFromDatabase(rawViewName, database));
            view.setRemarks(remarks);
            CatalogAndSchema schemaFromJdbcInfo = ((AbstractJdbcDatabase) database).getSchemaFromJdbcInfo(rawCatalogName, rawSchemaName);
            view.setSchema(new Schema(schemaFromJdbcInfo.getCatalogName(), schemaFromJdbcInfo.getSchemaName()));
            try {
                String definition = database.getViewDefinition(schemaFromJdbcInfo, view.getName());
                if (definition.startsWith("FULL_DEFINITION: ")) {
                    definition = definition.replaceFirst("^FULL_DEFINITION: ", "");
                    view.setContainsFullDefinition(true);
                }
                // remove strange zero-termination seen on some Oracle view definitions
                int length = definition.length();
                if (length > 0 && definition.charAt(length - 1) == 0) {
                    definition = definition.substring(0, length - 1);
                }
                if (database instanceof InformixDatabase) {
                    // Cleanup
                    definition = definition.trim();
                    definition = definition.replaceAll("\\s*,\\s*", ", ");
                    definition = definition.replaceAll("\\s*;", "");
                    // Strip the schema definition because it can optionally be included in the tag attribute
                    definition = definition.replaceAll("(?i)\"" + view.getSchema().getName() + "\"\\.", "");
                }
                definition = StringUtil.trimToNull(definition);
                if (definition == null) {
                    definition = "[CANNOT READ VIEW DEFINITION]";
                    String warningMessage = null;
                    if (database instanceof MariaDBDatabase) {
                        warningMessage = "\nThe current MariaDB user does not have permissions to access view definitions needed for this Liquibase command.\n" + "Please search the changelog for '[CANNOT READ VIEW DEFINITION]' to locate inaccessible objects. " + "Learn more about altering permissions with suggested MariaDB GRANTs at https://docs.liquibase.com/workflows/liquibase-pro/mariadbgrants.html\n";
                    } else if (database instanceof MySQLDatabase) {
                        warningMessage = "\nThe current MySQL user does not have permissions to access view definitions needed for this Liquibase command.\n" + "Please search the changelog for '[CANNOT READ VIEW DEFINITION]' to locate inaccessible objects. This is\n" + "potentially due to a known MySQL bug https://bugs.mysql.com/bug.php?id=22763. Learn more about altering\n" + "permissions with suggested MySQL GRANTs at https://docs.liquibase.com/workflows/liquibase-pro/mysqlgrants.html\n";
                    }
                    if (warningMessage != null) {
                        Scope.getCurrentScope().getUI().sendMessage("WARNING: " + warningMessage);
                        Scope.getCurrentScope().getLog(getClass()).warning(warningMessage);
                    }
                }
                view.setDefinition(definition);
            } catch (DatabaseException e) {
                throw new DatabaseException("Error getting " + database.getConnection().getURL() + " view with " + new GetViewDefinitionStatement(view.getSchema().getCatalogName(), view.getSchema().getName(), rawViewName), e);
            }
            return view;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : MariaDBDatabase(liquibase.database.core.MariaDBDatabase) CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) MySQLDatabase(liquibase.database.core.MySQLDatabase) CatalogAndSchema(liquibase.CatalogAndSchema) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) View(liquibase.structure.core.View) GetViewDefinitionStatement(liquibase.statement.core.GetViewDefinitionStatement) InformixDatabase(liquibase.database.core.InformixDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) InformixDatabase(liquibase.database.core.InformixDatabase) Database(liquibase.database.Database) MariaDBDatabase(liquibase.database.core.MariaDBDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Example 12 with JdbcDatabaseSnapshot

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

the class ColumnSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
    if (BooleanUtil.isTrue(((Column) example).getComputed()) || BooleanUtil.isTrue(((Column) example).getDescending())) {
        return example;
    }
    Database database = snapshot.getDatabase();
    Relation relation = ((Column) example).getRelation();
    Schema schema = relation.getSchema();
    try {
        Column column = null;
        if (example.getAttribute(LIQUIBASE_COMPLETE, false)) {
            column = (Column) example;
            example.setAttribute(LIQUIBASE_COMPLETE, null);
            return column;
        }
        String catalogName = ((AbstractJdbcDatabase) database).getJdbcCatalogName(schema);
        String schemaName = ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema);
        String tableName = relation.getName();
        String columnName = example.getName();
        JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
        List<CachedRow> metaDataColumns = databaseMetaData.getColumns(catalogName, schemaName, tableName, columnName);
        List<CachedRow> metaDataNotNullConst = databaseMetaData.getNotNullConst(catalogName, schemaName, tableName);
        if (!metaDataColumns.isEmpty()) {
            CachedRow data = metaDataColumns.get(0);
            column = readColumn(data, relation, database);
            setAutoIncrementDetails(column, database, snapshot);
            populateValidateNullableIfNeeded(column, metaDataNotNullConst, database);
        }
        example.setAttribute(LIQUIBASE_COMPLETE, null);
        if (column == null && database instanceof PostgresDatabase && looksLikeFunction(example.getName())) {
            ((Column) example).setComputed(true);
            return example;
        }
        return column;
    } catch (DatabaseException | SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) Database(liquibase.database.Database) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) 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