Defer SQL initialization to fit with JPA better
Added 2 new spring.datasource.* properties ("data" like
"schema", and "deferDdl" like the "spring.jpa.hibernate.*"
flag). The SQL scripts are then run separately and the "data"
ones are triggered by a new DataSourceInitializedEvent,
which is also published by the Hibernate DDL schema export.
Fixes gh-1006
This commit is contained in:
@@ -30,6 +30,7 @@ import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceInitialization;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
|
||||
@@ -152,6 +153,8 @@ public abstract class AbstractJpaAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void usesManuallyDefinedEntityManagerFactoryBeanIfAvailable() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.datasource.initialize:false");
|
||||
setupTestConfiguration(TestConfigurationWithEntityManagerFactory.class);
|
||||
this.context.refresh();
|
||||
LocalContainerEntityManagerFactoryBean factoryBean = this.context
|
||||
@@ -188,6 +191,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
|
||||
|
||||
protected void setupTestConfiguration(Class<?> configClass) {
|
||||
this.context.register(configClass, EmbeddedDataSourceConfiguration.class,
|
||||
DataSourceInitialization.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, getAutoConfigureClass());
|
||||
}
|
||||
|
||||
|
||||
@@ -16,12 +16,16 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.orm.jpa;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
@@ -38,6 +42,30 @@ public class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigura
|
||||
return HibernateJpaAutoConfiguration.class;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataScriptWithDdlAuto() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.datasource.data:classpath:/city.sql",
|
||||
"spring.datasource.schema:classpath:/ddl.sql");
|
||||
setupTestConfiguration();
|
||||
this.context.refresh();
|
||||
assertEquals(new Integer(1),
|
||||
new JdbcTemplate(this.context.getBean(DataSource.class)).queryForObject(
|
||||
"SELECT COUNT(*) from CITY", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataScriptWithDeferredDdl() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.datasource.data:classpath:/city.sql",
|
||||
"spring.datasource.deferDdl:true");
|
||||
setupTestConfiguration();
|
||||
this.context.refresh();
|
||||
assertEquals(new Integer(1),
|
||||
new JdbcTemplate(this.context.getBean(DataSource.class)).queryForObject(
|
||||
"SELECT COUNT(*) from CITY", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomNamingStrategy() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
|
||||
1
spring-boot-autoconfigure/src/test/resources/city.sql
Normal file
1
spring-boot-autoconfigure/src/test/resources/city.sql
Normal file
@@ -0,0 +1 @@
|
||||
INSERT INTO CITY (NAME, STATE, COUNTRY, MAP) values ('Washington', 'DC', 'US', 'Google');
|
||||
Reference in New Issue
Block a user