Search in sources :

Example 6 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class Liquibase method outputHeader.

private void outputHeader(String message) throws DatabaseException {
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    executor.comment("*********************************************************************");
    executor.comment(message);
    executor.comment("*********************************************************************");
    executor.comment("Change Log: " + changeLogFile);
    executor.comment("Ran at: " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()));
    DatabaseConnection connection = getDatabase().getConnection();
    if (connection != null) {
        executor.comment("Against: " + connection.getConnectionUserName() + "@" + connection.getURL());
    }
    executor.comment("Liquibase version: " + LiquibaseUtil.getBuildVersion());
    executor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    if (database instanceof OracleDatabase) {
        executor.execute(new RawSqlStatement("SET DEFINE OFF;"));
    }
    if (database instanceof MSSQLDatabase && database.getDefaultCatalogName() != null) {
        executor.execute(new RawSqlStatement("USE " + database.escapeObjectName(database.getDefaultCatalogName(), Catalog.class) + ";"));
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) DatabaseConnection(liquibase.database.DatabaseConnection) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Date(java.util.Date)

Example 7 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class DiffToChangeLog method addDependencies.

/**
     * Adds dependencies to the graph as schema.object_name.
     */
protected void addDependencies(DependencyUtil.DependencyGraph<String> graph, List<String> schemas, Collection<DatabaseObject> missingObjects, Database database) throws DatabaseException {
    if (database instanceof DB2Database) {
        Executor executor = ExecutorService.getInstance().getExecutor(database);
        List<Map<String, ?>> rs = executor.queryForList(new RawSqlStatement("select TABSCHEMA, TABNAME, BSCHEMA, BNAME from syscat.tabdep where (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "TABSCHEMA='" + obj + "'";
            }
        }) + ")"));
        for (Map<String, ?> row : rs) {
            String tabName = StringUtils.trimToNull((String) row.get("TABSCHEMA")) + "." + StringUtils.trimToNull((String) row.get("TABNAME"));
            String bName = StringUtils.trimToNull((String) row.get("BSCHEMA")) + "." + StringUtils.trimToNull((String) row.get("BNAME"));
            graph.add(bName, tabName);
        }
    } else if (database instanceof OracleDatabase) {
        Executor executor = ExecutorService.getInstance().getExecutor(database);
        List<Map<String, ?>> rs = executor.queryForList(new RawSqlStatement("select OWNER, NAME, REFERENCED_OWNER, REFERENCED_NAME from DBA_DEPENDENCIES where REFERENCED_OWNER != 'SYS' AND NOT(NAME LIKE 'BIN$%') AND NOT(OWNER = REFERENCED_OWNER AND NAME = REFERENCED_NAME) AND (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "OWNER='" + obj + "'";
            }
        }) + ")"));
        for (Map<String, ?> row : rs) {
            String tabName = StringUtils.trimToNull((String) row.get("OWNER")) + "." + StringUtils.trimToNull((String) row.get("NAME"));
            String bName = StringUtils.trimToNull((String) row.get("REFERENCED_OWNER")) + "." + StringUtils.trimToNull((String) row.get("REFERENCED_NAME"));
            graph.add(bName, tabName);
        }
    } else if (database instanceof MSSQLDatabase && database.getDatabaseMajorVersion() >= 9) {
        Executor executor = ExecutorService.getInstance().getExecutor(database);
        String sql = "select object_schema_name(referencing_id) as referencing_schema_name, object_name(referencing_id) as referencing_name, object_name(referenced_id) as referenced_name, object_schema_name(referenced_id) as referenced_schema_name  from sys.sql_expression_dependencies depz where (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "object_schema_name(referenced_id)='" + obj + "'";
            }
        }) + ")";
        sql += " UNION select object_schema_name(object_id) as referencing_schema_name, object_name(object_id) as referencing_name, object_name(parent_object_id) as referenced_name, object_schema_name(parent_object_id) as referenced_schema_name " + "from sys.objects " + "where parent_object_id > 0 " + "and is_ms_shipped=0 " + "and (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "object_schema_name(object_id)='" + obj + "'";
            }
        }) + ")";
        sql += " UNION select object_schema_name(fk.object_id) as referencing_schema_name, fk.name as referencing_name, i.name as referenced_name, object_schema_name(i.object_id) as referenced_schema_name " + "from sys.foreign_keys fk " + "join sys.indexes i on fk.referenced_object_id=i.object_id and fk.key_index_id=i.index_id " + "where fk.is_ms_shipped=0 " + "and (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "object_schema_name(fk.object_id)='" + obj + "'";
            }
        }) + ")";
        sql += " UNION select object_schema_name(i.object_id) as referencing_schema_name, object_name(i.object_id) as referencing_name, s.name as referenced_name, null as referenced_schema_name " + "from sys.indexes i " + "join sys.partition_schemes s on i.data_space_id = s.data_space_id";
        sql += " UNION select null as referencing_schema_name, s.name as referencing_name, f.name as referenced_name, null as referenced_schema_name from sys.partition_functions f " + "join sys.partition_schemes s on s.function_id=f.function_id";
        sql += " UNION select null as referencing_schema_name, s.name as referencing_name, fg.name as referenced_name, null as referenced_schema_name from sys.partition_schemes s " + "join sys.destination_data_spaces ds on s.data_space_id=ds.partition_scheme_id " + "join sys.filegroups fg on ds.data_space_id=fg.data_space_id";
        //get data file -> filegroup dependencies
        sql += " UNION select distinct null as referencing_schema_name, f.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.database_files f " + "join sys.data_spaces ds on f.data_space_id=ds.data_space_id " + "where f.data_space_id > 1";
        //get table -> filestream dependencies
        sql += " UNION select object_schema_name(t.object_id) as referencing_schema_name, t.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.tables t " + "join sys.data_spaces ds on t.filestream_data_space_id=ds.data_space_id " + "where t.filestream_data_space_id > 1";
        //get table -> filestream dependencies
        sql += " UNION select object_schema_name(t.object_id) as referencing_schema_name, t.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.tables t " + "join sys.data_spaces ds on t.lob_data_space_id=ds.data_space_id " + "where t.lob_data_space_id > 1";
        //get index -> filegroup dependencies
        sql += " UNION select object_schema_name(i.object_id) as referencing_schema_name, i.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.indexes i " + "join sys.data_spaces ds on i.data_space_id=ds.data_space_id " + "where i.data_space_id > 1";
        //get index -> table dependencies
        sql += " UNION select object_schema_name(i.object_id) as referencing_schema_name, i.name as referencing_name, object_name(i.object_id) as referenced_name, object_schema_name(i.object_id) as referenced_schema_name from sys.indexes i " + "where " + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "object_schema_name(i.object_id)='" + obj + "'";
            }
        });
        //get schema -> base object dependencies
        sql += " UNION SELECT SCHEMA_NAME(SCHEMA_ID) as referencing_schema_name, name as referencing_name, PARSENAME(BASE_OBJECT_NAME,1) AS referenced_name, (CASE WHEN PARSENAME(BASE_OBJECT_NAME,2) IS NULL THEN schema_name(schema_id) else PARSENAME(BASE_OBJECT_NAME,2) END) AS referenced_schema_name FROM SYS.SYNONYMS WHERE is_ms_shipped='false' AND " + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {

            @Override
            public String toString(String obj) {
                return "SCHEMA_NAME(SCHEMA_ID)='" + obj + "'";
            }
        });
        List<Map<String, ?>> rs = executor.queryForList(new RawSqlStatement(sql));
        if (rs.size() > 0) {
            for (Map<String, ?> row : rs) {
                String bName = StringUtils.trimToNull((String) row.get("REFERENCED_SCHEMA_NAME")) + "." + StringUtils.trimToNull((String) row.get("REFERENCED_NAME"));
                String tabName = StringUtils.trimToNull((String) row.get("REFERENCING_SCHEMA_NAME")) + "." + StringUtils.trimToNull((String) row.get("REFERENCING_NAME"));
                if (!bName.equals(tabName)) {
                    graph.add(bName, tabName);
                }
            }
        }
    }
}
Also used : DB2Database(liquibase.database.core.DB2Database) RawSqlStatement(liquibase.statement.core.RawSqlStatement) OracleDatabase(liquibase.database.core.OracleDatabase) Executor(liquibase.executor.Executor) StringUtils(liquibase.util.StringUtils) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 8 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class ChangedViewChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) changedObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    change.setReplaceIfExists(true);
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if (comparisonDatabase instanceof OracleDatabase && view.getColumns() != null && view.getColumns().size() > 0) {
        String viewName;
        if (change.getCatalogName() == null && change.getSchemaName() == null) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtils.join(view.getColumns(), ", ", new StringUtils.StringUtilsFormatter() {

            @Override
            public String toString(Object obj) {
                if (((Column) obj).getComputed() != null && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    return new Change[] { change };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) CreateViewChange(liquibase.change.core.CreateViewChange) DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) View(liquibase.structure.core.View)

Example 9 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class NCharType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof HsqlDatabase) {
        return new DatabaseDataType("CHAR", getParameters());
    }
    if (database instanceof OracleDatabase) {
        return new DatabaseDataType("NCHAR", getParameters());
    }
    if (database instanceof MSSQLDatabase) {
        Object[] parameters = getParameters();
        if (parameters.length > 0) {
            String param1 = parameters[0].toString();
            if (!param1.matches("\\d+") || new BigInteger(param1).compareTo(BigInteger.valueOf(4000)) > 0) {
                DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("nchar"), 4000);
                type.addAdditionalInformation(getAdditionalInformation());
                return type;
            }
        }
        if (parameters.length == 0) {
            parameters = new Object[] { 1 };
        } else if (parameters.length > 1) {
            parameters = Arrays.copyOfRange(parameters, 0, 1);
        }
        DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("nchar"), parameters);
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    }
    return super.toDatabaseDataType(database);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) HsqlDatabase(liquibase.database.core.HsqlDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) BigInteger(java.math.BigInteger) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 10 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class CreateProcedureGenerator method surroundWithSchemaSets.

