Search in sources :

Example 21 with OracleDatabase

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

the class LoggingExecutor method outputStatement.

private void outputStatement(SqlStatement sql, List<SqlVisitor> sqlVisitors) throws DatabaseException {
    try {
        if (SqlGeneratorFactory.getInstance().generateStatementsVolatile(sql, database)) {
            throw new DatabaseException(sql.getClass().getSimpleName() + " requires access to up to date database metadata which is not available in SQL output mode");
        }
        if (sql instanceof ExecutablePreparedStatement) {
            output.write("WARNING: This statement uses a prepared statement which cannot be execute directly by this script. Only works in 'update' mode\n\n");
        }
        for (String statement : applyVisitors(sql, sqlVisitors)) {
            if (statement == null) {
                continue;
            }
            if (database instanceof OracleDatabase) {
                //remove trailing /
                while (statement.matches("(?s).*[\\s\\r\\n]*/[\\s\\r\\n]*$")) {
                    //all trailing /'s
                    statement = statement.replaceFirst("[\\s\\r\\n]*/[\\s\\r\\n]*$", "");
                }
            }
            output.write(statement);
            if (database instanceof MSSQLDatabase || database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
                output.write(StreamUtil.getLineSeparator());
                output.write("GO");
            //            } else if (database instanceof OracleDatabase) {
            //                output.write(StreamUtil.getLineSeparator());
            //                output.write("/");
            } else {
                String endDelimiter = ";";
                String potentialDelimiter = null;
                if (sql instanceof RawSqlStatement) {
                    potentialDelimiter = ((RawSqlStatement) sql).getEndDelimiter();
                } else if (sql instanceof CreateProcedureStatement) {
                    potentialDelimiter = ((CreateProcedureStatement) sql).getEndDelimiter();
                }
                if (potentialDelimiter != null) {
                    //ignore trailing $ as a regexp to determine if it should be output
                    potentialDelimiter = potentialDelimiter.replaceFirst("\\$$", "");
                    if (potentialDelimiter.replaceAll("\\n", "\n").replace("\\r", "\r").matches("[;/\r\n\\w@\\-]+")) {
                        endDelimiter = potentialDelimiter;
                    }
                }
                endDelimiter = endDelimiter.replace("\\n", "\n");
                endDelimiter = endDelimiter.replace("\\r", "\r");
                if (!statement.endsWith(endDelimiter)) {
                    output.write(endDelimiter);
                }
            }
            output.write(StreamUtil.getLineSeparator());
            output.write(StreamUtil.getLineSeparator());
        }
    } catch (IOException e) {
        throw new DatabaseException(e);
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SybaseDatabase(liquibase.database.core.SybaseDatabase) CreateProcedureStatement(liquibase.statement.core.CreateProcedureStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) IOException(java.io.IOException) DatabaseException(liquibase.exception.DatabaseException) ExecutablePreparedStatement(liquibase.statement.ExecutablePreparedStatement)

Example 22 with OracleDatabase

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

the class ChangedUniqueConstraintChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();
    UniqueConstraint uniqueConstraint = (UniqueConstraint) changedObject;
    DropUniqueConstraintChange dropUniqueConstraintChange = createDropUniqueConstraintChange();
    dropUniqueConstraintChange.setTableName(uniqueConstraint.getTable().getName());
    dropUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
    AddUniqueConstraintChange addUniqueConstraintChange = createAddUniqueConstraintChange();
    addUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
    addUniqueConstraintChange.setTableName(uniqueConstraint.getTable().getName());
    addUniqueConstraintChange.setColumnNames(uniqueConstraint.getColumnNames());
    returnList.add(dropUniqueConstraintChange);
    if (control.getIncludeCatalog()) {
        dropUniqueConstraintChange.setCatalogName(uniqueConstraint.getSchema().getCatalogName());
        addUniqueConstraintChange.setCatalogName(uniqueConstraint.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropUniqueConstraintChange.setSchemaName(uniqueConstraint.getSchema().getName());
        addUniqueConstraintChange.setSchemaName(uniqueConstraint.getSchema().getName());
    }
    Index backingIndex = uniqueConstraint.getBackingIndex();
    if (comparisonDatabase instanceof OracleDatabase) {
        if (backingIndex != null && backingIndex.getName() != null) {
            Change[] missingIndexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (missingIndexChanges != null) {
                returnList.addAll(Arrays.asList(missingIndexChanges));
            }
            addUniqueConstraintChange.setForIndexName(backingIndex.getName());
            Schema schema = backingIndex.getSchema();
            if (schema != null) {
                if (control.getIncludeCatalog()) {
                    addUniqueConstraintChange.setForIndexCatalogName(schema.getCatalogName());
                }
                if (control.getIncludeSchema()) {
                    addUniqueConstraintChange.setForIndexSchemaName(schema.getName());
                }
            }
        }
    }
    control.setAlreadyHandledChanged(backingIndex);
    returnList.add(addUniqueConstraintChange);
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) Schema(liquibase.structure.core.Schema) ArrayList(java.util.ArrayList) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change)

Example 23 with OracleDatabase

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

the class ChangedColumnChangeGenerator method handleTypeDifferences.

protected void handleTypeDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List<Change> changes, Database referenceDatabase, Database comparisonDatabase) {
    Difference typeDifference = differences.getDifference("type");
    if (typeDifference != null) {
        String catalogName = null;
        String schemaName = null;
        if (control.getIncludeCatalog()) {
            catalogName = column.getRelation().getSchema().getCatalog().getName();
        }
        if (control.getIncludeSchema()) {
            schemaName = column.getRelation().getSchema().getName();
        }
        String tableName = column.getRelation().getName();
        if (comparisonDatabase instanceof OracleDatabase && (((DataType) typeDifference.getReferenceValue()).getTypeName().equalsIgnoreCase("clob") || ((DataType) typeDifference.getComparedValue()).getTypeName().equalsIgnoreCase("clob"))) {
            String tempColName = "TEMP_CLOB_CONVERT";
            OutputChange outputChange = new OutputChange();
            outputChange.setMessage("Cannot convert directly from " + ((DataType) typeDifference.getComparedValue()).getTypeName() + " to " + ((DataType) typeDifference.getReferenceValue()).getTypeName() + ". Instead a new column will be created and the data transferred. This may cause unexpected side effects including constraint issues and/or table locks.");
            changes.add(outputChange);
            AddColumnChange addColumn = new AddColumnChange();
            addColumn.setCatalogName(catalogName);
            addColumn.setSchemaName(schemaName);
            addColumn.setTableName(tableName);
            AddColumnConfig addColumnConfig = new AddColumnConfig(column);
            addColumnConfig.setName(tempColName);
            addColumnConfig.setType(typeDifference.getReferenceValue().toString());
            addColumnConfig.setAfterColumn(column.getName());
            addColumn.setColumns(Arrays.asList(addColumnConfig));
            changes.add(addColumn);
            changes.add(new RawSQLChange("UPDATE " + referenceDatabase.escapeObjectName(tableName, Table.class) + " SET " + tempColName + "=" + referenceDatabase.escapeObjectName(column.getName(), Column.class)));
            DropColumnChange dropColumnChange = new DropColumnChange();
            dropColumnChange.setCatalogName(catalogName);
            dropColumnChange.setSchemaName(schemaName);
            dropColumnChange.setTableName(tableName);
            dropColumnChange.setColumnName(column.getName());
            changes.add(dropColumnChange);
            RenameColumnChange renameColumnChange = new RenameColumnChange();
            renameColumnChange.setCatalogName(catalogName);
            renameColumnChange.setSchemaName(schemaName);
            renameColumnChange.setTableName(tableName);
            renameColumnChange.setOldColumnName(tempColName);
            renameColumnChange.setNewColumnName(column.getName());
            changes.add(renameColumnChange);
        } else {
            ModifyDataTypeChange change = new ModifyDataTypeChange();
            change.setCatalogName(catalogName);
            change.setSchemaName(schemaName);
            change.setTableName(tableName);
            change.setColumnName(column.getName());
            DataType referenceType = (DataType) typeDifference.getReferenceValue();
            change.setNewDataType(DataTypeFactory.getInstance().from(referenceType, comparisonDatabase).toString());
            changes.add(change);
        }
    }
}
Also used : Difference(liquibase.diff.Difference) OracleDatabase(liquibase.database.core.OracleDatabase) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) AddColumnConfig(liquibase.change.AddColumnConfig)

