Skip to content

Commit

Permalink
Read version from postgres -V
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed May 17, 2022
1 parent 2ba7111 commit fc19560
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public PostgreSQLContainer(final DockerImageName dockerImageName) {

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

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

addExposedPort(POSTGRESQL_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,43 @@
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.utility.ComparableVersion;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PostgreSQLWaitStrategy extends AbstractWaitStrategy {

private final String version;

public PostgreSQLWaitStrategy(String version) {
this.version = version;
}
private final Pattern pattern = Pattern.compile("(?s)(?:\\d\\S*)");

@Override
protected void waitUntilReady() {
boolean isAtLeastMajorVersion94 = new ComparableVersion(this.version).isGreaterThanOrEqualTo("9.4");

List<String> firstAttempt = new ArrayList<>();
firstAttempt.add(".*PostgreSQL init process complete.*$");
firstAttempt.add(".*database system is ready to accept connections.*$");

List<String> secondAttempt = new ArrayList<>();
if (isAtLeastMajorVersion94) {
secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$");
try {
String postgresVersion = this.waitStrategyTarget.execInContainer("postgres", "-V").getStdout();
Matcher matcher = this.pattern.matcher(postgresVersion);
if (matcher.find()) {
String version = matcher.group();
boolean isAtLeastMajorVersion94 = new ComparableVersion(version).isGreaterThanOrEqualTo("9.4");

List<String> firstAttempt = new ArrayList<>();
firstAttempt.add(".*PostgreSQL init process complete.*$");
firstAttempt.add(".*database system is ready to accept connections.*$");

List<String> secondAttempt = new ArrayList<>();
if (isAtLeastMajorVersion94) {
secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$");
}
secondAttempt.add(".*database system is ready to accept connections.*$");

new MultiLogMessageWaitStrategy()
.withRegEx(firstAttempt)
.withRegEx(secondAttempt)
.waitUntilReady(this.waitStrategyTarget);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
secondAttempt.add(".*database system is ready to accept connections.*$");

new MultiLogMessageWaitStrategy()
.withRegEx(firstAttempt)
.withRegEx(secondAttempt)
.waitUntilReady(this.waitStrategyTarget);
}

}

0 comments on commit fc19560

Please sign in to comment.