Search in sources :

Example 46 with UnexpectedLiquibaseException

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

the class DropAllForeignKeyConstraintsChange method generateChildren.

private List<DropForeignKeyConstraintChange> generateChildren(Database database) {
    // Make a new list
    List<DropForeignKeyConstraintChange> childDropChanges = new ArrayList<DropForeignKeyConstraintChange>();
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    FindForeignKeyConstraintsStatement sql = new FindForeignKeyConstraintsStatement(getBaseTableCatalogName(), getBaseTableSchemaName(), getBaseTableName());
    try {
        List<Map<String, ?>> results = executor.queryForList(sql);
        Set<String> handledConstraints = new HashSet<String>();
        if (results != null && results.size() > 0) {
            for (Map result : results) {
                String baseTableName = (String) result.get(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME);
                String constraintName = (String) result.get(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME);
                if (DatabaseObjectComparatorFactory.getInstance().isSameObject(new Table().setName(getBaseTableName()), new Table().setName(baseTableName), null, database)) {
                    if (!handledConstraints.contains(constraintName)) {
                        DropForeignKeyConstraintChange dropForeignKeyConstraintChange = new DropForeignKeyConstraintChange();
                        dropForeignKeyConstraintChange.setBaseTableSchemaName(getBaseTableSchemaName());
                        dropForeignKeyConstraintChange.setBaseTableName(baseTableName);
                        dropForeignKeyConstraintChange.setConstraintName(constraintName);
                        childDropChanges.add(dropForeignKeyConstraintChange);
                        handledConstraints.add(constraintName);
                    }
                } else {
                    throw new IllegalStateException("Expected to return only foreign keys for base table name: " + getBaseTableName() + " and got results for table: " + baseTableName);
                }
            }
        }
        return childDropChanges;
    } catch (DatabaseException e) {
        throw new UnexpectedLiquibaseException("Failed to find foreign keys for table: " + getBaseTableName(), e);
    }
}
Also used : Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) Executor(liquibase.executor.Executor) FindForeignKeyConstraintsStatement(liquibase.statement.core.FindForeignKeyConstraintsStatement) Map(java.util.Map) DatabaseException(liquibase.exception.DatabaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) HashSet(java.util.HashSet)

Example 47 with UnexpectedLiquibaseException

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

the class SQLFileChange method getSql.

@Override
@DatabaseChangeProperty(isChangeProperty = false)
public String getSql() {
    String sql = super.getSql();
    if (sql == null) {
        InputStream sqlStream;
        try {
            sqlStream = openSqlStream();
            if (sqlStream == null) {
                return null;
            }
            String content = StreamUtil.getStreamContents(sqlStream, encoding);
            if (getChangeSet() != null) {
                ChangeLogParameters parameters = getChangeSet().getChangeLogParameters();
                if (parameters != null) {
                    content = parameters.expandExpressions(content, getChangeSet().getChangeLog());
                }
            }
            return content;
        } catch (IOException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    } else {
        return sql;
    }
}
Also used : InputStream(java.io.InputStream) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) IOException(java.io.IOException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 48 with UnexpectedLiquibaseException

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

the class LoadDataChange method generateCheckSum.

@Override
public CheckSum generateCheckSum() {
    InputStream stream = null;
    try {
        stream = StreamUtil.openStream(file, isRelativeToChangelogFile(), getChangeSet(), getResourceAccessor());
        if (stream == null) {
            throw new UnexpectedLiquibaseException(getFile() + " could not be found");
        }
        stream = new EmptyLineAndCommentSkippingInputStream(stream, commentLineStartsWith);
        return CheckSum.compute(getTableName() + ":" + CheckSum.compute(stream, /*standardizeLineEndings*/
        true));
    } catch (IOException e) {
        throw new UnexpectedLiquibaseException(e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : EmptyLineAndCommentSkippingInputStream(liquibase.io.EmptyLineAndCommentSkippingInputStream) InputStream(java.io.InputStream) EmptyLineAndCommentSkippingInputStream(liquibase.io.EmptyLineAndCommentSkippingInputStream) IOException(java.io.IOException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 49 with UnexpectedLiquibaseException

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

the class SqlParser method parse.

public static StringClauses parse(String sqlBlock, boolean preserveWhitespace, boolean preserveComments) {
    StringClauses clauses = new StringClauses(preserveWhitespace ? "" : " ");
    SimpleSqlGrammarTokenManager tokenManager = new SimpleSqlGrammarTokenManager(new SimpleCharStream(new StringReader(sqlBlock)));
    SimpleSqlGrammar t = new SimpleSqlGrammar(tokenManager);
    try {
        Token token = t.getNextToken();
        while (!token.toString().equals("")) {
            if (token.kind == SimpleSqlGrammarConstants.WHITESPACE) {
                if (preserveWhitespace) {
                    clauses.append(new StringClauses.Whitespace(token.image));
                }
            } else if (token.kind == SimpleSqlGrammarConstants.LINE_COMMENT || token.kind == SimpleSqlGrammarConstants.MULTI_LINE_COMMENT) {
                if (preserveComments) {
                    String comment = token.image;
                    if (!preserveWhitespace && token.kind == SimpleSqlGrammarConstants.LINE_COMMENT) {
                        if (!comment.endsWith("\n")) {
                            comment = comment + "\n";
                        }
                    }
                    clauses.append(new StringClauses.Comment(comment));
                }
            } else {
                clauses.append(token.image);
            }
            token = t.getNextToken();
        }
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
    return clauses;
}
Also used : StringReader(java.io.StringReader) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 50 with UnexpectedLiquibaseException

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

the class ContextExpression method matches.

private boolean matches(String expression, Contexts runtimeContexts) {
    if (runtimeContexts.isEmpty()) {
        return true;
    }
    if (expression.trim().equals(":TRUE")) {
        return true;
    }
    if (expression.trim().equals(":FALSE")) {
        return false;
    }
    while (expression.contains("(")) {
        Pattern pattern = Pattern.compile("(.*?)\\((.*?)\\)(.*)");
        Matcher matcher = pattern.matcher(expression);
        if (!matcher.matches()) {
            throw new UnexpectedLiquibaseException("Cannot parse context pattern " + expression);
        }
        String parenExpression = matcher.group(2);
        parenExpression = ":" + String.valueOf(matches(parenExpression, runtimeContexts)).toUpperCase();
        expression = matcher.group(1) + " " + parenExpression + " " + matcher.group(3);
    }
    String[] orSplit = expression.split("\\s+or\\s+");
    if (orSplit.length > 1) {
        for (String split : orSplit) {
            if (matches(split, runtimeContexts)) {
                return true;
            }
        }
        return false;
    }
    String[] andSplit = expression.split("\\s+and\\s+");
    if (andSplit.length > 1) {
        for (String split : andSplit) {
            if (!matches(split, runtimeContexts)) {
                return false;
            }
        }
        return true;
    }
    boolean notExpression = false;
    if (expression.startsWith("!")) {
        notExpression = true;
        expression = expression.substring(1);
    }
    for (String context : runtimeContexts.getContexts()) {
        if (context.equalsIgnoreCase(expression)) {
            if (notExpression) {
                return false;
            } else {
                return true;
            }
        }
    }
    if (notExpression) {
        return true;
    } else {
        return false;
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) 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