Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable property parsing in FormattedSqlChangeParser DAT-4793 #2333

Merged
merged 5 commits into from Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -81,6 +81,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
ChangeSet changeSet = null;
RawSQLChange change = null;
Pattern changeLogPattern = Pattern.compile("\\-\\-\\s*liquibase formatted.*", Pattern.CASE_INSENSITIVE);
Pattern propertyPattern = Pattern.compile("\\s*\\-\\-[\\s]*property\\s+(.*:.*)\\s+(.*:.*).*", Pattern.CASE_INSENSITIVE);
Pattern changeSetPattern = Pattern.compile("\\s*\\-\\-[\\s]*changeset\\s+(\"[^\"]+\"|[^:]+):\\s*(\"[^\"]+\"|\\S+).*", Pattern.CASE_INSENSITIVE);
Pattern rollbackPattern = Pattern.compile("\\s*\\-\\-[\\s]*rollback (.*)", Pattern.CASE_INSENSITIVE);
Pattern preconditionsPattern = Pattern.compile("\\s*\\-\\-[\\s]*preconditions(.*)", Pattern.CASE_INSENSITIVE);
Expand All @@ -93,7 +94,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
Pattern commentPattern = Pattern.compile("\\-\\-[\\s]*comment:? (.*)", Pattern.CASE_INSENSITIVE);
Pattern validCheckSumPattern = Pattern.compile("\\-\\-[\\s]*validCheckSum:? (.*)", Pattern.CASE_INSENSITIVE);
Pattern ignoreLinesPattern = Pattern.compile("\\-\\-[\\s]*ignoreLines:(\\w+)", Pattern.CASE_INSENSITIVE);
Pattern runWithPattern = Pattern.compile(".*runWith:(\\w+).*", Pattern.CASE_INSENSITIVE);
Pattern runWithPattern = Pattern.compile(".*runWith:([\\w\\$\\{\\}]+).*", Pattern.CASE_INSENSITIVE);

Pattern runOnChangePattern = Pattern.compile(".*runOnChange:(\\w+).*", Pattern.CASE_INSENSITIVE);
Pattern runAlwaysPattern = Pattern.compile(".*runAlways:(\\w+).*", Pattern.CASE_INSENSITIVE);
Expand All @@ -114,7 +115,11 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame

