strip comments from sql before splitting ; (to avoid ; in comments)

This commit is contained in:
Ken Stevens 2019-12-11 14:02:24 -05:00
parent f5c4c8e3e3
commit 7c1544d8f3
3 changed files with 16 additions and 8 deletions

View File

@ -30,7 +30,6 @@ import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -68,13 +67,6 @@ public abstract class BaseFlywayMigrateDatabaseCommand<T extends Enum> extends B
return MIGRATE_DATABASE;
}
@Override
public List<String> provideUsageNotes() {
String versions = "The following versions are supported: " +
provideAllowedVersions().stream().map(Enum::name).collect(Collectors.joining(", "));
return Collections.singletonList(versions);
}
@Override
public Options getOptions() {
Options retVal = new Options();

View File

@ -38,6 +38,7 @@ import java.util.regex.Pattern;
import static org.apache.commons.lang3.StringUtils.isBlank;
public class SchemaInitializationProvider implements ISchemaInitializationProvider {
private static final Pattern ourSqlCommentPattern = Pattern.compile("(?m)^\\s*--.*$");
private final String mySchemaFileClassPath;
private final String mySchemaExistsIndicatorTable;
@ -76,6 +77,10 @@ public class SchemaInitializationProvider implements ISchemaInitializationProvid
return retval;
}
static String stripComments(String theSqlString) {
return ourSqlCommentPattern.matcher(theSqlString).replaceAll("");
}
private static final Pattern ourTrailingCommaPattern = Pattern.compile(",(\\s+)\\)");
static String clean(String theStatement) {

View File

@ -11,4 +11,15 @@ public class SchemaInitializationProviderTest {
assertEquals("foo\n )", SchemaInitializationProvider.clean("foo,\n )"));
assertEquals("foo,bar\n )", SchemaInitializationProvider.clean("foo,bar\n )"));
}
@Test
public void testStripComments() {
String input = "no comment\n" +
" --spacecomment \n" +
"so like definitely no comment\n" +
"-- nospace comment\n";
String expectedOutput = "no comment\n\n" +
"so like definitely no comment\n\n";
assertEquals(expectedOutput, SchemaInitializationProvider.stripComments(input));
}
}