Skip to content

Commit fc19560

Browse files
committed
Read version from postgres -V
1 parent 2ba7111 commit fc19560

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public PostgreSQLContainer(final DockerImageName dockerImageName) {
4646

4747
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
4848

49-
this.waitStrategy = new PostgreSQLWaitStrategy(dockerImageName.getVersionPart());
49+
this.waitStrategy = new PostgreSQLWaitStrategy();
5050
this.setCommand("postgres", "-c", FSYNC_OFF_OPTION);
5151

5252
addExposedPort(POSTGRESQL_PORT);

modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLWaitStrategy.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,43 @@
33
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
44
import org.testcontainers.utility.ComparableVersion;
55

6+
import java.io.IOException;
67
import java.util.ArrayList;
78
import java.util.List;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
811

912
public class PostgreSQLWaitStrategy extends AbstractWaitStrategy {
1013

11-
private final String version;
12-
13-
public PostgreSQLWaitStrategy(String version) {
14-
this.version = version;
15-
}
14+
private final Pattern pattern = Pattern.compile("(?s)(?:\\d\\S*)");
1615

1716
@Override
1817
protected void waitUntilReady() {
19-
boolean isAtLeastMajorVersion94 = new ComparableVersion(this.version).isGreaterThanOrEqualTo("9.4");
20-
21-
List<String> firstAttempt = new ArrayList<>();
22-
firstAttempt.add(".*PostgreSQL init process complete.*$");
23-
firstAttempt.add(".*database system is ready to accept connections.*$");
24-
25-
List<String> secondAttempt = new ArrayList<>();
26-
if (isAtLeastMajorVersion94) {
27-
secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$");
18+
try {
19+
String postgresVersion = this.waitStrategyTarget.execInContainer("postgres", "-V").getStdout();
20+
Matcher matcher = this.pattern.matcher(postgresVersion);
21+
if (matcher.find()) {
22+
String version = matcher.group();
23+
boolean isAtLeastMajorVersion94 = new ComparableVersion(version).isGreaterThanOrEqualTo("9.4");
24+
25+
List<String> firstAttempt = new ArrayList<>();
26+
firstAttempt.add(".*PostgreSQL init process complete.*$");
27+
firstAttempt.add(".*database system is ready to accept connections.*$");
28+
29+
List<String> secondAttempt = new ArrayList<>();
30+
if (isAtLeastMajorVersion94) {
31+
secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$");
32+
}
33+
secondAttempt.add(".*database system is ready to accept connections.*$");
34+
35+
new MultiLogMessageWaitStrategy()
36+
.withRegEx(firstAttempt)
37+
.withRegEx(secondAttempt)
38+
.waitUntilReady(this.waitStrategyTarget);
39+
}
40+
} catch (IOException | InterruptedException e) {
41+
throw new RuntimeException(e);
2842
}
29-
secondAttempt.add(".*database system is ready to accept connections.*$");
30-
31-
new MultiLogMessageWaitStrategy()
32-
.withRegEx(firstAttempt)
33-
.withRegEx(secondAttempt)
34-
.waitUntilReady(this.waitStrategyTarget);
3543
}
3644

3745
}

0 commit comments

Comments
 (0)