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);
}
}
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;
}
}
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) {
}
}
}
}
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;
}
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;
}
}
Aggregations