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]
This commit is contained in:
Dave Syer 2013-10-07 08:14:14 -04:00
parent 182328697d
commit dfb660aa87
4 changed files with 46 additions and 2 deletions

View File

@ -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<Resource> resources = new ArrayList<Resource>();

View File

@ -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<String, Object> map = new HashMap<String, Object>();
@ -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<String, Object> map = new HashMap<String, Object>();
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 {

View File

@ -0,0 +1,4 @@
CREATE TABLE SPAM (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(30),
);

View File

@ -0,0 +1,4 @@
CREATE TABLE BAR (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(30),
);