Example 24 with OracleDatabase

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

the class ChangedPrimaryKeyChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    //don't try to recreate PKs that differ in just clustered
    Difference clusteredDiff = differences.getDifference("clustered");
    if (clusteredDiff != null) {
        if (clusteredDiff.getReferenceValue() == null || clusteredDiff.getComparedValue() == null) {
            differences.removeDifference("clustered");
        }
    }
    if (!differences.hasDifferences()) {
        return new Change[0];
    }
    PrimaryKey pk = (PrimaryKey) changedObject;
    List<Change> returnList = new ArrayList<Change>();
    DropPrimaryKeyChange dropPkChange = new DropPrimaryKeyChange();
    dropPkChange.setTableName(pk.getTable().getName());
    returnList.add(dropPkChange);
    AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange();
    addPkChange.setTableName(pk.getTable().getName());
    addPkChange.setColumnNames(pk.getColumnNames());
    addPkChange.setConstraintName(pk.getName());
    if (comparisonDatabase instanceof OracleDatabase) {
        Index backingIndex = pk.getBackingIndex();
        if (backingIndex != null && backingIndex.getName() != null) {
            Change[] indexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (indexChanges != null) {
                returnList.addAll(Arrays.asList(indexChanges));
            }
            addPkChange.setForIndexName(backingIndex.getName());
            Schema schema = backingIndex.getSchema();
            if (schema != null) {
                if (control.getIncludeCatalog()) {
                    addPkChange.setForIndexCatalogName(schema.getCatalogName());
                }
                if (control.getIncludeSchema()) {
                    addPkChange.setForIndexSchemaName(schema.getName());
                }
            }
        }
    }
    returnList.add(addPkChange);
    if (control.getIncludeCatalog()) {
        dropPkChange.setCatalogName(pk.getSchema().getCatalogName());
        addPkChange.setCatalogName(pk.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropPkChange.setSchemaName(pk.getSchema().getName());
        addPkChange.setSchemaName(pk.getSchema().getName());
    }
    Difference columnDifferences = differences.getDifference("columns");
    List<Column> referenceColumns;
    List<Column> comparedColumns;
    if (columnDifferences == null) {
        referenceColumns = pk.getColumns();
        comparedColumns = pk.getColumns();
    } else {
        referenceColumns = (List<Column>) columnDifferences.getReferenceValue();
        comparedColumns = (List<Column>) columnDifferences.getComparedValue();
    }
    StringUtils.ToStringFormatter formatter = new StringUtils.ToStringFormatter();
    control.setAlreadyHandledChanged(new Index().setTable(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new Index().setTable(pk.getTable()).setColumns(comparedColumns));
    }
    control.setAlreadyHandledChanged(new UniqueConstraint().setTable(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new UniqueConstraint().setTable(pk.getTable()).setColumns(comparedColumns));
    }
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : ArrayList(java.util.ArrayList) Difference(liquibase.diff.Difference) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) Change(liquibase.change.Change) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) OracleDatabase(liquibase.database.core.OracleDatabase) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) StringUtils(liquibase.util.StringUtils) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange)

