Skip to content

Commit d2b2200

Browse files
committed
fix(postgres): Convert postgres to new RunFunctionWaitStrategy
This gets rid of a annoying depcreation warning.
1 parent 0916b41 commit d2b2200

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

modules/postgres/testcontainers/postgres/__init__.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313
import os
14-
from typing import Optional
14+
from typing import Optional, cast
1515

1616
from testcontainers.core.generic import DbContainer
1717
from testcontainers.core.utils import raise_for_deprecated_parameter
18-
from testcontainers.core.waiting_utils import wait_container_is_ready
18+
from testcontainers.core.wait_strategies import RunFunctionWaitStrategy
19+
from testcontainers.core.waiting_utils import WaitStrategyTarget
1920

2021
_UNSET = object()
2122

@@ -64,6 +65,7 @@ def __init__(
6465
self.driver = f"+{driver}" if driver else ""
6566

6667
self.with_exposed_ports(self.port)
68+
self.waiting_for(RunFunctionWaitStrategy(_check_postgres_ready))
6769

6870
def _configure(self) -> None:
6971
self.with_env("POSTGRES_USER", self.username)
@@ -87,15 +89,17 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] =
8789
port=self.port,
8890
)
8991

90-
@wait_container_is_ready()
91-
def _connect(self) -> None:
92-
escaped_single_password = self.password.replace("'", "'\"'\"'")
93-
result = self.exec(
94-
[
95-
"sh",
96-
"-c",
97-
f"PGPASSWORD='{escaped_single_password}' psql --username {self.username} --dbname {self.dbname} --host 127.0.0.1 -c 'select version();'",
98-
]
99-
)
100-
if result.exit_code:
101-
raise ConnectionError("pg_isready is not ready yet")
92+
93+
def _check_postgres_ready(container: WaitStrategyTarget) -> bool:
94+
wrapped = cast("PostgresContainer", container.get_wrapped_container())
95+
escaped_single_password = wrapped.password.replace("'", "'\"'\"'")
96+
result = wrapped.exec(
97+
[
98+
"sh",
99+
"-c",
100+
f"PGPASSWORD='{escaped_single_password}' psql --username {wrapped.username} --dbname {wrapped.dbname} --host 127.0.0.1 -c 'select version();'",
101+
]
102+
)
103+
if result.exit_code:
104+
raise ConnectionError("pg is not ready yet")
105+
return True

0 commit comments

Comments
 (0)