Skip to content

Commit e873c53

Browse files
committed
#13 - Support setting [default] schema via properties
1 parent 64e4a2e commit e873c53

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/main/java/io/ebean/datasource/DataSourceConfig.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class DataSourceConfig {
2222

2323
private String password;
2424

25+
private String schema;
26+
2527
/**
2628
* The name of the database platform (for use with ownerUsername and InitDatabase).
2729
*/
@@ -98,6 +100,7 @@ public DataSourceConfig copy() {
98100
copy.url = url;
99101
copy.username = username;
100102
copy.password = password;
103+
copy.schema = schema;
101104
copy.platform = platform;
102105
copy.ownerUsername = ownerUsername;
103106
copy.ownerPassword = ownerPassword;
@@ -149,6 +152,9 @@ public DataSourceConfig setDefaults(DataSourceConfig other) {
149152
if (password == null) {
150153
password = other.password;
151154
}
155+
if (schema == null) {
156+
schema = other.schema;
157+
}
152158
return this;
153159
}
154160

@@ -207,6 +213,21 @@ public DataSourceConfig setPassword(String password) {
207213
return this;
208214
}
209215

216+
/**
217+
* Return the database username.
218+
*/
219+
public String getSchema() {
220+
return schema;
221+
}
222+
223+
/**
224+
* Set the default database schema to use.
225+
*/
226+
public DataSourceConfig setSchema(String schema) {
227+
this.schema = schema;
228+
return this;
229+
}
230+
210231
/**
211232
* Return the database driver.
212233
*/
@@ -752,6 +773,7 @@ private void loadSettings(ConfigPropertiesHelper properties) {
752773

753774
username = properties.get("username", username);
754775
password = properties.get("password", password);
776+
schema = properties.get("schema", schema);
755777
platform = properties.get("platform", platform);
756778
ownerUsername = properties.get("ownerUsername", ownerUsername);
757779
ownerPassword = properties.get("ownerPassword", ownerPassword);

src/test/java/io/ebean/datasource/DataSourceConfigTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.junit.Test;
44

5+
import java.io.IOException;
56
import java.util.LinkedHashMap;
67
import java.util.Map;
8+
import java.util.Properties;
79

810
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
911
import static org.junit.Assert.assertEquals;
@@ -54,6 +56,7 @@ public void copy() {
5456
source.setUsername("un");
5557
source.setPassword("pw");
5658
source.setUrl("url");
59+
source.setSchema("sch");
5760

5861
Map<String,String> customSource = new LinkedHashMap<>();
5962
customSource.put("a","a");
@@ -65,6 +68,7 @@ public void copy() {
6568
assertEquals("un", copy.getUsername());
6669
assertEquals("pw", copy.getPassword());
6770
assertEquals("url", copy.getUrl());
71+
assertEquals("sch", copy.getSchema());
6872
assertEquals(42, copy.getMinConnections());
6973
assertEquals(45, copy.getMaxConnections());
7074

@@ -88,6 +92,8 @@ public void defaults() {
8892
assertThat(readOnly.getUrl()).isEqualTo(config.getUrl());
8993
assertThat(readOnly.getUsername()).isEqualTo(config.getUsername());
9094
assertThat(readOnly.getPassword()).isEqualTo(config.getPassword());
95+
assertThat(readOnly.getSchema()).isEqualTo(config.getSchema());
96+
9197
}
9298

9399
@Test
@@ -115,4 +121,18 @@ private DataSourceConfig create() {
115121
config.setPassword("bar");
116122
return config;
117123
}
124+
125+
@Test
126+
public void loadSettings() throws IOException {
127+
128+
DataSourceConfig config = new DataSourceConfig();
129+
130+
Properties props = new Properties();
131+
props.load(getClass().getResourceAsStream("/example.properties"));
132+
config.loadSettings(props, "db");
133+
134+
assertThat(config.getUsername()).isEqualTo("myusername");
135+
assertThat(config.getPassword()).isEqualTo("mypassword");
136+
assertThat(config.getSchema()).isEqualTo("myschema");
137+
}
118138
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
datasource.db.username=myusername
2+
datasource.db.password=mypassword
3+
datasource.db.schema=myschema

0 commit comments

Comments
 (0)