Support -- and # by default as Quartz datasource init comment prefixes
Closes gh-17435
This commit is contained in:
@@ -43,7 +43,7 @@ public class QuartzDataSourceInitializer extends AbstractDataSourceInitializer {
|
||||
|
||||
@Override
|
||||
protected void customize(ResourceDatabasePopulator populator) {
|
||||
populator.setCommentPrefix(this.properties.getJdbc().getCommentPrefix());
|
||||
populator.setCommentPrefixes(this.properties.getJdbc().getCommentPrefix().toArray(new String[0]));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
package org.springframework.boot.autoconfigure.quartz;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@@ -144,9 +147,9 @@ public class QuartzProperties {
|
||||
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() {
|
||||
return this.schema;
|
||||
@@ -164,11 +167,11 @@ public class QuartzProperties {
|
||||
this.initializeSchema = initializeSchema;
|
||||
}
|
||||
|
||||
public String getCommentPrefix() {
|
||||
public List<String> getCommentPrefix() {
|
||||
return this.commentPrefix;
|
||||
}
|
||||
|
||||
public void setCommentPrefix(String commentPrefix) {
|
||||
public void setCommentPrefix(List<String> commentPrefix) {
|
||||
this.commentPrefix = commentPrefix;
|
||||
}
|
||||
|
||||
|
||||
@@ -743,6 +743,13 @@
|
||||
"level" : "error"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.quartz.jdbc.comment-prefix",
|
||||
"defaultValue": [
|
||||
"#",
|
||||
"--"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "spring.quartz.jdbc.initialize-schema",
|
||||
"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.JdbcTemplateAutoConfiguration;
|
||||
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.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -47,16 +48,31 @@ class QuartzDataSourceInitializerTests {
|
||||
.withPropertyValues("spring.datasource.url=" + String.format(
|
||||
"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
|
||||
void commentPrefixCanBeCustomized() {
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues(
|
||||
"spring.quartz.jdbc.comment-prefix=##",
|
||||
"spring.quartz.jdbc.schema=classpath:org/springframework/boot/autoconfigure/quartz/tables_@@platform@@.sql")
|
||||
.run((context) -> {
|
||||
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
|
||||
assertThat(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM QRTZ_TEST_TABLE", Integer.class))
|
||||
.isEqualTo(0);
|
||||
});
|
||||
.run(this::assertThatDataSourceHasBeenInitialized);
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user