Skip to content

Commit

Permalink
DAT-16494 Revert "Implemented ability for formatted SQL changelog to …
Browse files Browse the repository at this point in the history
…have rollback scripts DAT-16019" (#5385)

Revert "Implemented ability for formatted SQL changelog to have rollback scripts  DAT-16019 (#5312)"

This reverts commit 541aeea.

Co-authored-by: rberezen <ruslan.berezenskyi@gmail.com>
Co-authored-by: suryaaki2 <80348493+suryaaki2@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 14, 2023
1 parent e605cee commit 73cb2e1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -51,9 +50,6 @@ public abstract class AbstractFormattedChangeLogParser implements ChangeLogParse
protected final String ALT_CHANGE_SET_NO_OTHER_INFO_REGEX = String.format("\\s*%s[\\s]*changeset[\\s]*.*$", getSingleLineCommentSequence());
protected final Pattern ALT_CHANGE_SET_NO_OTHER_INFO_PATTERN = Pattern.compile(ALT_CHANGE_SET_NO_OTHER_INFO_REGEX, Pattern.CASE_INSENSITIVE);

protected final String ROLLBACK_SQL_FILE_REGEX = String.format("\\s*%s[\\s]*rollbackSqlFile[\\s]+(.*)", getSingleLineCommentSequence());
protected final Pattern ROLLBACK_SQL_FILE_PATTERN = Pattern.compile(ROLLBACK_SQL_FILE_REGEX, Pattern.CASE_INSENSITIVE);

protected final String ROLLBACK_REGEX = String.format("\\s*%s[\\s]*rollback (.*)", getSingleLineCommentSequence());
protected final Pattern ROLLBACK_PATTERN = Pattern.compile(ROLLBACK_REGEX, Pattern.CASE_INSENSITIVE);

Expand Down Expand Up @@ -135,12 +131,6 @@ public abstract class AbstractFormattedChangeLogParser implements ChangeLogParse
protected static final String LABELS_REGEX = ".*labels:(\".*?\"|\\S*).*";
protected final Pattern LABELS_PATTERN = Pattern.compile(LABELS_REGEX, Pattern.CASE_INSENSITIVE);

protected static final String PATH_REGEX = ".*path:(\".*?\"|\\S*).*";
protected final Pattern PATH_PATTERN = Pattern.compile(PATH_REGEX, Pattern.CASE_INSENSITIVE);

protected static final String RELATIVE_TO_CHANGELOG_REGEX = ".*relativeToChangelog:(\".*?\"|\\S*).*";
protected final Pattern RELATIVE_TO_CHANGELOG_PATTERN = Pattern.compile(RELATIVE_TO_CHANGELOG_REGEX, Pattern.CASE_INSENSITIVE);

protected static final String RUN_IN_TRANSACTION_REGEX = ".*runInTransaction:(\\w+).*";
protected final Pattern RUN_IN_TRANSACTION_PATTERN = Pattern.compile(RUN_IN_TRANSACTION_REGEX, Pattern.CASE_INSENSITIVE);

