Skip to content

Commit 3788abc

Browse files
committed
Fix database name cannot be customized when using the Bitnami PostgreSQL image
Signed-off-by: He Zean <realhezean@gmail.com>
1 parent cc49b30 commit 3788abc

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {
4040

4141
@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL)
42-
void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
42+
void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
4343
assertConnectionDetails(connectionDetails);
44+
checkDatabaseAccess(connectionDetails);
4445
}
4546

4647
@DockerComposeTest(composeFile = "postgres-with-trust-host-auth-method-compose.yaml", image = TestImage.POSTGRESQL)
@@ -53,8 +54,10 @@ void runCreatesConnectionDetailsThatCanAccessDatabaseWhenHostAuthMethodIsTrust(
5354
}
5455

5556
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
56-
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
57+
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails)
58+
throws ClassNotFoundException {
5759
assertConnectionDetails(connectionDetails);
60+
checkDatabaseAccess(connectionDetails);
5861
}
5962

6063
@DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL)

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {
4343
@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL)
4444
void runCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
4545
assertConnectionDetails(connectionDetails);
46+
checkDatabaseAccess(connectionDetails);
4647
}
4748

4849
@DockerComposeTest(composeFile = "postgres-with-trust-host-auth-method-compose.yaml", image = TestImage.POSTGRESQL)
@@ -59,6 +60,7 @@ void runCreatesConnectionDetailsThatCanAccessDatabaseWhenHostAuthMethodIsTrust(
5960
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
6061
void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
6162
assertConnectionDetails(connectionDetails);
63+
checkDatabaseAccess(connectionDetails);
6264
}
6365

6466
@DockerComposeTest(composeFile = "postgres-application-name-compose.yaml", image = TestImage.POSTGRESQL)

spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ services:
44
ports:
55
- '5432'
66
environment:
7-
- 'POSTGRESQL_USER=myuser'
8-
- 'POSTGRESQL_DB=mydatabase'
7+
- 'POSTGRESQL_USERNAME=myuser'
8+
- 'POSTGRESQL_DATABASE=mydatabase'
99
- 'POSTGRESQL_PASSWORD=secret'

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,19 +29,37 @@
2929
* @author Phillip Webb
3030
* @author Scott Frederick
3131
* @author Sidmar Theodoro
32+
* @author He Zean
3233
*/
3334
class PostgresEnvironment {
3435

36+
private static final String[] USERNAME_KEYS = new String[] { "POSTGRES_USER", "POSTGRES_USERNAME",
37+
"POSTGRESQL_USER", "POSTGRESQL_USERNAME" };
38+
39+
private static final String DEFAULT_USERNAME = "postgres";
40+
41+
private static final String[] DATABASE_KEYS = new String[] { "POSTGRES_DB", "POSTGRES_DATABASE",
42+
"POSTGRESQL_DATABASE" };
43+
3544
private final String username;
3645

3746
private final String password;
3847

3948
private final String database;
4049

4150
PostgresEnvironment(Map<String, String> env) {
42-
this.username = env.getOrDefault("POSTGRES_USER", env.getOrDefault("POSTGRESQL_USER", "postgres"));
51+
this.username = extractUsername(env);
4352
this.password = extractPassword(env);
44-
this.database = env.getOrDefault("POSTGRES_DB", env.getOrDefault("POSTGRESQL_DB", this.username));
53+
this.database = extractDatabase(env);
54+
}
55+
56+
private String extractUsername(Map<String, String> env) {
57+
for (String key : USERNAME_KEYS) {
58+
if (env.containsKey(key)) {
59+
return env.get(key);
60+
}
61+
}
62+
return DEFAULT_USERNAME;
4563
}
4664

4765
private String extractPassword(Map<String, String> env) {
@@ -53,6 +71,15 @@ private String extractPassword(Map<String, String> env) {
5371
return password;
5472
}
5573

74+
private String extractDatabase(Map<String, String> env) {
75+
for (String key : DATABASE_KEYS) {
76+
if (env.containsKey(key)) {
77+
return env.get(key);
78+
}
79+
}
80+
return this.username;
81+
}
82+
5683
private boolean isUsingTrustHostAuthMethod(Map<String, String> env) {
5784
String hostAuthMethod = env.get("POSTGRES_HOST_AUTH_METHOD");
5885
return "trust".equals(hostAuthMethod);

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* @author Phillip Webb
3333
* @author Scott Frederick
3434
* @author Sidmar Theodoro
35+
* @author He Zean
3536
*/
3637
class PostgresEnvironmentTests {
3738

@@ -67,6 +68,20 @@ void getUsernameWhenHasPostgresqlUser() {
6768
assertThat(environment.getUsername()).isEqualTo("me");
6869
}
6970

71+
@Test
72+
void getUsernameWhenHasPostgresUsername() {
73+
PostgresEnvironment environment = new PostgresEnvironment(
74+
Map.of("POSTGRES_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
75+
assertThat(environment.getUsername()).isEqualTo("me");
76+
}
77+
78+
@Test
79+
void getUsernameWhenHasPostgresqlUsername() {
80+
PostgresEnvironment environment = new PostgresEnvironment(
81+
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
82+
assertThat(environment.getUsername()).isEqualTo("me");
83+
}
84+
7085
@Test
7186
void getPasswordWhenHasPostgresPassword() {
7287
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
@@ -111,6 +126,13 @@ void getDatabaseWhenNoPostgresqlDbAndPostgresUser() {
111126
assertThat(environment.getDatabase()).isEqualTo("me");
112127
}
113128

129+
@Test
130+
void getDatabaseWhenNoPostgresqlDatabaseAndPostgresqlUsername() {
131+
PostgresEnvironment environment = new PostgresEnvironment(
132+
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
133+
assertThat(environment.getDatabase()).isEqualTo("me");
134+
}
135+
114136
@Test
115137
void getDatabaseWhenHasPostgresDb() {
116138
PostgresEnvironment environment = new PostgresEnvironment(
@@ -119,9 +141,16 @@ void getDatabaseWhenHasPostgresDb() {
119141
}
120142

121143
@Test
122-
void getDatabaseWhenHasPostgresqlDb() {
144+
void getDatabaseWhenHasPostgresDatabase() {
145+
PostgresEnvironment environment = new PostgresEnvironment(
146+
Map.of("POSTGRES_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
147+
assertThat(environment.getDatabase()).isEqualTo("db");
148+
}
149+
150+
@Test
151+
void getDatabaseWhenHasPostgresqlDatabase() {
123152
PostgresEnvironment environment = new PostgresEnvironment(
124-
Map.of("POSTGRESQL_DB", "db", "POSTGRESQL_PASSWORD", "secret"));
153+
Map.of("POSTGRESQL_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
125154
assertThat(environment.getDatabase()).isEqualTo("db");
126155
}
127156

0 commit comments

Comments
 (0)