Support multiple labels in JdbcEnvironmentRepository (#2455)

* Support multiple labels in JdbcEnvironmentRepository

Fixes #2449

* Reversing labels

---------

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2024-10-02 10:14:06 -04:00
committed by GitHub
parent d8586b255c
commit 4f2ec6433f
3 changed files with 35 additions and 8 deletions

View File

@@ -119,15 +119,25 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
List<String> envs = new ArrayList<>(new LinkedHashSet<>(Arrays.asList(profiles)));
Collections.reverse(applications);
Collections.reverse(envs);
for (String env : envs) {
for (String app : applications) {
addPropertySource(environment, app, env, label);
}
List<String> labels;
if (label.contains(",")) {
labels = Arrays.asList(StringUtils.commaDelimitedListToStringArray(label));
Collections.reverse(labels);
}
// add properties without profile, equivalent to foo.yml, application.yml
if (!configIncomplete) {
for (String app : applications) {
addPropertySource(environment, app, null, label);
else {
labels = Collections.singletonList(label);
}
for (String l : labels) {
for (String env : envs) {
for (String app : applications) {
addPropertySource(environment, app, env, l);
}
}
// add properties without profile, equivalent to foo.yml, application.yml
if (!configIncomplete) {
for (String app : applications) {
addPropertySource(environment, app, null, l);
}
}
}
return environment;

View File

@@ -229,6 +229,22 @@ public class JdbcEnvironmentRepositoryTests {
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("application-bar");
}
@Test
public void testMultipleLabels() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setDefaultLabel("main");
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
.findOne("application", "default", "main,master");
assertThat(env.getName()).isEqualTo("application");
assertThat(env.getProfiles()).isEqualTo(new String[] { "default" });
assertThat(env.getLabel()).isEqualTo("main,master");
assertThat(env.getPropertySources()).isNotEmpty();
assertThat(env.getPropertySources().get(0).getSource().get("a.b.c")).isEqualTo("application-default");
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("application-null");
assertThat(env.getPropertySources().get(2).getSource().get("e.f.g")).isEqualTo("application-default");
}
@ImportAutoConfiguration(SqlInitializationAutoConfiguration.class)
@Configuration(proxyBeanMethods = false)
protected static class ApplicationConfiguration {

View File

@@ -14,3 +14,4 @@ INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('foo', 'bar', 'main', 'a.b.c', 'foo-bar');
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('application', 'bar', 'main', 'a.b.c', 'application-bar');
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('application', 'default', 'main', 'e.f.g', 'application-default');