Expand Down Expand Up @@ -466,7 +456,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
throw new ChangeLogParseException("\n" + message);
}
if (changeSet != null) {
configureChangeSet(physicalChangeLogLocation, changeLogParameters, reader, currentSequence, currentRollbackSequence, changeSet, count, line, commentMatcher, resourceAccessor);
configureChangeSet(physicalChangeLogLocation, changeLogParameters, reader, currentSequence, currentRollbackSequence, changeSet, count, line, commentMatcher);
} else {
if (commentMatcher.matches()) {
String message =
Expand Down Expand Up @@ -495,11 +485,6 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
}

protected void configureChangeSet(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, BufferedReader reader, StringBuilder currentSequence, StringBuilder currentRollbackSequence, ChangeSet changeSet, int count, String line, Matcher commentMatcher) throws ChangeLogParseException, IOException {
configureChangeSet(physicalChangeLogLocation, changeLogParameters, reader, currentSequence, currentRollbackSequence, changeSet, count, line, commentMatcher, null);
}

protected void configureChangeSet(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, BufferedReader reader, StringBuilder currentSequence, StringBuilder currentRollbackSequence, ChangeSet changeSet, int count, String line, Matcher commentMatcher, ResourceAccessor resourceAccessor)
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 @@ -511,7 +496,6 @@ protected void configureChangeSet(String physicalChangeLogLocation, ChangeLogPar
Matcher validCheckSumMatcher = VALID_CHECK_SUM_PATTERN.matcher(line);
Matcher altValidCheckSumOneDashMatcher = ALT_VALID_CHECK_SUM_ONE_CHARACTER_PATTERN.matcher(line);
Matcher rollbackMultiLineStartMatcher = ROLLBACK_MULTI_LINE_START_PATTERN.matcher(line);
Matcher rollbackSqlFileMatcher = ROLLBACK_SQL_FILE_PATTERN.matcher(line);

if (commentMatcher.matches()) {
if (commentMatcher.groupCount() == 0) {
Expand Down Expand Up @@ -547,24 +531,6 @@ protected void configureChangeSet(String physicalChangeLogLocation, ChangeLogPar
if (rollbackMultiLineStartMatcher.groupCount() == 0) {
currentRollbackSequence.append(extractMultiLineRollBack(reader));
}
} else if (rollbackSqlFileMatcher.matches()) {
if (resourceAccessor != null) {
String rollbackSqlAttributes = rollbackSqlFileMatcher.group(1);
Matcher m = PATH_PATTERN.matcher(rollbackSqlAttributes);
if (! m.matches()) {
String message =
String.format(EXCEPTION_MESSAGE, physicalChangeLogLocation, count, getSequenceName(),
"--rollbackSqlFile path:<SQL file path> [relativeToChangelog:true|false]", getDocumentationLink());
throw new ChangeLogParseException("\n" + message);
}
String path = DatabaseChangeLog.normalizePath(m.group(1));
boolean isRelativeToChangelog = false;
m = RELATIVE_TO_CHANGELOG_PATTERN.matcher(rollbackSqlAttributes);
if (m.matches()) {
isRelativeToChangelog = Boolean.parseBoolean(m.group(1));
}
currentRollbackSequence.append(readRollbackSqlFile(path, resourceAccessor, physicalChangeLogLocation, isRelativeToChangelog));
}
} else if (preconditionsMatcher.matches()) {
handlePreconditionsCase(changeSet, count, preconditionsMatcher);
} else if (altPreconditionsOneDashMatcher.matches()) {
Expand Down Expand Up @@ -617,17 +583,6 @@ protected InputStream openChangeLogFile(String physicalChangeLogLocation, Resour
return resourceAccessor.getExisting(physicalChangeLogLocation).openInputStream();
}

protected String readRollbackSqlFile(String rollbackSpecification, ResourceAccessor resourceAccessor, String physicalChangelogLocation, boolean isRelativeToChangelog)
throws IOException {
if (isRelativeToChangelog) {
rollbackSpecification = resourceAccessor.get(physicalChangelogLocation).resolveSibling(rollbackSpecification).getPath();
rollbackSpecification = DatabaseChangeLog.normalizePath(Paths.get(rollbackSpecification).toString());
}
try (InputStream inputStream = resourceAccessor.get(rollbackSpecification).openInputStream()) {
return StreamUtil.readStreamAsString(inputStream);
}
}

private void handleRollbackSequence(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, DatabaseChangeLog changeLog, StringBuilder currentRollbackSequence, ChangeSet changeSet, Matcher rollbackSplitStatementsPatternMatcher, boolean rollbackSplitStatements, String rollbackEndDelimiter) throws ChangeLogParseException {
String currentRollbackSequenceAsString = currentRollbackSequence.toString();
if (StringUtil.trimToNull(currentRollbackSequenceAsString) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package liquibase.parser.core.formattedsql

import liquibase.Contexts
import liquibase.LabelExpression
import liquibase.Scope
import liquibase.change.core.EmptyChange
import liquibase.change.core.RawSQLChange
import liquibase.changelog.ChangeLogParameters
Expand All @@ -13,8 +12,6 @@ import liquibase.exception.ChangeLogParseException
import liquibase.precondition.core.PreconditionContainer
import liquibase.precondition.core.SqlPrecondition
import liquibase.resource.ResourceAccessor
import liquibase.resource.SearchPathResourceAccessor
import liquibase.resource.SearchPathResourceAccessorTest
import liquibase.servicelocator.LiquibaseService
import liquibase.test.JUnitResourceAccessor
import liquibase.util.StringUtil
Expand Down Expand Up @@ -70,7 +67,6 @@ create table \${tablename} (
);
--rollback drop table \${tablename};
-- changeset mysql:1
-- comment: this is a comment
create table mysql_boo (
Expand Down Expand Up @@ -272,24 +268,6 @@ CREATE TABLE ALL_CAPS_TABLE_2 (
)
"""

private static final String VALID_CHANGELOG_WITH_ROLLBACK_FILE =
"""
--ChangeSet paikens:8
create table singular (
id int primary key
);
--rollbackSqlFile path:dropTableSingular.sql
"""

private static final String VALID_CHANGELOG_WITH_ROLLBACK_FILE_RELATIVE_TO_CHANGELOG =
"""
--ChangeSet paikens:8
create table singular (
id int primary key
);
--rollbackSqlFile path:dropTableSingular.sql relativeToChangelog:true
"""

def supports() throws Exception {
expect:
assert new MockFormattedSqlChangeLogParser(VALID_CHANGELOG).supports("asdf.sql", new JUnitResourceAccessor())
Expand Down Expand Up @@ -462,37 +440,6 @@ CREATE TABLE ALL_CAPS_TABLE_2 (
((RawSQLChange) changeLog.getChangeSets().get(24).getRollback().getChanges().get(0)).getSql().startsWith("create table test_table (")
}

def parseWithRollbackFile() throws Exception {
expect:
ChangeLogParameters params = new ChangeLogParameters()
def resourceAccessor = new SearchPathResourceAccessor(".,target/test-classes")
Scope.child(Scope.Attr.resourceAccessor.name(), resourceAccessor, { ->
DatabaseChangeLog changeLog =
new MockFormattedSqlChangeLogParser(VALID_CHANGELOG_WITH_ROLLBACK_FILE).parse("asdf.sql", params, new JUnitResourceAccessor())
assert changeLog
assert changeLog.getChangeSets().get(0).getRollback().getChanges().size() == 1
def rawSql = (RawSQLChange)changeLog.getChangeSets().get(0).getRollback().getChanges().get(0)
assert rawSql.getSql() == "drop table singular;"
})

}

def parseWithRollbackFileRelative() throws Exception {
expect:
ChangeLogParameters params = new ChangeLogParameters()
def resourceAccessor = new SearchPathResourceAccessor(".,target/test-classes")
Scope.child(Scope.Attr.resourceAccessor.name(), resourceAccessor, { ->
DatabaseChangeLog changeLog =
new MockFormattedSqlChangeLogParser(VALID_CHANGELOG_WITH_ROLLBACK_FILE_RELATIVE_TO_CHANGELOG)
.parse("asdf.sql", params, new JUnitResourceAccessor())
assert changeLog
assert changeLog.getChangeSets().get(0).getRollback().getChanges().size() == 1
def rawSql = (RawSQLChange)changeLog.getChangeSets().get(0).getRollback().getChanges().get(0)
assert rawSql.getSql() == "drop table singular;"
})

}

def parseWithSpaces() throws Exception {
expect:
ChangeLogParameters params = new ChangeLogParameters()
Expand Down

This file was deleted.

0 comments on commit 73cb2e1

Please sign in to comment.