Search in sources :

Example 31 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class SelectFromDatabaseChangeLogGenerator method generateSql.

@Override
public Sql[] generateSql(SelectFromDatabaseChangeLogStatement statement, final Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<ColumnConfig> columnsToSelect = Arrays.asList(statement.getColumnsToSelect());
    String sql = "SELECT " + (database instanceof MSSQLDatabase && statement.getLimit() != null ? "TOP " + statement.getLimit() + " " : "") + StringUtils.join(columnsToSelect, ",", new StringUtils.StringUtilsFormatter<ColumnConfig>() {

        @Override
        public String toString(ColumnConfig column) {
            if (column.getComputed() != null && column.getComputed()) {
                return column.getName();
            } else {
                return database.escapeColumnName(null, null, null, column.getName());
            }
        }
    }).toUpperCase() + " FROM " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
    SelectFromDatabaseChangeLogStatement.WhereClause whereClause = statement.getWhereClause();
    if (whereClause != null) {
        if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByTag) {
            sql += " WHERE " + database.escapeColumnName(null, null, null, "TAG") + "='" + ((SelectFromDatabaseChangeLogStatement.ByTag) whereClause).getTagName() + "'";
        } else if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByNotNullCheckSum) {
            sql += " WHERE " + database.escapeColumnName(null, null, null, "MD5SUM") + " IS NOT NULL";
        } else {
            throw new UnexpectedLiquibaseException("Unknown where clause type: " + whereClause.getClass().getName());
        }
    }
    if (statement.getOrderByColumns() != null && statement.getOrderByColumns().length > 0) {
        sql += " ORDER BY ";
        Iterator<String> orderBy = Arrays.asList(statement.getOrderByColumns()).iterator();
        while (orderBy.hasNext()) {
            String orderColumn = orderBy.next();
            String[] orderColumnData = orderColumn.split(" ");
            sql += database.escapeColumnName(null, null, null, orderColumnData[0]);
            if (orderColumnData.length == 2) {
                sql += " ";
                sql += orderColumnData[1].toUpperCase();
            }
            if (orderBy.hasNext()) {
                sql += ", ";
            }
        }
    }
    if (statement.getLimit() != null) {
        if (database instanceof OracleDatabase) {
            if (whereClause == null) {
                sql += " WHERE ROWNUM=" + statement.getLimit();
            } else {
                sql += " AND ROWNUM=" + statement.getLimit();
            }
        } else if (database instanceof MySQLDatabase || database instanceof PostgresDatabase) {
            sql += " LIMIT " + statement.getLimit();
        } else if (database instanceof DB2Database) {
            sql += " FETCH FIRST " + statement.getLimit() + " ROWS ONLY";
        }
    }
    return new Sql[] { new UnparsedSql(sql) };
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) UnparsedSql(liquibase.sql.UnparsedSql) SelectFromDatabaseChangeLogStatement(liquibase.statement.core.SelectFromDatabaseChangeLogStatement) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) StringUtils(liquibase.util.StringUtils) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 32 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class MD5Util method computeMD5.

