Skip to content

Commit 4d6db55

Browse files
committed
Merge branch '3.1.x'
2 parents 5dabc7d + 52b1909 commit 4d6db55

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentProperties.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
4343
/** SQL used to query database for keys and values. */
4444
private String sql = DEFAULT_SQL;
4545

46-
private boolean enableSqlWithoutProfile = false;
47-
4846
/** SQL used to query database for keys and values when profile is null. */
4947
private String sqlWithoutProfile = DEFAULT_SQL_WITHOUT_PROFILE;
5048

@@ -94,12 +92,9 @@ public void setFailOnError(boolean failOnError) {
9492
this.failOnError = failOnError;
9593
}
9694

97-
public boolean isEnableSqlWithoutProfile() {
98-
return enableSqlWithoutProfile;
99-
}
100-
101-
public void setEnableSqlWithoutProfile(boolean enableSqlWithoutProfile) {
102-
this.enableSqlWithoutProfile = enableSqlWithoutProfile;
95+
public boolean isConfigIncomplete() {
96+
// sql and sqlWithoutProfile should be customized at the same time
97+
return !this.sql.equals(DEFAULT_SQL) && this.sqlWithoutProfile.equals(DEFAULT_SQL_WITHOUT_PROFILE);
10398
}
10499

105100
}

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepository.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
6767

6868
private boolean failOnError;
6969

70-
private boolean enableSqlWithoutProfile;
70+
private boolean configIncomplete;
7171

7272
@Deprecated
7373
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {
@@ -82,7 +82,7 @@ public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties pr
8282
this.sqlWithoutProfile = properties.getSqlWithoutProfile();
8383
this.failOnError = properties.isFailOnError();
8484
this.extractor = extractor;
85-
this.enableSqlWithoutProfile = properties.isEnableSqlWithoutProfile();
85+
this.configIncomplete = properties.isConfigIncomplete();
8686
}
8787

