Skip to content

Commit 8f53c2e

Browse files
committed
Support for custom comment prefix
Some Quartz initialization scripts have comments in a different format. This commit introduces a `comment-prefix` property that should be set by the user if their target database has a script that contains those unusual comments. Closes gh-13041
1 parent 3dd2f5b commit 8f53c2e

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
2222
import org.springframework.boot.jdbc.DataSourceInitializationMode;
2323
import org.springframework.core.io.ResourceLoader;
24+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2425
import org.springframework.util.Assert;
2526

2627
/**
@@ -40,6 +41,11 @@ public QuartzDataSourceInitializer(DataSource dataSource,
4041
this.properties = properties;
4142
}
4243

44+
@Override
45+
protected void customize(ResourceDatabasePopulator populator) {
46+
populator.setCommentPrefix(this.properties.getJdbc().getCommentPrefix());
47+
}
48+
4349
@Override
4450
protected DataSourceInitializationMode getMode() {
4551
return this.properties.getJdbc().getInitializeSchema();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,6 +75,11 @@ public static class Jdbc {
7575
*/
7676
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
7777

78+
/**
79+
* Prefix for single-line comments in SQL initialization scripts.
80+
*/
81+
private String commentPrefix = "--";
82+
7883
public String getSchema() {
7984
return this.schema;
8085
}
@@ -91,6 +96,14 @@ public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
9196
this.initializeSchema = initializeSchema;
9297
}
9398

99+
public String getCommentPrefix() {
100+
return this.commentPrefix;
101+
}
102+
103+
public void setCommentPrefix(String commentPrefix) {
104+
this.commentPrefix = commentPrefix;
105+
}
106+
94107
}
95108

96109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.quartz;
18+
19+
import java.util.UUID;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.junit.Test;
24+
25+
import org.springframework.boot.autoconfigure.AutoConfigurations;
26+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
28+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
29+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.core.io.ResourceLoader;
33+
import org.springframework.jdbc.core.JdbcTemplate;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Tests for {@link QuartzDataSourceInitializer}.
39+
*
40+
* @author Stephane Nicoll
41+
*/
42+
public class QuartzDataSourceInitializerTests {
43+
44+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
45+
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
46+
JdbcTemplateAutoConfiguration.class))
47+
.withPropertyValues("spring.datasource.url=" + String.format(
48+
"jdbc:h2:mem:test-%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
49+
UUID.randomUUID().toString()));
50+
51+
@Test
52+
public void commentPrefixCanBeCustomized() {
53+
this.contextRunner.withUserConfiguration(TestConfiguration.class)
54+
.withPropertyValues(
55+
"spring.quartz.jdbc.comment-prefix=##",
56+
"spring.quartz.jdbc.schema=classpath:org/springframework/boot/autoconfigure/quartz/tables_@@platform@@.sql")
57+
.run((context) -> {
58+
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
59+
assertThat(jdbcTemplate.queryForObject(
60+
"SELECT COUNT(*) FROM QRTZ_TEST_TABLE", Integer.class))
61+
.isEqualTo(0);
62+
});
63+
}
64+
65+
@Configuration
66+
@EnableConfigurationProperties(QuartzProperties.class)
67+
static class TestConfiguration {
68+
69+
@Bean
70+
public QuartzDataSourceInitializer initializer(DataSource dataSource,
71+
ResourceLoader resourceLoader, QuartzProperties properties) {
72+
return new QuartzDataSourceInitializer(dataSource, resourceLoader, properties);
73+
}
74+
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## This is a test script to check custom comment prefix is taken into account
2+
3+
CREATE TABLE QRTZ_TEST_TABLE (
4+
SCHED_NAME VARCHAR(120) NOT NULL,
5+
CALENDAR_NAME VARCHAR (200) NOT NULL
6+
);
7+
8+
## Another comment
9+
10+
COMMIT;

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ content into your application. Rather, pick only the properties that you need.
140140
spring.profiles.include= # Unconditionally activate the specified comma-separated list of profiles (or list of profiles if using YAML).
141141
142142
# QUARTZ SCHEDULER ({sc-spring-boot-autoconfigure}/quartz/QuartzProperties.{sc-ext}[QuartzProperties])
143+
spring.quartz.jdbc.comment-prefix=-- # Prefix for single-line comments in SQL initialization scripts.
143144
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
144145
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
145146
spring.quartz.job-store-type=memory # Quartz job store type.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/AbstractDataSourceInitializer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,6 +62,7 @@ protected void initialize() {
6262
}
6363
populator.addScript(this.resourceLoader.getResource(schemaLocation));
6464
populator.setContinueOnError(true);
65+
customize(populator);
6566
DatabasePopulatorUtils.execute(populator, this.dataSource);
6667
}
6768

@@ -76,6 +77,13 @@ private boolean isEnabled() {
7677
return true;
7778
}
7879

80+
/**
81+
* Customize the {@link ResourceDatabasePopulator}.
82+
* @param populator the configured database populator
83+
*/
84+
protected void customize(ResourceDatabasePopulator populator) {
85+
}
86+
7987
protected abstract DataSourceInitializationMode getMode();
8088

8189
protected abstract String getSchemaLocation();

0 commit comments

Comments
 (0)