Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DbUpdaterEngine implements DbUpdater {

private static final String GROOVY_EXTENSION = "groovy";
protected static final String UPGRADE_GROOVY_EXTENSION = "upgrade.groovy";

protected static final Pattern END_LINE_REGEX = Pattern.compile("\\r?\\n");
protected static final Pattern RESTAPI_REGEX = Pattern.compile("^\\d+-restapi.*$");

protected static final List<Pattern> EXCLUDED_ADDONS = ImmutableList.of(RESTAPI_REGEX);
Expand Down Expand Up @@ -361,8 +361,28 @@ protected void markScript(String name, boolean init) {
}
}

protected String deleteComments(String sql) {
if (StringUtils.isEmpty(sql)) {
return sql;
}
StringBuilder stringBuilder = new StringBuilder();
String[] lines = END_LINE_REGEX.split(sql);
for (String line : lines) {
int commentStart = line.indexOf(SQL_COMMENT_PREFIX);
line = commentStart == -1 ? line.trim() : line.substring(0, commentStart).trim();
if (!StringUtils.isBlank(line)) {
stringBuilder.append(line)
.append(System.lineSeparator());
}
}
return stringBuilder.toString();
}

protected boolean isEmpty(String sql) {
String[] lines = sql.split("\\r?\\n");
if (StringUtils.isEmpty(sql)) {
return true;
}
String[] lines = END_LINE_REGEX.split(sql);
for (String line : lines) {
line = line.trim();
if (!line.startsWith(SQL_COMMENT_PREFIX) && !StringUtils.isBlank(line)) {
Expand All @@ -379,6 +399,7 @@ protected boolean executeSqlScript(ScriptResource file) {
} catch (IOException e) {
throw new RuntimeException(ERROR + "Error resolving SQL script " + file.name, e);
}
script = deleteComments(script);
ScriptSplitter splitter = new ScriptSplitter(SQL_DELIMITER);
for (String sql : splitter.split(script)) {
if (!isEmpty(sql)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,21 @@ public void testGetScriptName() throws Exception {
scriptName = engine.getScriptName(script.getAbsolutePath());
assertEquals("10-cuba/init/postgres/create-db.sql", scriptName);
}

@Test
void deleteComments() {
String sql = "first;^\n" +
"second; -- comment^\n" +
" -- third;";

DbUpdaterEngine engine = new DbUpdaterEngine();
sql = engine.deleteComments(sql);
assertEquals("first;^\nsecond;\n", sql);

sql = "first;^\n" +
"-- comment^\n" +
"second;";
sql = engine.deleteComments(sql);
assertEquals("first;^\nsecond;\n", sql);
}
}