Skip to content

Commit 9cd46fd

Browse files
committed
fix: Add support for postgres 9.2 and lower
1 parent b53e4f2 commit 9cd46fd

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/Testcontainers.PostgreSql/PostgreSqlBuilder.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ public override PostgreSqlContainer Build()
7474
{
7575
Validate();
7676

77+
// The PostgreSql image does not contain pg_isready before version 9.3. Wait for logging message that says server is ready - "PostgreSQL init process complete; ready for start up."
78+
// this was used instead of using psql select version() because the database is still not ready to accept external connection https://github.com/docker-library/postgres/issues/146
79+
var isLegacyPostgreSql = DockerResourceConfiguration.Image.MatchVersion(v => v.Major <= 9 && v.Minor < 3);
7780
// By default, the base builder waits until the container is running. However, for PostgreSql, a more advanced waiting strategy is necessary that requires access to the configured database and username.
7881
// If the user does not provide a custom waiting strategy, append the default PostgreSql waiting strategy.
79-
var postgreSqlBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ? this : WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil(DockerResourceConfiguration)));
82+
var postgreSqlBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1
83+
? this
84+
: isLegacyPostgreSql
85+
? WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("PostgreSQL init process complete; ready for start up."))
86+
: WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil(DockerResourceConfiguration)));
8087
return new PostgreSqlContainer(postgreSqlBuilder.DockerResourceConfiguration);
8188
}
8289

tests/Testcontainers.PostgreSql.Tests/PostgreSqlContainerTest.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
namespace Testcontainers.PostgreSql;
22

3-
public sealed class PostgreSqlContainerTest : IAsyncLifetime
3+
public abstract class PostgreSqlContainerTest : IAsyncLifetime
44
{
55
// # --8<-- [start:UsePostgreSqlContainer]
6-
private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder().Build();
6+
private readonly PostgreSqlContainer _postgreSqlContainer;
7+
8+
public PostgreSqlContainer Container { get { return _postgreSqlContainer; } }
9+
10+
public PostgreSqlContainerTest(PostgreSqlContainer postgreSqlContainer)
11+
{
12+
_postgreSqlContainer = postgreSqlContainer;
13+
}
714

815
public Task InitializeAsync()
916
{
@@ -46,13 +53,13 @@ public async Task ExecScriptReturnsSuccessful()
4653
}
4754
// # --8<-- [end:UsePostgreSqlContainer]
4855

49-
public sealed class ReuseContainerTest : IClassFixture<PostgreSqlFixture>, IDisposable
56+
public sealed class ReusePostgres15ContainerTest : IClassFixture<PostgreSql15Fixture>, IDisposable
5057
{
5158
private readonly CancellationTokenSource _cts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
5259

53-
private readonly PostgreSqlFixture _fixture;
60+
private readonly PostgreSql15Fixture _fixture;
5461

55-
public ReuseContainerTest(PostgreSqlFixture fixture)
62+
public ReusePostgres15ContainerTest(PostgreSql15Fixture fixture)
5663
{
5764
_fixture = fixture;
5865
}
@@ -79,10 +86,20 @@ await _fixture.Container.StartAsync(_cts.Token)
7986
}
8087

8188
[UsedImplicitly]
82-
public sealed class PostgreSqlFixture : ContainerFixture<PostgreSqlBuilder, PostgreSqlContainer>
89+
public sealed class PostgreSql15Fixture : PostgreSqlContainerTest
90+
{
91+
public PostgreSql15Fixture()
92+
: base(new PostgreSqlBuilder().Build())
93+
{
94+
}
95+
}
96+
97+
[UsedImplicitly]
98+
public sealed class PostgreSql9Fixture : PostgreSqlContainerTest
8399
{
84-
public PostgreSqlFixture(IMessageSink messageSink)
85-
: base(messageSink)
100+
// https://github.com/testcontainers/testcontainers-dotnet/issues/1142.
101+
public PostgreSql9Fixture()
102+
: base(new PostgreSqlBuilder().WithImage("postgres:9.2").Build())
86103
{
87104
}
88105
}

0 commit comments

Comments
 (0)