Skip to content

Commit

Permalink
Fix: TmpDb::init() wait for psql (#602)
Browse files Browse the repository at this point in the history
Previously `TmpDb::init()` waited for postgres to become ready,
but the logic would panic if `psql` is not available on the
host. But, we should not have any expectation that `psql` be available
on the host. This PR updates the wait loop to run a query inside
the docker container.

---------

Co-authored-by: tbro <tbro@users.noreply.github.com>
  • Loading branch information
tbro and tbro authored May 31, 2024
1 parent 140a223 commit 05743e5
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/data_source/storage/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3097,33 +3097,42 @@ pub mod testing {
let db = Self {
host,
port,
container_id,
container_id: container_id.clone(),
};

// Wait for the database to be ready.
while !Command::new("psql")
while Command::new("docker")
.args([
"exec",
&container_id,
"pg_isready",
"-h",
&(db.host()),
"-p",
&(db.port().to_string()),
"localhost",
"-U",
"postgres",
])
.env("PGPASSWORD", "password")
// Null input so the command terminates as soon as it manages to connect.
.stdin(Stdio::null())
// Output from this command is not useful, it's just a prompt.
// Discard command output.
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap()
.success()
// We should ensure the exit status. A simple `unwrap`
// would panic on unrelated errors (such as network
// connection failures)
.and_then(|status| {
status
.success()
.then_some(true)
// Any ol' Error will do
.ok_or(std::io::Error::from_raw_os_error(666))
})
.is_err()
{
tracing::warn!("database is not ready");
sleep(Duration::from_secs(1)).await;
}

db
}

Expand Down

0 comments on commit 05743e5

Please sign in to comment.