Skip to content

Commit

Permalink
Support multiple labels in JdbcEnvironmentRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Aug 28, 2024
1 parent 774f6cc commit 6bbc610
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,21 @@ public Environment findOne(String application, String profile, String label) {
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);
}
String[] labels = { label };
if (labels[0].contains(",")) {
labels = labels[0].split(",");
}
// add properties without profile, equivalent to foo.yml, application.yml
if (!configIncomplete) {
for (String app : applications) {
addPropertySource(environment, app, null, 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ public void testCustomLabel() {
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("e.f.g")).isEqualTo("application-default");
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("application-default");
assertThat(env.getPropertySources().get(2).getSource().get("a.b.c")).isEqualTo("application-null");
}

@ImportAutoConfiguration(SqlInitializationAutoConfiguration.class)
@Configuration(proxyBeanMethods = false)
protected static class ApplicationConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit 6bbc610

Please # to comment.