Search in sources :

Example 11 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class DatabaseObjectComparator method compare.

@Override
public int compare(DatabaseObject o1, DatabaseObject o2) {
    Schema schema1 = o1.getSchema();
    Schema schema2 = o2.getSchema();
    if (schema1 != null && schema2 != null) {
        int i = schema1.toString().compareTo(schema2.toString());
        if (i != 0) {
            return i;
        }
    }
    String name1 = StringUtils.trimToEmpty(o1.getName());
    String name2 = StringUtils.trimToEmpty(o2.getName());
    int i = name1.compareTo(name2);
    if (i == 0) {
        return StringUtils.trimToEmpty(o1.toString()).compareTo(StringUtils.trimToEmpty(o2.toString()));
    }
    return i;
}
Also used : Schema(liquibase.structure.core.Schema)

Example 12 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class SnapshotGeneratorFactory method createSnapshot.

public DatabaseSnapshot createSnapshot(CatalogAndSchema[] examples, Database database, SnapshotControl snapshotControl) throws DatabaseException, InvalidExampleException {
    if (database == null) {
        return null;
    }
    Schema[] schemas = new Schema[examples.length];
    for (int i = 0; i < schemas.length; i++) {
        examples[i] = examples[i].customize(database);
        schemas[i] = new Schema(examples[i].getCatalogName(), examples[i].getSchemaName());
    }
    return createSnapshot(schemas, database, snapshotControl);
}
Also used : Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema)

Example 13 with Schema

use of liquibase.structure.core.Schema 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 = getMetaData(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).getMetaData().getViews(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), example.getName());
        if (viewsMetadataRs.size() > 0) {
            CachedRow row = viewsMetadataRs.get(0);
            String rawViewName = row.getString("TABLE_NAME");
            String rawSchemaName = StringUtils.trimToNull(row.getString("TABLE_SCHEM"));
            String rawCatalogName = StringUtils.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 (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() + "\"\\.", "");
                }
                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 : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) 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) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Example 14 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class ViewSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(View.class)) {
        return;
    }
    if (foundObject instanceof Schema) {
        Schema schema = (Schema) foundObject;
        Database database = snapshot.getDatabase();
        List<CachedRow> viewsMetadataRs = null;
        try {
            viewsMetadataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaData().getViews(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
            for (CachedRow row : viewsMetadataRs) {
                View view = new View();
                view.setName(row.getString("TABLE_NAME"));
                view.setSchema(schema);
                view.setRemarks(row.getString("REMARKS"));
                view.setDefinition(row.getString("OBJECT_BODY"));
                if (database instanceof OracleDatabase) {
                    view.setAttribute("editioning", "Y".equals(row.getString("EDITIONING_VIEW")));
                }
                schema.addDatabaseObject(view);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) OracleDatabase(liquibase.database.core.OracleDatabase) InformixDatabase(liquibase.database.core.InformixDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) View(liquibase.structure.core.View) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 15 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class DiffToReport method printSetComparison.

protected void printSetComparison(String title, Set<? extends DatabaseObject> objects, PrintStream out) {
    out.print(title + ": ");
    Schema lastSchema = null;
    if (objects.size() == 0) {
        out.println("NONE");
    } else {
        out.println();
        for (DatabaseObject object : objects) {
            if (getIncludeSchema() && object.getSchema() != null && (lastSchema == null || !lastSchema.equals(object.getSchema()))) {
                lastSchema = object.getSchema();
                String schemaName = object.getSchema().getName();
                if (schemaName == null) {
                    schemaName = object.getSchema().getCatalogName();
                }
                schemaName = includeSchemaComparison(schemaName);
                out.println("  SCHEMA: " + schemaName);
            }
            out.println("     " + object);
        }
    }
}
Also used : Schema(liquibase.structure.core.Schema) DatabaseObject(liquibase.structure.DatabaseObject)

Aggregations

Schema (liquibase.structure.core.Schema)23 CatalogAndSchema (liquibase.CatalogAndSchema)9 Table (liquibase.structure.core.Table)6 Database (liquibase.database.Database)5 OracleDatabase (liquibase.database.core.OracleDatabase)4 SQLException (java.sql.SQLException)3 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)3 DatabaseException (liquibase.exception.DatabaseException)3 RawSqlStatement (liquibase.statement.core.RawSqlStatement)3 Column (liquibase.structure.core.Column)3 View (liquibase.structure.core.View)3 InformixDatabase (liquibase.database.core.InformixDatabase)2 CachedRow (liquibase.snapshot.CachedRow)2 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)2 UnparsedSql (liquibase.sql.UnparsedSql)2 Catalog (liquibase.structure.core.Catalog)2 Index (liquibase.structure.core.Index)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1