Add default-label property to Jdbc Repository (#2176)
This commit is contained in:
@@ -51,6 +51,8 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
|
||||
*/
|
||||
private boolean failOnError = true;
|
||||
|
||||
private String defaultLabel = "master";
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
@@ -97,4 +99,12 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
|
||||
return !this.sql.equals(DEFAULT_SQL) && this.sqlWithoutProfile.equals(DEFAULT_SQL_WITHOUT_PROFILE);
|
||||
}
|
||||
|
||||
public String getDefaultLabel() {
|
||||
return this.defaultLabel;
|
||||
}
|
||||
|
||||
public void setDefaultLabel(String defaultLabel) {
|
||||
this.defaultLabel = defaultLabel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
|
||||
private boolean configIncomplete;
|
||||
|
||||
private String defaultLabel;
|
||||
|
||||
@Deprecated
|
||||
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {
|
||||
this(jdbc, properties, new PropertiesResultSetExtractor());
|
||||
@@ -83,6 +85,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
this.failOnError = properties.isFailOnError();
|
||||
this.extractor = extractor;
|
||||
this.configIncomplete = properties.isConfigIncomplete();
|
||||
this.defaultLabel = properties.getDefaultLabel();
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
@@ -97,7 +100,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
public Environment findOne(String application, String profile, String label) {
|
||||
String config = application;
|
||||
if (StringUtils.isEmpty(label)) {
|
||||
label = "master";
|
||||
label = this.defaultLabel;
|
||||
}
|
||||
if (!StringUtils.hasText(profile)) {
|
||||
profile = "default";
|
||||
|
||||
@@ -207,6 +207,22 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
assertThatThrownBy(() -> repository.findOne("foo", "bar", "")).isInstanceOf(DataAccessException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomLabel() {
|
||||
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
|
||||
properties.setDefaultLabel("main");
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
|
||||
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "bar", "");
|
||||
assertThat(env.getName()).isEqualTo("foo");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "bar" });
|
||||
assertThat(env.getLabel()).isEqualTo("main");
|
||||
assertThat(env.getPropertySources()).isNotEmpty();
|
||||
assertThat(env.getPropertySources().get(0).getName()).isEqualTo("foo-bar");
|
||||
assertThat(env.getPropertySources().get(0).getSource().get("a.b.c")).isEqualTo("foo-bar");
|
||||
assertThat(env.getPropertySources().get(1).getName()).isEqualTo("application-bar");
|
||||
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("application-bar");
|
||||
}
|
||||
|
||||
@ImportAutoConfiguration(SqlInitializationAutoConfiguration.class)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
protected static class ApplicationConfiguration {
|
||||
|
||||
@@ -10,4 +10,7 @@ INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values
|
||||
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', null, 'master', 'a.b.c', 'foo-null');
|
||||
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', 'bar', 'master', 'a.b.c', 'application-bar');
|
||||
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', 'default', 'master', 'a.b.c', 'application-default');
|
||||
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', null, 'master', 'a.b.c', 'application-null');
|
||||
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', null, 'master', 'a.b.c', 'application-null');
|
||||
|
||||
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('foo', 'bar', 'main', 'a.b.c', 'foo-bar');
|
||||
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('application', 'bar', 'main', 'a.b.c', 'application-bar');
|
||||
|
||||
Reference in New Issue
Block a user