Example 25 with OracleDatabase

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

the class UnknownType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    int dataTypeMaxParameters;
    if (getName().equalsIgnoreCase("enum") || getName().equalsIgnoreCase("set")) {
        dataTypeMaxParameters = Integer.MAX_VALUE;
    } else {
        dataTypeMaxParameters = database.getDataTypeMaxParameters(getName());
    }
    Object[] parameters = getParameters();
    if (database instanceof OracleDatabase) {
        if (getName().equalsIgnoreCase("LONG") || getName().equalsIgnoreCase("BFILE") || getName().equalsIgnoreCase("ROWID") || getName().equalsIgnoreCase("ANYDATA") || getName().equalsIgnoreCase("SDO_GEOMETRY")) {
            parameters = new Object[0];
        } else if (getName().equalsIgnoreCase("RAW")) {
            return new DatabaseDataType(getName(), parameters);
        } else if (getName().toUpperCase().startsWith("INTERVAL ")) {
            return new DatabaseDataType(getName().replaceAll("\\(\\d+\\)", ""));
        } else {
            //probably a user defined type. Can't call getUserDefinedTypes() to know for sure, since that returns all types including system types.
            return new DatabaseDataType(getName().toUpperCase());
        }
    }
    if (dataTypeMaxParameters < parameters.length) {
        parameters = Arrays.copyOfRange(parameters, 0, dataTypeMaxParameters);
    }
    DatabaseDataType type;
    if (database instanceof MSSQLDatabase) {
        type = new DatabaseDataType(database.escapeDataTypeName(getName()), parameters);
    } else {
        type = new DatabaseDataType(getName().toUpperCase(), parameters);
    }
    type.addAdditionalInformation(getAdditionalInformation());
    return type;
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

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