8888
public String getSql() {
@@ -102,7 +102,8 @@ public Environment findOne(String application, String profile, String label) {
102102
if (!StringUtils.hasText(profile)) {
103103
profile = "default";
104104
}
105-
if (!enableSqlWithoutProfile && !profile.startsWith("default")) {
105+
// fallback to previous logic: always include profile "default"
106+
if (configIncomplete && !profile.startsWith("default")) {
106107
profile = "default," + profile;
107108
}
108109
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
@@ -120,7 +121,9 @@ public Environment findOne(String application, String profile, String label) {
120121
addPropertySource(environment, app, env, label);
121122
}
122123
// add properties without profile, equivalent to foo.yml, application.yml
123-
addPropertySource(environment, app, null, label);
124+
if (!configIncomplete) {
125+
addPropertySource(environment, app, null, label);
126+
}
124127
}
125128
return environment;
126129
}
@@ -129,7 +132,7 @@ private void addPropertySource(Environment environment, String application, Stri
129132
try {
130133
Map<String, Object> source;
131134
String name;
132-
if (!enableSqlWithoutProfile || (enableSqlWithoutProfile && profile != null)) {
135+
if (profile != null) {
133136
source = this.jdbc.query(this.sql, this.extractor, application, profile, label);
134137
name = application + "-" + profile;
135138
}

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JdbcEnvironmentRepositoryTests.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public class JdbcEnvironmentRepositoryTests {
5555
@Test
5656
public void basicProperties() {
5757
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
58-
properties.setEnableSqlWithoutProfile(true);
5958
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
6059
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "bar", "");
6160
assertThat(env.getName()).isEqualTo("foo");
@@ -75,7 +74,6 @@ public void basicProperties() {
7574
@Test
7675
public void testDefaultProfile() {
7776
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
78-
properties.setEnableSqlWithoutProfile(true);
7977
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
8078
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "", "");
8179
assertThat(env.getName()).isEqualTo("foo");
@@ -95,7 +93,6 @@ public void testDefaultProfile() {
9593
@Test
9694
public void testProfileNotExist() {
9795
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
98-
properties.setEnableSqlWithoutProfile(true);
9996
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
10097
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "not_exist", "");
10198
assertThat(env.getName()).isEqualTo("foo");
@@ -111,7 +108,6 @@ public void testProfileNotExist() {
111108
@Test
112109
public void testApplicationNotExist() {
113110
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
114-
properties.setEnableSqlWithoutProfile(true);
115111
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
116112
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("not_exist", "bar", "");
117113
assertThat(env.getName()).isEqualTo("not_exist");
@@ -127,7 +123,6 @@ public void testApplicationNotExist() {
127123
@Test
128124
public void testApplicationProfileBothNotExist() {
129125
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
130-
properties.setEnableSqlWithoutProfile(true);
131126
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
132127
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("not_exist", "not_exist", "");
133128
assertThat(env.getName()).isEqualTo("not_exist");
@@ -141,7 +136,6 @@ public void testApplicationProfileBothNotExist() {
141136
@Test
142137
public void testCustomSql() {
143138
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
144-
properties.setEnableSqlWithoutProfile(true);
145139
properties.setSql("SELECT MY_KEY, MY_VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?");
146140
properties.setSqlWithoutProfile(
147141
"SELECT MY_KEY, MY_VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE is null and LABEL=?");
@@ -161,11 +155,30 @@ public void testCustomSql() {
161155
assertThat(env.getPropertySources().get(3).getSource().get("a.b.c")).isEqualTo("application-null");
162156
}
163157

158+
@Test
159+
public void testIncompleteConfig() {
160+
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
161+
properties.setSql("SELECT MY_KEY, MY_VALUE from MY_PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?");
162+
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
163+
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "bar", "");
164+
assertThat(env.getName()).isEqualTo("foo");
165+
assertThat(env.getProfiles()).isEqualTo(new String[] { "default", "bar" });
166+
assertThat(env.getLabel()).isEqualTo("master");
167+
assertThat(env.getPropertySources()).isNotEmpty();
168+
assertThat(env.getPropertySources().get(0).getName()).isEqualTo("foo-bar");
169+
assertThat(env.getPropertySources().get(0).getSource().get("a.b.c")).isEqualTo("foo-bar");
170+
assertThat(env.getPropertySources().get(1).getName()).isEqualTo("foo-default");
171+
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("foo-default");
172+
assertThat(env.getPropertySources().get(2).getName()).isEqualTo("application-bar");
173+
assertThat(env.getPropertySources().get(2).getSource().get("a.b.c")).isEqualTo("application-bar");
174+
assertThat(env.getPropertySources().get(3).getName()).isEqualTo("application-default");
175+
assertThat(env.getPropertySources().get(3).getSource().get("a.b.c")).isEqualTo("application-default");
176+
}
177+
164178
@Test
165179
public void testNotFailOnError() {
166180
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
167181
properties.setFailOnError(false);
168-
properties.setEnableSqlWithoutProfile(true);
169182
// when sql is customized but forgot to customize sqlWithoutProfile then
170183
// sqlWithoutProfile should fail but sql with profile should still working when
171184
// failOnError is off
@@ -187,7 +200,6 @@ public void testNotFailOnError() {
187200
@Test
188201
public void testFailOnError() {
189202
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
190-
properties.setEnableSqlWithoutProfile(true);
191203
properties.setSqlWithoutProfile(
192204
"SELECT SHOULD_FAIL from TABLE_NOTEXIST where APPLICATION=? and PROFILE is null and LABEL=?");
193205
JdbcEnvironmentRepository repository = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),

spring-cloud-config-server/src/test/resources/data-jdbc.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('app
66
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('application', null, 'master', 'a.b.c', 'application-null');
77

88
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', 'bar', 'master', 'a.b.c', 'foo-bar');
9+
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', 'default', 'master', 'a.b.c', 'foo-default');
910
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', null, 'master', 'a.b.c', 'foo-null');
1011
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', 'bar', 'master', 'a.b.c', 'application-bar');
12+
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', 'default', 'master', 'a.b.c', 'application-default');
1113
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', null, 'master', 'a.b.c', 'application-null');

0 commit comments

Comments
 (0)