String line;
while ((line = reader.readLine()) != null) {

Matcher propertyPatternMatcher = propertyPattern.matcher(line);
if (propertyPatternMatcher.matches()) {
handleProperty(changeLogParameters, changeLog, propertyPatternMatcher);
continue;
}
Matcher changeLogPatterMatcher = changeLogPattern.matcher (line);
if (changeLogPatterMatcher.matches ()) {
Matcher logicalFilePathMatcher = logicalFilePathPattern.matcher (line);
Expand Down Expand Up @@ -202,21 +207,39 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
boolean failOnError = parseBoolean(failOnErrorPatternMatcher, changeSet, true);

String runWith = parseString(runWithMatcher);
if (runWith != null) {
runWith = changeLogParameters.expandExpressions(runWith, changeLog);
}
String endDelimiter = parseString(endDelimiterPatternMatcher);
rollbackEndDelimiter = parseString(rollbackEndDelimiterPatternMatcher);
String context = StringUtil.trimToNull(
StringUtil.trimToEmpty(parseString(contextPatternMatcher)).replaceFirst("^\"", "").replaceFirst("\"$", "") //remove surrounding quotes if they're in there
StringUtil.trimToEmpty(parseString(contextPatternMatcher)).replaceFirst("^\"", "").replaceFirst("\"$", "") //remove surrounding quotes if they're in there
);
if (context != null) {
context = changeLogParameters.expandExpressions(context, changeLog);
}
String labels = parseString(labelsPatternMatcher);
if (labels != null) {
labels = changeLogParameters.expandExpressions(labels, changeLog);
}
String logicalFilePath = parseString(logicalFilePathMatcher);
if ((logicalFilePath == null) || "".equals(logicalFilePath)) {
logicalFilePath = changeLog.getLogicalFilePath ();
logicalFilePath = changeLog.getLogicalFilePath();
}
if (logicalFilePath != null) {
logicalFilePath = changeLogParameters.expandExpressions(logicalFilePath, changeLog);
}
String dbms = parseString(dbmsPatternMatcher);
if (dbms != null) {
dbms = changeLogParameters.expandExpressions(dbms, changeLog);
}


String changeSetId =
changeLogParameters.expandExpressions(StringUtil.stripEnclosingQuotes(changeSetPatternMatcher.group(2)), changeLog);
String changeSetAuthor =
changeLogParameters.expandExpressions(StringUtil.stripEnclosingQuotes(changeSetPatternMatcher.group(1)), changeLog);
changeSet =
new ChangeSet(StringUtil.stripEnclosingQuotes(changeSetPatternMatcher.group(2)), StringUtil.stripEnclosingQuotes(changeSetPatternMatcher.group(1)), runAlways, runOnChange, logicalFilePath, context, dbms, runWith, runInTransaction, changeLog.getObjectQuotingStrategy(), changeLog);
new ChangeSet(changeSetId, changeSetAuthor, runAlways, runOnChange, logicalFilePath, context, dbms, runWith, runInTransaction, changeLog.getObjectQuotingStrategy(), changeLog);
changeSet.setLabels(new Labels(labels));
changeSet.setFailOnError(failOnError);
changeLog.addChangeSet(changeSet);
Expand Down Expand Up @@ -319,7 +342,40 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
return changeLog;
}


private void handleProperty(ChangeLogParameters changeLogParameters, DatabaseChangeLog changeLog, Matcher propertyPatternMatcher) {
String name = null;
String value = null;
String context = null;
String labels = null;
String dbms = null;
boolean global = false;
for (int i = 1; i <= propertyPatternMatcher.groupCount(); i++) {
String temp = propertyPatternMatcher.group(i);
String[] parts = temp.split(":");
String key = parts[0].trim().toLowerCase();
switch (key) {
case "name":
name = parts[1].trim();
break;
case "value":
value = parts[1].trim();
break;
case "context":
context = parts[1].trim();
break;
case "labels":
labels = parts[1].trim();
break;
case "dbms":
dbms = parts[1].trim();
break;
case "global":
global = Boolean.parseBoolean(parts[1].trim());
break;
}
}
changeLogParameters.set(name, value, context, labels, dbms, global, changeLog);
}

private SqlPrecondition parseSqlCheckCondition(String body) throws ChangeLogParseException{
Pattern[] patterns = new Pattern[] {
Expand Down
Expand Up @@ -23,8 +23,14 @@ public class FormattedSqlChangeLogParserTest extends Specification {
private static final String VALID_CHANGELOG = """
--liquibase formatted sql

--changeset nvoxland:1
select * from table1;
--property name:idProp value:1
--property name:authorProp value:nvoxland
--property nAmE:tableNameProp value:table1
--property name:runwith value: sqlplus


--changeset \${authorProp}:\${idProp}
select * from \${tableNameProp};

--changeset "n voxland":"change 2" (stripComments:false splitStatements:false endDelimiter:X runOnChange:true runAlways:true context:y dbms:mysql runInTransaction:false failOnError:false)
create table table1 (
Expand Down Expand Up @@ -78,6 +84,12 @@ create table my_table (

--changeset complexContext:1 context:"a or b"
select 1

-- changeset wesley:wesley-1 runWith:\${runWith}
create table table2 (
id int primary key
);

""".trim()


Expand Down Expand Up @@ -109,7 +121,7 @@ select 1

changeLog.getLogicalFilePath() == "asdf.sql"

changeLog.getChangeSets().size() == 10
changeLog.getChangeSets().size() == 11

changeLog.getChangeSets().get(0).getAuthor() == "nvoxland"
changeLog.getChangeSets().get(0).getId() == "1"
Expand Down Expand Up @@ -195,6 +207,7 @@ select 1
assert changeLog.getChangeSets().get(7).getContexts().toString().contains("second")
assert changeLog.getChangeSets().get(7).getContexts().toString().contains("third")

changeLog.getChangeSets().get(10).getRunWith() == "sqlplus"

ChangeSet cs = changeLog.getChangeSets().get(8)
cs.getAuthor() == "bboisvert"
Expand Down