Commit dfb660aa authored by Dave Syer's avatar Dave Syer

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]
parent 18232869
......@@ -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>();
......
......@@ -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 {
......
CREATE TABLE SPAM (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(30),
);
\ No newline at end of file
CREATE TABLE BAR (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(30),
);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment