From 52b1909d4543a02f4032aac07786d08be85d59b8 Mon Sep 17 00:00:00 2001 From: woshikid Date: Thu, 15 Dec 2022 21:57:19 +0800 Subject: [PATCH] make sqlWithoutProfile adaptive (#2200) --- .../JdbcEnvironmentProperties.java | 11 ++------ .../JdbcEnvironmentRepository.java | 13 +++++---- .../JdbcEnvironmentRepositoryTests.java | 28 +++++++++++++------ .../src/test/resources/data-jdbc.sql | 2 ++ 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentProperties.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentProperties.java index 90f2611971..3ab148c356 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentProperties.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentProperties.java @@ -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 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); } } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepository.java index 7264764fd3..2ce49be8e8 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepository.java @@ -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 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() { @@ -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); @@ -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; } @@ -129,7 +132,7 @@ private void addPropertySource(Environment environment, String application, Stri try { Map 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; } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepositoryTests.java index 9b74da2029..dc19a032e7 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepositoryTests.java @@ -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 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"); @@ -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"); @@ -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"); @@ -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"); @@ -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=?"); @@ -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 @@ -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), diff --git a/spring-cloud-config-server/src/test/resources/data-jdbc.sql b/spring-cloud-config-server/src/test/resources/data-jdbc.sql index 194e169b81..245eb36775 100644 --- a/spring-cloud-config-server/src/test/resources/data-jdbc.sql +++ b/spring-cloud-config-server/src/test/resources/data-jdbc.sql @@ -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'); \ No newline at end of file