Search in sources :

Example 1 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 2 with OracleDatabase

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

the class ChangeMetaDataTest method getRequiredParameters.

@Test
public void getRequiredParameters() {
    HashSet<ChangeParameterMetaData> parameters = new HashSet<ChangeParameterMetaData>();
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "noneRequired", "x", null, null, null, Integer.class, new String[] { "none" }, null, null, null));
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "allRequired", "x", null, null, null, Integer.class, new String[] { "all" }, null, null, null));
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "h2Required", "x", null, null, null, Integer.class, new String[] { "h2" }, null, null, null));
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "oracleRequired", "x", null, null, null, Integer.class, new String[] { "oracle" }, null, null, null));
    ChangeMetaData changeMetaData = new ChangeMetaData("x", "y", 1, null, null, parameters);
    assertSetsEqual(new String[] { "allRequired", "h2Required" }, changeMetaData.getRequiredParameters(new H2Database()).keySet());
    assertSetsEqual(new String[] { "allRequired", "oracleRequired" }, changeMetaData.getRequiredParameters(new OracleDatabase()).keySet());
    assertSetsEqual(new String[] { "allRequired" }, changeMetaData.getRequiredParameters(new MySQLDatabase()).keySet());
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) H2Database(liquibase.database.core.H2Database) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with OracleDatabase

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

the class ChangeMetaDataTest method getOptionalParameters.

@Test
public void getOptionalParameters() {
    HashSet<ChangeParameterMetaData> parameters = new HashSet<ChangeParameterMetaData>();
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "noneRequired", "x", null, null, null, Integer.class, new String[] { "none" }, null, null, null));
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "allRequired", "x", null, null, null, Integer.class, new String[] { "all" }, null, null, null));
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "h2Required", "x", null, null, null, Integer.class, new String[] { "h2" }, null, null, null));
    parameters.add(new ChangeParameterMetaData(new ExampleAbstractChange(), "oracleRequired", "x", null, null, null, Integer.class, new String[] { "oracle" }, null, null, null));
    ChangeMetaData changeMetaData = new ChangeMetaData("x", "y", 1, null, null, parameters);
    assertSetsEqual(new String[] { "noneRequired", "oracleRequired" }, changeMetaData.getOptionalParameters(new H2Database()).keySet());
    assertSetsEqual(new String[] { "noneRequired", "h2Required" }, changeMetaData.getOptionalParameters(new OracleDatabase()).keySet());
    assertSetsEqual(new String[] { "noneRequired", "h2Required", "oracleRequired" }, changeMetaData.getOptionalParameters(new MySQLDatabase()).keySet());
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) H2Database(liquibase.database.core.H2Database) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with OracleDatabase

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

the class MissingUniqueConstraintChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<>();
    UniqueConstraint uc = (UniqueConstraint) missingObject;
    if (uc.getRelation() == null) {
        return null;
    }
    AddUniqueConstraintChange change = createAddUniqueConstraintChange();
    change.setTableName(uc.getRelation().getName());
    if ((uc.getBackingIndex() != null) && control.getIncludeTablespace()) {
        change.setTablespace(uc.getBackingIndex().getTablespace());
    }
    if (control.getIncludeCatalog()) {
        change.setCatalogName(uc.getRelation().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(uc.getRelation().getSchema().getName());
    }
    change.setConstraintName(uc.getName());
    change.setColumnNames(uc.getColumnNames());
    change.setDeferrable(uc.isDeferrable() ? Boolean.TRUE : null);
    change.setValidate(!uc.shouldValidate() ? Boolean.FALSE : null);
    change.setInitiallyDeferred(uc.isInitiallyDeferred() ? Boolean.TRUE : null);
    change.setDisabled(uc.isDisabled() ? Boolean.TRUE : null);
    if (referenceDatabase instanceof MSSQLDatabase) {
        change.setClustered(uc.isClustered() ? Boolean.TRUE : null);
    }
    if (comparisonDatabase instanceof OracleDatabase) {
        Index backingIndex = uc.getBackingIndex();
        if ((backingIndex != null) && (backingIndex.getName() != null)) {
            if (referenceDatabase.equals(comparisonDatabase) || !alreadyExists(backingIndex, comparisonDatabase, control)) {
                Change[] changes = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
                if (changes != null) {
                    returnList.addAll(Arrays.asList(changes));
                    change.setForIndexName(backingIndex.getName());
                    Schema schema = backingIndex.getSchema();
                    if (schema != null) {
                        if (control.getIncludeCatalog()) {
                            change.setForIndexCatalogName(schema.getCatalogName());
                        }
                        if (control.getIncludeSchema()) {
                            change.setForIndexSchemaName(schema.getName());
                        }
                    }
                }
            }
        }
    }
    Index backingIndex = uc.getBackingIndex();
    // if (backingIndex == null) {
    // Index exampleIndex = new Index().setTable(uc.getTable());
    // for (String col : uc.getColumns()) {
    // exampleIndex.getColumns().add(col);
    // }
    // control.setAlreadyHandledMissing(exampleIndex);
    // } else {
    control.setAlreadyHandledMissing(backingIndex);
    // }
    returnList.add(change);
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) ArrayList(java.util.ArrayList) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 5 with OracleDatabase

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

