From 9377b9a9e415f4e667043be1c731a7b3ed7e18a8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 18 Sep 2019 20:15:46 +0100 Subject: [PATCH] Support -- and # by default as Quartz datasource init comment prefixes Closes gh-17435 --- .../quartz/QuartzDataSourceInitializer.java | 2 +- .../quartz/QuartzProperties.java | 11 +++++--- ...itional-spring-configuration-metadata.json | 7 +++++ .../QuartzDataSourceInitializerTests.java | 26 +++++++++++++++---- .../quartz/tables_#_comments.sql | 10 +++++++ .../quartz/tables_--_comments.sql | 10 +++++++ 6 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_#_comments.sql create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_--_comments.sql diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java index 8d62e7b8b6..5f94f84d62 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java @@ -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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java index 15bc597978..3509ddbb3f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java @@ -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 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 getCommentPrefix() { return this.commentPrefix; } - public void setCommentPrefix(String commentPrefix) { + public void setCommentPrefix(List commentPrefix) { this.commentPrefix = commentPrefix; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index a15900f3a7..80a79c8dbc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -743,6 +743,13 @@ "level" : "error" } }, + { + "name": "spring.quartz.jdbc.comment-prefix", + "defaultValue": [ + "#", + "--" + ] + }, { "name": "spring.quartz.jdbc.initialize-schema", "defaultValue": "embedded" diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java index ca1e237ea9..cd8a7a3411 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java @@ -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) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_#_comments.sql b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_#_comments.sql new file mode 100644 index 0000000000..1da490a9f3 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_#_comments.sql @@ -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; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_--_comments.sql b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_--_comments.sql new file mode 100644 index 0000000000..31ddbad7ce --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/quartz/tables_--_comments.sql @@ -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;