/**
     * Convenience method for when the schemaName is set but we don't want to parse the body
     */
public static void surroundWithSchemaSets(List<Sql> sql, String schemaName, Database database) {
    if ((StringUtils.trimToNull(schemaName) != null) && !LiquibaseConfiguration.getInstance().getProperty(ChangeLogParserCofiguration.class, ChangeLogParserCofiguration.USE_PROCEDURE_SCHEMA).getValue(Boolean.class)) {
        String defaultSchema = database.getDefaultSchemaName();
        if (database instanceof OracleDatabase) {
            sql.add(0, new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(schemaName, Schema.class)));
            sql.add(new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(defaultSchema, Schema.class)));
        } else if (database instanceof DB2Database) {
            sql.add(0, new UnparsedSql("SET CURRENT SCHEMA " + schemaName));
            sql.add(new UnparsedSql("SET CURRENT SCHEMA " + defaultSchema));
        }
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) DB2Database(liquibase.database.core.DB2Database) UnparsedSql(liquibase.sql.UnparsedSql) Schema(liquibase.structure.core.Schema) ChangeLogParserCofiguration(liquibase.parser.ChangeLogParserCofiguration)

Aggregations

OracleDatabase (liquibase.database.core.OracleDatabase)31 Test (org.junit.Test)11 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)9 DB2Database (liquibase.database.core.DB2Database)8 MySQLDatabase (liquibase.database.core.MySQLDatabase)7 Change (liquibase.change.Change)6 ArrayList (java.util.ArrayList)5 Database (liquibase.database.Database)5 H2Database (liquibase.database.core.H2Database)4 Sql (liquibase.sql.Sql)4 HashSet (java.util.HashSet)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 DatabaseException (liquibase.exception.DatabaseException)3 LiquibaseException (liquibase.exception.LiquibaseException)3 IOException (java.io.IOException)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 ColumnConfig (liquibase.change.ColumnConfig)2 AddPrimaryKeyChange (liquibase.change.core.AddPrimaryKeyChange)2 AddUniqueConstraintChange (liquibase.change.core.AddUniqueConstraintChange)2 CreateViewChange (liquibase.change.core.CreateViewChange)2