From dfb660aa8737e592f578b3ddc4809dfcda86ae89 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 7 Oct 2013 08:14:14 -0400 Subject: [PATCH] Add schema.sql,data.sql to default SQL initializers ...for compatibility with Spring JDBC. Users can still optionally specify spring.database.schema, but the default location is schema-${spring.database.platform}.sql, schema.sql, data.sql. [Fixes #58332710] --- .../jdbc/DataSourceAutoConfiguration.java | 3 +- .../DataSourceAutoConfigurationTests.java | 37 ++++++++++++++++++- .../boot/autoconfigure/jdbc/another.sql | 4 ++ .../src/test/resources/schema.sql | 4 ++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/jdbc/another.sql create mode 100644 spring-boot-autoconfigure/src/test/resources/schema.sql diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java index 4d28bf8b15..6d717347cf 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java @@ -92,7 +92,8 @@ public class DataSourceAutoConfiguration implements EnvironmentAware { String schema = this.environment.getProperty("schema"); if (schema == null) { schema = "classpath*:schema-" - + this.environment.getProperty("platform", "all") + ".sql"; + + this.environment.getProperty("platform", "all") + + ".sql,classpath*:schema.sql,classpath*:data.sql"; } List resources = new ArrayList(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java index 51c0d43883..dd3e885558 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java @@ -24,7 +24,6 @@ import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -86,6 +85,19 @@ public class DataSourceAutoConfigurationTests { @Test public void testDataSourceInitialized() throws Exception { + this.context.register(DataSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + DataSource dataSource = this.context.getBean(DataSource.class); + assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); + assertNotNull(dataSource); + JdbcOperations template = new JdbcTemplate(dataSource); + assertEquals(new Integer(0), + template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); + } + + @Test + public void testDataSourceInitializedWithExplicitScript() throws Exception { this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Map map = new HashMap(); @@ -102,6 +114,29 @@ public class DataSourceAutoConfigurationTests { template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); } + @Test + public void testDataSourceInitializedWithMultipleScripts() throws Exception { + this.context.register(DataSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + Map map = new HashMap(); + map.put("spring.database.schema", + ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql") + + "," + + ClassUtils.addResourcePathToPackagePath(getClass(), + "another.sql")); + this.context.getEnvironment().getPropertySources() + .addFirst(new MapPropertySource("test", map)); + this.context.refresh(); + DataSource dataSource = this.context.getBean(DataSource.class); + assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); + assertNotNull(dataSource); + JdbcOperations template = new JdbcTemplate(dataSource); + assertEquals(new Integer(0), + template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); + assertEquals(new Integer(0), + template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)); + } + @Configuration static class TestDataSourceConfiguration { diff --git a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/jdbc/another.sql b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/jdbc/another.sql new file mode 100644 index 0000000000..b96a8cbd70 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/jdbc/another.sql @@ -0,0 +1,4 @@ +CREATE TABLE SPAM ( + id INTEGER IDENTITY PRIMARY KEY, + name VARCHAR(30), +); \ No newline at end of file diff --git a/spring-boot-autoconfigure/src/test/resources/schema.sql b/spring-boot-autoconfigure/src/test/resources/schema.sql new file mode 100644 index 0000000000..fdf0368762 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/schema.sql @@ -0,0 +1,4 @@ +CREATE TABLE BAR ( + id INTEGER IDENTITY PRIMARY KEY, + name VARCHAR(30), +); \ No newline at end of file