Skip to content

Commit

Permalink
Merge branch '3.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Dec 15, 2022
2 parents 5dabc7d + 52b1909 commit 4d6db55
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
/** SQL used to query database for keys and values. */
private String sql = DEFAULT_SQL;

private boolean enableSqlWithoutProfile = false;

/** SQL used to query database for keys and values when profile is null. */
private String sqlWithoutProfile = DEFAULT_SQL_WITHOUT_PROFILE;

Expand Down Expand Up @@ -94,12 +92,9 @@ public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

public boolean isEnableSqlWithoutProfile() {
return enableSqlWithoutProfile;
}

public void setEnableSqlWithoutProfile(boolean enableSqlWithoutProfile) {
this.enableSqlWithoutProfile = enableSqlWithoutProfile;
public boolean isConfigIncomplete() {
// sql and sqlWithoutProfile should be customized at the same time
return !this.sql.equals(DEFAULT_SQL) && this.sqlWithoutProfile.equals(DEFAULT_SQL_WITHOUT_PROFILE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered

private boolean failOnError;

private boolean enableSqlWithoutProfile;
private boolean configIncomplete;

@Deprecated
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {
Expand All @@ -82,7 +82,7 @@ public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties pr
this.sqlWithoutProfile = properties.getSqlWithoutProfile();
this.failOnError = properties.isFailOnError();
this.extractor = extractor;
this.enableSqlWithoutProfile = properties.isEnableSqlWithoutProfile();
this.configIncomplete = properties.isConfigIncomplete();
}

public String getSql() {
Expand All @@ -102,7 +102,8 @@ public Environment findOne(String application, String profile, String label) {
if (!StringUtils.hasText(profile)) {
profile = "default";
}
if (!enableSqlWithoutProfile && !profile.startsWith("default")) {
// fallback to previous logic: always include profile "default"
if (configIncomplete && !profile.startsWith("default")) {
profile = "default," + profile;
}
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
Expand All @@ -120,7 +121,9 @@ public Environment findOne(String application, String profile, String label) {
addPropertySource(environment, app, env, label);
}
// add properties without profile, equivalent to foo.yml, application.yml
addPropertySource(environment, app, null, label);
if (!configIncomplete) {
addPropertySource(environment, app, null, label);
}
}
return environment;
}
Expand All @@ -129,7 +132,7 @@ private void addPropertySource(Environment environment, String application, Stri
try {
Map<String, Object> source;
String name;
if (!enableSqlWithoutProfile || (enableSqlWithoutProfile && profile != null)) {
if (profile != null) {
source = this.jdbc.query(this.sql, this.extractor, application, profile, label);
name = application + "-" + profile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public class JdbcEnvironmentRepositoryTests {
@Test
public void basicProperties() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "bar", "");
assertThat(env.getName()).isEqualTo("foo");
Expand All @@ -75,7 +74,6 @@ public void basicProperties() {
@Test
public void testDefaultProfile() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "", "");
assertThat(env.getName()).isEqualTo("foo");
Expand All @@ -95,7 +93,6 @@ public void testDefaultProfile() {
@Test
public void testProfileNotExist() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "not_exist", "");
assertThat(env.getName()).isEqualTo("foo");
Expand All @@ -111,7 +108,6 @@ public void testProfileNotExist() {
@Test
public void testApplicationNotExist() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("not_exist", "bar", "");
assertThat(env.getName()).isEqualTo("not_exist");
Expand All @@ -127,7 +123,6 @@ public void testApplicationNotExist() {
@Test
public void testApplicationProfileBothNotExist() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("not_exist", "not_exist", "");
assertThat(env.getName()).isEqualTo("not_exist");
Expand All @@ -141,7 +136,6 @@ public void testApplicationProfileBothNotExist() {
@Test
public void testCustomSql() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
properties.setSql("SELECT MY_KEY, MY_VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?");
properties.setSqlWithoutProfile(
"SELECT MY_KEY, MY_VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE is null and LABEL=?");
Expand All @@ -161,11 +155,30 @@ public void testCustomSql() {
assertThat(env.getPropertySources().get(3).getSource().get("a.b.c")).isEqualTo("application-null");
}

@Test
public void testIncompleteConfig() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setSql("SELECT MY_KEY, MY_VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?");
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[] { "default", "bar" });
assertThat(env.getLabel()).isEqualTo("master");
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("foo-default");
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("foo-default");
assertThat(env.getPropertySources().get(2).getName()).isEqualTo("application-bar");
assertThat(env.getPropertySources().get(2).getSource().get("a.b.c")).isEqualTo("application-bar");
assertThat(env.getPropertySources().get(3).getName()).isEqualTo("application-default");
assertThat(env.getPropertySources().get(3).getSource().get("a.b.c")).isEqualTo("application-default");
}

@Test
public void testNotFailOnError() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setFailOnError(false);
properties.setEnableSqlWithoutProfile(true);
// when sql is customized but forgot to customize sqlWithoutProfile then
// sqlWithoutProfile should fail but sql with profile should still working when
// failOnError is off
Expand All @@ -187,7 +200,6 @@ public void testNotFailOnError() {
@Test
public void testFailOnError() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setEnableSqlWithoutProfile(true);
properties.setSqlWithoutProfile(
"SELECT SHOULD_FAIL from TABLE_NOTEXIST where APPLICATION=? and PROFILE is null and LABEL=?");
JdbcEnvironmentRepository repository = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
Expand Down
2 changes: 2 additions & 0 deletions spring-cloud-config-server/src/test/resources/data-jdbc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('app
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('application', null, 'master', 'a.b.c', 'application-null');

INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', 'bar', 'master', 'a.b.c', 'foo-bar');
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', 'default', 'master', 'a.b.c', 'foo-default');
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');

0 comments on commit 4d6db55

Please sign in to comment.