Support -- and # by default as Quartz datasource init comment prefixes

Closes gh-17435
pull/18281/head
Andy Wilkinson 5 years ago
parent bf56b24ca5
commit 9377b9a9e4

@ -43,7 +43,7 @@ public class QuartzDataSourceInitializer extends AbstractDataSourceInitializer {
@Override @Override
protected void customize(ResourceDatabasePopulator populator) { protected void customize(ResourceDatabasePopulator populator) {
populator.setCommentPrefix(this.properties.getJdbc().getCommentPrefix()); populator.setCommentPrefixes(this.properties.getJdbc().getCommentPrefix().toArray(new String[0]));
} }
@Override @Override

@ -17,7 +17,10 @@
package org.springframework.boot.autoconfigure.quartz; package org.springframework.boot.autoconfigure.quartz;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@ -144,9 +147,9 @@ public class QuartzProperties {
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED; private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
/** /**
* Prefix for single-line comments in SQL initialization scripts. * Prefixes for single-line comments in SQL initialization scripts.
*/ */
private String commentPrefix = "--"; private List<String> commentPrefix = new ArrayList<>(Arrays.asList("#", "--"));
public String getSchema() { public String getSchema() {
return this.schema; return this.schema;
@ -164,11 +167,11 @@ public class QuartzProperties {
this.initializeSchema = initializeSchema; this.initializeSchema = initializeSchema;
} }
public String getCommentPrefix() { public List<String> getCommentPrefix() {
return this.commentPrefix; return this.commentPrefix;
} }
public void setCommentPrefix(String commentPrefix) { public void setCommentPrefix(List<String> commentPrefix) {
this.commentPrefix = commentPrefix; this.commentPrefix = commentPrefix;
} }

@ -743,6 +743,13 @@
"level" : "error" "level" : "error"
} }
}, },
{
"name": "spring.quartz.jdbc.comment-prefix",
"defaultValue": [
"#",
"--"
]
},
{ {
"name": "spring.quartz.jdbc.initialize-schema", "name": "spring.quartz.jdbc.initialize-schema",
"defaultValue": "embedded" "defaultValue": "embedded"

@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -47,16 +48,31 @@ class QuartzDataSourceInitializerTests {
.withPropertyValues("spring.datasource.url=" + String.format( .withPropertyValues("spring.datasource.url=" + String.format(
"jdbc:h2:mem:test-%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", UUID.randomUUID().toString())); "jdbc:h2:mem:test-%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", UUID.randomUUID().toString()));
@Test
void hashIsUsedAsACommentPrefixByDefault() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues(
"spring.quartz.jdbc.schema=classpath:org/springframework/boot/autoconfigure/quartz/tables_#_comments.sql")
.run(this::assertThatDataSourceHasBeenInitialized);
}
@Test
void doubleDashIsUsedAsACommentPrefixByDefault() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues(
"spring.quartz.jdbc.schema=classpath:org/springframework/boot/autoconfigure/quartz/tables_--_comments.sql")
.run(this::assertThatDataSourceHasBeenInitialized);
}
@Test @Test
void commentPrefixCanBeCustomized() { void commentPrefixCanBeCustomized() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues( this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues(
"spring.quartz.jdbc.comment-prefix=##", "spring.quartz.jdbc.comment-prefix=##",
"spring.quartz.jdbc.schema=classpath:org/springframework/boot/autoconfigure/quartz/tables_@@platform@@.sql") "spring.quartz.jdbc.schema=classpath:org/springframework/boot/autoconfigure/quartz/tables_@@platform@@.sql")
.run((context) -> { .run(this::assertThatDataSourceHasBeenInitialized);
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class); }
assertThat(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM QRTZ_TEST_TABLE", Integer.class))
.isEqualTo(0); private void assertThatDataSourceHasBeenInitialized(AssertableApplicationContext context) {
}); JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM QRTZ_TEST_TABLE", Integer.class)).isEqualTo(0);
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

@ -0,0 +1,10 @@
# This is a test script to check # is treated as a comment prefix by default
CREATE TABLE QRTZ_TEST_TABLE (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR (200) NOT NULL
);
# Another comment
COMMIT;

@ -0,0 +1,10 @@
-- This is a test script to check -- is treated as a comment prefix by default
CREATE TABLE QRTZ_TEST_TABLE (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR (200) NOT NULL
);
-- Another comment
COMMIT;
Loading…
Cancel
Save