public static String computeMD5(String input) {
    if (input == null) {
        return null;
    }
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
        digest.update(input.getBytes(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
    byte[] digestBytes = digest.digest();
    String returnString = new String(encodeHex(digestBytes));
    String inputToLog = input;
    if (inputToLog.length() > 500) {
        inputToLog = inputToLog.substring(0, 500) + "... [truncated in log]";
    }
    LogFactory.getLogger().debug("Computed checksum for " + inputToLog + " as " + returnString);
    return returnString;
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) MessageDigest(java.security.MessageDigest) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 33 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class ObjectUtil method setProperty.

public static void setProperty(Object object, String propertyName, String propertyValue) {
    Method method = getWriteMethod(object, propertyName);
    if (method == null) {
        throw new UnexpectedLiquibaseException("Property '" + propertyName + "' not found on object type " + object.getClass().getName());
    }
    Class<?> parameterType = method.getParameterTypes()[0];
    Object finalValue = propertyValue;
    if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) {
        finalValue = Boolean.valueOf(propertyValue);
    } else if (parameterType.equals(Integer.class)) {
        finalValue = Integer.valueOf(propertyValue);
    } else if (parameterType.equals(Long.class)) {
        finalValue = Long.valueOf(propertyValue);
    } else if (parameterType.equals(BigInteger.class)) {
        finalValue = new BigInteger(propertyValue);
    } else if (parameterType.equals(BigDecimal.class)) {
        finalValue = new BigDecimal(propertyValue);
    } else if (parameterType.equals(DatabaseFunction.class)) {
        finalValue = new DatabaseFunction(propertyValue);
    } else if (parameterType.equals(SequenceNextValueFunction.class)) {
        finalValue = new SequenceNextValueFunction(propertyValue);
    } else if (parameterType.equals(SequenceCurrentValueFunction.class)) {
        finalValue = new SequenceCurrentValueFunction(propertyValue);
    } else if (Enum.class.isAssignableFrom(parameterType)) {
        finalValue = Enum.valueOf((Class<Enum>) parameterType, propertyValue);
    }
    try {
        method.invoke(object, finalValue);
    } catch (IllegalAccessException e) {
        throw new UnexpectedLiquibaseException(e);
    } catch (IllegalArgumentException e) {
        throw new UnexpectedLiquibaseException("Cannot call " + method.toString() + " with value of type " + finalValue.getClass().getName());
    } catch (InvocationTargetException e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : SequenceCurrentValueFunction(liquibase.statement.SequenceCurrentValueFunction) DatabaseFunction(liquibase.statement.DatabaseFunction) Method(java.lang.reflect.Method) BigDecimal(java.math.BigDecimal) InvocationTargetException(java.lang.reflect.InvocationTargetException) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 34 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class AbstractExecuteTest method resetAvailableDatabases.

public void resetAvailableDatabases() throws Exception {
    for (Database database : DatabaseTestContext.getInstance().getAvailableDatabases()) {
        DatabaseConnection connection = database.getConnection();
        Statement connectionStatement = ((JdbcConnection) connection).getUnderlyingConnection().createStatement();
        try {
            database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
        } catch (Throwable e) {
            throw new UnexpectedLiquibaseException("Error dropping objects for database " + database.getShortName(), e);
        }
        try {
            connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
        } catch (SQLException e) {
            ;
        }
        connection.commit();
        try {
            connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
        } catch (SQLException e) {
            ;
        }
        connection.commit();
        if (database.supportsSchemas()) {
            database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA));
            connection.commit();
            try {
                connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogLockTableName()));
            } catch (SQLException e) {
            //ok
            }
            connection.commit();
            try {
                connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogTableName()));
            } catch (SQLException e) {
            //ok
            }
            connection.commit();
        }
        List<? extends SqlStatement> setupStatements = setupStatements(database);
        if (setupStatements != null) {
            for (SqlStatement statement : setupStatements) {
                ExecutorService.getInstance().getExecutor(database).execute(statement);
            }
        }
        connectionStatement.close();
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) SQLException(java.sql.SQLException) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) MockDatabase(liquibase.sdk.database.MockDatabase) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) DatabaseConnection(liquibase.database.DatabaseConnection) CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 35 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project dropwizard by dropwizard.

the class DbDumpCommand method generateChangeLog.

private void generateChangeLog(final Database database, final CatalogAndSchema catalogAndSchema, final DiffToChangeLog changeLogWriter, PrintStream outputStream, final Set<Class<? extends DatabaseObject>> compareTypes) throws DatabaseException, IOException, ParserConfigurationException {
    @SuppressWarnings({ "unchecked", "rawtypes" }) final SnapshotControl snapshotControl = new SnapshotControl(database, compareTypes.toArray(new Class[compareTypes.size()]));
    final CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, compareTypes);
    final CatalogAndSchema[] compareControlSchemas = compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE);
    try {
        final DatabaseSnapshot referenceSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControlSchemas, database, snapshotControl);
        final DatabaseSnapshot comparisonSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControlSchemas, null, snapshotControl);
        final DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(referenceSnapshot, comparisonSnapshot, compareControl);
        changeLogWriter.setDiffResult(diffResult);
        changeLogWriter.print(outputStream);
    } catch (InvalidExampleException e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) CatalogAndSchema(liquibase.CatalogAndSchema) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)75 DatabaseException (liquibase.exception.DatabaseException)12 IOException (java.io.IOException)10 Database (liquibase.database.Database)10 DatabaseObject (liquibase.structure.DatabaseObject)10 LiquibaseException (liquibase.exception.LiquibaseException)9 InvalidExampleException (liquibase.snapshot.InvalidExampleException)9 CatalogAndSchema (liquibase.CatalogAndSchema)8 InputStream (java.io.InputStream)7 ParsedNodeException (liquibase.parser.core.ParsedNodeException)7 SQLException (java.sql.SQLException)6 DatabaseFunction (liquibase.statement.DatabaseFunction)6 SqlStatement (liquibase.statement.SqlStatement)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 Change (liquibase.change.Change)5 ColumnConfig (liquibase.change.ColumnConfig)5 Executor (liquibase.executor.Executor)5 Sql (liquibase.sql.Sql)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4