the class UpdateDataChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    boolean needsPreparedStatement = false;
    for (ColumnConfig column : getColumns()) {
        if (column.getValueBlobFile() != null) {
            needsPreparedStatement = true;
        }
        if (column.getValueClobFile() != null) {
            needsPreparedStatement = true;
        }
        if ((database instanceof OracleDatabase) && (column.getType() != null) && "CLOB".equalsIgnoreCase(column.getType()) && (column.getValue() != null) && (column.getValue().length() >= 4000)) {
            needsPreparedStatement = true;
        }
        final Object valueObject = column.getValueObject();
        if (valueObject instanceof SequenceNextValueFunction) {
            ((SequenceNextValueFunction) valueObject).setSchemaName(this.getSchemaName());
        }
        if (valueObject instanceof SequenceCurrentValueFunction) {
            ((SequenceCurrentValueFunction) valueObject).setSchemaName(this.getSchemaName());
        }
    }
    if (needsPreparedStatement) {
        UpdateExecutablePreparedStatement statement = new UpdateExecutablePreparedStatement(database, catalogName, schemaName, tableName, columns, getChangeSet(), Scope.getCurrentScope().getResourceAccessor());
        statement.setWhereClause(where);
        for (ColumnConfig whereParam : whereParams) {
            if (whereParam.getName() != null) {
                statement.addWhereColumnName(whereParam.getName());
            }
            statement.addWhereParameter(whereParam.getValueObject());
        }
        return new SqlStatement[] { statement };
    }
    UpdateStatement statement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
    for (ColumnConfig column : getColumns()) {
        statement.addNewColumnValue(column.getName(), column.getValueObject());
    }
    statement.setWhereClause(where);
    for (ColumnConfig whereParam : whereParams) {
        if (whereParam.getName() != null) {
            statement.addWhereColumnName(whereParam.getName());
        }
        statement.addWhereParameter(whereParam.getValueObject());
    }
    return new SqlStatement[] { statement };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) SequenceCurrentValueFunction(liquibase.statement.SequenceCurrentValueFunction) SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) UpdateExecutablePreparedStatement(liquibase.statement.UpdateExecutablePreparedStatement) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction)

Aggregations

OracleDatabase (liquibase.database.core.OracleDatabase)40 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)17 Test (org.junit.Test)17 Sql (liquibase.sql.Sql)16 MySQLDatabase (liquibase.database.core.MySQLDatabase)15 Database (liquibase.database.Database)11 PostgresDatabase (liquibase.database.core.PostgresDatabase)11 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)10 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)10 H2Database (liquibase.database.core.H2Database)9 SybaseDatabase (liquibase.database.core.SybaseDatabase)8 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)8 ArrayList (java.util.ArrayList)7 HsqlDatabase (liquibase.database.core.HsqlDatabase)7 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)7 CreateTableStatement (liquibase.statement.core.CreateTableStatement)7 DerbyDatabase (liquibase.database.core.DerbyDatabase)6 Change (liquibase.change.Change)5 MockDatabaseConnection (liquibase.database.MockDatabaseConnection)5 ColumnConfig (liquibase.change.ColumnConfig)4