Skip to content

Commit

Permalink
Tweaked exception message for invalid formatted SQL file (#4870)
Browse files Browse the repository at this point in the history
DAT-15874

Co-authored-by: Wesley Willard <wwillard@datical.com>
  • Loading branch information
filipelautert and wwillard7800 committed Sep 11, 2023
1 parent 727c540 commit b3fb003
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
handleProperty(changeLogParameters, changeLog, line);
continue;
} else if (altPropertyPatternMatcher.matches()) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--property name=<property name> value=<property value>", getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--property name=<property name> value=<property value>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}
Matcher changeLogPatterMatcher = FIRST_LINE_PATTERN.matcher(line);
Expand All @@ -298,7 +298,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
}
} else if (altIgnoreLinesOneDashMatcher.matches()) {
String message =
String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--ignoreLines:end", getDocumentationLink());
String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--ignoreLines:end", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}
}
Expand All @@ -317,7 +317,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
}
} else if (altIgnoreLinesOneDashMatcher.matches() || altIgnoreMatcher.matches()) {
String message =
String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--ignoreLines:<count|start>", getDocumentationLink());
String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--ignoreLines:<count|start>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}

Expand Down Expand Up @@ -415,7 +415,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
Matcher changeSetAuthorIdPatternMatcher = changeSetAuthorIdPattern.matcher(line);
if (!changeSetAuthorIdPatternMatcher.matches()) {
String message =
String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--changeset <authorname>:<changesetId>", getDocumentationLink());
String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--changeset <authorname>:<changesetId>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}

Expand All @@ -441,11 +441,11 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
Matcher altChangeSetOneDashPatternMatcher = ALT_CHANGE_SET_ONE_CHARACTER_PATTERN.matcher(line);
Matcher altChangeSetNoOtherInfoPatternMatcher = ALT_CHANGE_SET_NO_OTHER_INFO_PATTERN.matcher(line);
if (altChangeSetOneDashPatternMatcher.matches() || altChangeSetNoOtherInfoPatternMatcher.matches()) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--changeset <authorname>:<changesetId>", getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--changeset <authorname>:<changesetId>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}
if (changeSet != null) {
configureChangeSet(changeLogParameters, reader, currentSequence, currentRollbackSequence, changeSet, count, line, commentMatcher);
configureChangeSet(physicalChangeLogLocation, changeLogParameters, reader, currentSequence, currentRollbackSequence, changeSet, count, line, commentMatcher);
} else {
if (commentMatcher.matches()) {
String message =
Expand Down Expand Up @@ -473,7 +473,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
return changeLog;
}

protected void configureChangeSet(ChangeLogParameters changeLogParameters, BufferedReader reader, StringBuilder currentSequence, StringBuilder currentRollbackSequence, ChangeSet changeSet, int count, String line, Matcher commentMatcher) throws ChangeLogParseException, IOException {
protected void configureChangeSet(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, BufferedReader reader, StringBuilder currentSequence, StringBuilder currentRollbackSequence, ChangeSet changeSet, int count, String line, Matcher commentMatcher) throws ChangeLogParseException, IOException {
Matcher altCommentOneDashMatcher = ALT_COMMENT_ONE_CHARACTER_PATTERN.matcher(line);
Matcher altCommentPluralMatcher = ALT_COMMENT_PLURAL_PATTERN.matcher(line);
Matcher rollbackMatcher = ROLLBACK_PATTERN.matcher(line);
Expand All @@ -488,33 +488,33 @@ protected void configureChangeSet(ChangeLogParameters changeLogParameters, Buffe

if (commentMatcher.matches()) {
if (commentMatcher.groupCount() == 0) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--comment <comment>", getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--comment <comment>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}
if (commentMatcher.groupCount() == 1) {
changeSet.setComments(commentMatcher.group(1));
}
} else if (altCommentOneDashMatcher.matches() || altCommentPluralMatcher.matches()) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--comment <comment>", getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--comment <comment>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
} else if (validCheckSumMatcher.matches()) {
if (validCheckSumMatcher.groupCount() == 0) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), String.format("--rollback <rollback %s>", getSequenceName()), getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), String.format("--rollback <rollback %s>", getSequenceName()), getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
} else if (validCheckSumMatcher.groupCount() == 1) {
changeSet.addValidCheckSum(validCheckSumMatcher.group(1));
}
} else if (altValidCheckSumOneDashMatcher.matches()) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--validChecksum <checksum>", getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--validChecksum <checksum>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
} else if (rollbackMatcher.matches()) {
if (rollbackMatcher.groupCount() == 0) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), String.format("--rollback <rollback %s>", getSequenceName()), getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), String.format("--rollback <rollback %s>", getSequenceName()), getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}
currentRollbackSequence.append(rollbackMatcher.group(1)).append(System.lineSeparator());
} else if (altRollbackMatcher.matches()) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), String.format("--rollback <rollback %s>", getSequenceName()), getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), String.format("--rollback <rollback %s>", getSequenceName()), getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
} else if (rollbackMultiLineStartMatcher.matches()) {
if (rollbackMultiLineStartMatcher.groupCount() == 0) {
Expand All @@ -523,13 +523,13 @@ protected void configureChangeSet(ChangeLogParameters changeLogParameters, Buffe
} else if (preconditionsMatcher.matches()) {
handlePreconditionsCase(changeSet, count, preconditionsMatcher);
} else if (altPreconditionsOneDashMatcher.matches()) {
String message = String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--preconditions <onFail>|<onError>|<onUpdate>", getDocumentationLink());
String message = String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--preconditions <onFail>|<onError>|<onUpdate>", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
} else if (preconditionMatcher.matches()) {
handlePreconditionCase(changeLogParameters, changeSet, preconditionMatcher);
} else if (altPreconditionOneDashMatcher.matches()) {
String message =
String.format(EXCEPTION_MESSAGE, count, getSequenceName(), "--precondition-sql-check", getDocumentationLink());
String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(), "--precondition-sql-check", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
} else {
currentSequence.append(line).append(System.lineSeparator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ could.not.release.lock=Could not release lock
unable.to.find.column.in.table=Unable to find column '%s' in table '%s'
file.not.found=File '%s' not found
loaddata.successful=Data loaded from '%s' into table '%s'
formatted.changelog.exception.message=Unexpected formatting at line %d. Formatted %s changelogs require known formats, such as '%s' and others to be recognized and run. Learn all the options at %s
formatted.changelog.exception.message=Unexpected formatting in formatted changelog '%s' at line %d.%nFormatted %s changelogs require known formats, such as '%s' and others to be recognized and run.%nLearn all the options at %s
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ CREATE TABLE ALL_CAPS_TABLE_2 (
assert e != null
assert e instanceof ChangeLogParseException
assert e.getMessage().toLowerCase().contains("--precondition-sql-check")
assert e.getMessage().contains("Unexpected formatting in formatted changelog 'asdf.sql' at line 4.")
}

def parse() throws Exception {
Expand Down Expand Up @@ -462,6 +463,7 @@ CREATE TABLE ALL_CAPS_TABLE_2 (
then:
def e = thrown(ChangeLogParseException)
assert e
assert e.getMessage().contains("Unexpected formatting in formatted changelog 'asdf.sql' at line 3.")
}

def "parse changeset with 'onSqlOutput' precondition set"() throws Exception {
Expand Down

0 comments on commit b3fb003

Please sign in to comment.