make sqlWithoutProfile adaptive (#2200)

This commit is contained in:
woshikid
2022-12-15 21:57:19 +08:00
committed by GitHub
parent 2ad5c4641a
commit 52b1909d45
4 changed files with 33 additions and 21 deletions

View File

@@ -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;
@@ -94,12 +92,9 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
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);
}
}

View File

@@ -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) {
@@ -82,7 +82,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
this.sqlWithoutProfile = properties.getSqlWithoutProfile();
this.failOnError = properties.isFailOnError();
this.extractor = extractor;
this.enableSqlWithoutProfile = properties.isEnableSqlWithoutProfile();
this.configIncomplete = properties.isConfigIncomplete();
}
public String getSql() {
@@ -102,7 +102,8 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
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);
@@ -120,7 +121,9 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
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;
}
@@ -129,7 +132,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
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;
}

View File

@@ -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");
@@ -75,7 +74,6 @@ public class JdbcEnvironmentRepositoryTests {
@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");
@@ -95,7 +93,6 @@ public class JdbcEnvironmentRepositoryTests {
@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");
@@ -111,7 +108,6 @@ public class JdbcEnvironmentRepositoryTests {
@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");
@@ -127,7 +123,6 @@ public class JdbcEnvironmentRepositoryTests {
@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");
@@ -141,7 +136,6 @@ public class JdbcEnvironmentRepositoryTests {
@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=?");
@@ -161,11 +155,30 @@ public class JdbcEnvironmentRepositoryTests {
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
@@ -187,7 +200,6 @@ public class JdbcEnvironmentRepositoryTests {
@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),

View File

@@ -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');