Skip to content

Commit 7d21395

Browse files
committed
Never run data.sql scripts if spring.datasource.initialize is false
Previously, the data scripts were always run in response to the publication of a DataSourceInitializedEvent, irrespective of spring.datasource.initialize. While the event won't be published by DataSourceInitializer if spring.datasource.initialize is false, it will be published if spring.jpa.hibernate.hbm2ddl.auto has been set. This commit updates DataSourceInitializer's handling of DataSourceInitializedEvent to only run the data scripts if spring.datasource.initialize is true. Fixes #1336
1 parent e185793 commit 7d21395

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ private void runSchemaScripts() {
8888

8989
@Override
9090
public void onApplicationEvent(DataSourceInitializedEvent event) {
91+
if (!this.properties.isInitialize()) {
92+
logger.debug("Initialization disabled (not running data scripts)");
93+
return;
94+
}
9195
// NOTE the event can happen more than once and
9296
// the event datasource is not used here
9397
if (!this.initialized) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19+
import java.sql.SQLException;
1920
import java.util.Random;
2021

2122
import javax.sql.DataSource;
@@ -31,13 +32,15 @@
3132
import org.springframework.context.annotation.Bean;
3233
import org.springframework.context.annotation.Configuration;
3334
import org.springframework.context.annotation.Primary;
35+
import org.springframework.jdbc.BadSqlGrammarException;
3436
import org.springframework.jdbc.core.JdbcOperations;
3537
import org.springframework.jdbc.core.JdbcTemplate;
3638
import org.springframework.util.ClassUtils;
3739

3840
import static org.junit.Assert.assertEquals;
3941
import static org.junit.Assert.assertNotNull;
4042
import static org.junit.Assert.assertTrue;
43+
import static org.junit.Assert.fail;
4144

4245
/**
4346
* Tests for {@link DataSourceInitializer}.
@@ -96,48 +99,56 @@ public void testDataSourceInitialized() throws Exception {
9699
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
97100
assertNotNull(dataSource);
98101
JdbcOperations template = new JdbcTemplate(dataSource);
99-
assertEquals(new Integer(0),
102+
assertEquals(new Integer(1),
100103
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class));
101104
}
102105

103106
@Test
104107
public void testDataSourceInitializedWithExplicitScript() throws Exception {
105108
this.context.register(DataSourceAutoConfiguration.class,
106109
PropertyPlaceholderAutoConfiguration.class);
107-
EnvironmentTestUtils.addEnvironment(
108-
this.context,
109-
"spring.datasource.initialize:true",
110-
"spring.datasource.schema:"
111-
+ ClassUtils.addResourcePathToPackagePath(getClass(),
112-
"schema.sql"));
110+
EnvironmentTestUtils
111+
.addEnvironment(
112+
this.context,
113+
"spring.datasource.initialize:true",
114+
"spring.datasource.schema:"
115+
+ ClassUtils.addResourcePathToPackagePath(getClass(),
116+
"schema.sql"),
117+
"spring.datasource.data:"
118+
+ ClassUtils.addResourcePathToPackagePath(getClass(),
119+
"data.sql"));
113120
this.context.refresh();
114121
DataSource dataSource = this.context.getBean(DataSource.class);
115122
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
116123
assertNotNull(dataSource);
117124
JdbcOperations template = new JdbcTemplate(dataSource);
118-
assertEquals(new Integer(0),
125+
assertEquals(new Integer(1),
119126
template.queryForObject("SELECT COUNT(*) from FOO", Integer.class));
120127
}
121128

122129
@Test
123130
public void testDataSourceInitializedWithMultipleScripts() throws Exception {
124-
EnvironmentTestUtils.addEnvironment(
125-
this.context,
126-
"spring.datasource.initialize:true",
127-
"spring.datasource.schema:"
128-
+ ClassUtils.addResourcePathToPackagePath(getClass(),
129-
"schema.sql")
130-
+ ","
131-
+ ClassUtils.addResourcePathToPackagePath(getClass(),
132-
"another.sql"));
131+
EnvironmentTestUtils
132+
.addEnvironment(
133+
this.context,
134+
"spring.datasource.initialize:true",
135+
"spring.datasource.schema:"
136+
+ ClassUtils.addResourcePathToPackagePath(getClass(),
137+
"schema.sql")
138+
+ ","
139+
+ ClassUtils.addResourcePathToPackagePath(getClass(),
140+
"another.sql"),
141+
"spring.datasource.data:"
142+
+ ClassUtils.addResourcePathToPackagePath(getClass(),
143+
"data.sql"));
133144
this.context.register(DataSourceAutoConfiguration.class,
134145
PropertyPlaceholderAutoConfiguration.class);
135146
this.context.refresh();
136147
DataSource dataSource = this.context.getBean(DataSource.class);
137148
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
138149
assertNotNull(dataSource);
139150
JdbcOperations template = new JdbcTemplate(dataSource);
140-
assertEquals(new Integer(0),
151+
assertEquals(new Integer(1),
141152
template.queryForObject("SELECT COUNT(*) from FOO", Integer.class));
142153
assertEquals(new Integer(0),
143154
template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class));
@@ -170,6 +181,31 @@ public void testDataSourceInitializedWithExplicitSqlScriptEncoding() throws Exce
170181
template.queryForObject("SELECT name from BAR WHERE id=2", String.class));
171182
}
172183

184+
@Test
185+
public void testInitializationDisabled() throws Exception {
186+
this.context.register(DataSourceAutoConfiguration.class,
187+
PropertyPlaceholderAutoConfiguration.class);
188+
this.context.refresh();
189+
190+
DataSource dataSource = this.context.getBean(DataSource.class);
191+
192+
this.context.publishEvent(new DataSourceInitializedEvent(dataSource));
193+
194+
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
195+
assertNotNull(dataSource);
196+
JdbcOperations template = new JdbcTemplate(dataSource);
197+
198+
try {
199+
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class);
200+
fail("Query should have failed as BAR table does not exist");
201+
}
202+
catch (BadSqlGrammarException ex) {
203+
SQLException sqlException = ex.getSQLException();
204+
int expectedCode = -5501; // user lacks privilege or object not found
205+
assertEquals(expectedCode, sqlException.getErrorCode());
206+
}
207+
}
208+
173209
@Configuration
174210
@EnableConfigurationProperties
175211
protected static class TwoDataSources {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO BAR VALUES (1, 'Andy');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO FOO VALUES (1, 'Andy');

0 commit comments

Comments
 (0)