Make SQL without profile optional (#2196)
Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
@@ -43,6 +43,8 @@ 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;
|
||||
|
||||
@@ -92,4 +94,12 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
|
||||
this.failOnError = failOnError;
|
||||
}
|
||||
|
||||
public boolean isEnableSqlWithoutProfile() {
|
||||
return enableSqlWithoutProfile;
|
||||
}
|
||||
|
||||
public void setEnableSqlWithoutProfile(boolean enableSqlWithoutProfile) {
|
||||
this.enableSqlWithoutProfile = enableSqlWithoutProfile;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,6 +67,8 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
|
||||
private boolean failOnError;
|
||||
|
||||
private boolean enableSqlWithoutProfie;
|
||||
|
||||
@Deprecated
|
||||
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {
|
||||
this(jdbc, properties, new PropertiesResultSetExtractor());
|
||||
@@ -80,6 +82,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
this.sqlWithoutProfile = properties.getSqlWithoutProfile();
|
||||
this.failOnError = properties.isFailOnError();
|
||||
this.extractor = extractor;
|
||||
this.enableSqlWithoutProfie = properties.isEnableSqlWithoutProfile();
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
@@ -96,9 +99,12 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
if (StringUtils.isEmpty(label)) {
|
||||
label = "master";
|
||||
}
|
||||
if (StringUtils.isEmpty(profile)) {
|
||||
if (!StringUtils.hasText(profile)) {
|
||||
profile = "default";
|
||||
}
|
||||
if (!enableSqlWithoutProfie && !profile.startsWith("default")) {
|
||||
profile = "default," + profile;
|
||||
}
|
||||
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
|
||||
Environment environment = new Environment(application, profiles, label, null, null);
|
||||
if (!config.startsWith("application")) {
|
||||
@@ -123,7 +129,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
try {
|
||||
Map<String, Object> source;
|
||||
String name;
|
||||
if (profile != null) {
|
||||
if (enableSqlWithoutProfie && profile != null) {
|
||||
source = this.jdbc.query(this.sql, this.extractor, application, profile, label);
|
||||
name = application + "-" + profile;
|
||||
}
|
||||
|
||||
@@ -54,9 +54,10 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void basicProperties() {
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
|
||||
new JdbcEnvironmentProperties(), new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
|
||||
.findOne("foo", "bar", "");
|
||||
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");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "bar" });
|
||||
assertThat(env.getLabel()).isEqualTo("master");
|
||||
@@ -73,9 +74,10 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void testDefaultProfile() {
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
|
||||
new JdbcEnvironmentProperties(), new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
|
||||
.findOne("foo", "", "");
|
||||
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");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "default" });
|
||||
assertThat(env.getLabel()).isEqualTo("master");
|
||||
@@ -92,9 +94,10 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void testProfileNotExist() {
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
|
||||
new JdbcEnvironmentProperties(), new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
|
||||
.findOne("foo", "not_exist", "");
|
||||
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");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "not_exist" });
|
||||
assertThat(env.getLabel()).isEqualTo("master");
|
||||
@@ -107,9 +110,10 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void testApplicationNotExist() {
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
|
||||
new JdbcEnvironmentProperties(), new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
|
||||
.findOne("not_exist", "bar", "");
|
||||
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");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "bar" });
|
||||
assertThat(env.getLabel()).isEqualTo("master");
|
||||
@@ -122,9 +126,10 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void testApplicationProfileBothNotExist() {
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
|
||||
new JdbcEnvironmentProperties(), new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
|
||||
.findOne("not_exist", "not_exist", "");
|
||||
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");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "not_exist" });
|
||||
assertThat(env.getLabel()).isEqualTo("master");
|
||||
@@ -136,6 +141,7 @@ 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=?");
|
||||
@@ -159,6 +165,7 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user