feat: pg_isready in `postgres import`

Fixed issue where arguments passed to `compose stop` weren't passed to
the underlying command. Added pg_isready call between each command call
in restore script.
This commit is contained in:
Matej Janezic 2023-11-27 16:33:00 +01:00
parent 2754a3bb30
commit 98f02c1c08
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 23 additions and 4 deletions

View File

@ -4,8 +4,9 @@ use std::{
ffi::OsStr,
fmt::Display,
fs::{File, OpenOptions},
os::unix::process::CommandExt,
path::PathBuf,
process::{Command, Stdio},
process::{Child, Command, Stdio},
};
use crate::debug_println;
@ -81,6 +82,10 @@ impl CommandBuilder {
Ok(())
}
pub fn spawn(mut self) -> Result<(Child)> {
Ok(self.build()?.spawn()?)
}
pub fn exec_redirect_stdout(mut self, stdio: Stdio) -> Result<()> {
self.build()?.stdout(stdio).spawn()?.wait()?;
Ok(())

View File

@ -30,7 +30,7 @@ pub fn ps() -> Result<()> {
/// `docker compose --env-file ./.env -f docker/local/docker-compose.yaml up start`
pub fn start(containers: Option<&str>) -> Result<()> {
let args = format!("start {}", containers.unwrap_or(""));
CommandBuilder::docker_compose().args("start").exec()
CommandBuilder::docker_compose().args(&args).exec()
}
pub fn stop() -> Result<()> {

View File

@ -3,6 +3,8 @@ use std::{
fs::File,
path::{Path, PathBuf},
process::Stdio,
thread,
time::Duration,
};
use super::docker_compose;
@ -42,7 +44,7 @@ pub fn import(file: &Path) -> Result<()> {
docker_compose::stop()?;
println!("starting db container");
docker_compose::start(Some(&db_container))?;
docker_compose::start(Some("postgresdb"))?;
println!("restoring database");
let commands = [
@ -51,8 +53,20 @@ pub fn import(file: &Path) -> Result<()> {
format!("exec {db_container} createdb -U db -E utf8 -T template0 db"),
format!("exec {db_container} pg_restore -U db --dbname=db /tmp/dbdump"),
];
for command in commands {
CommandBuilder::docker().args(&command).exec()?;
println!("waiting until pg_isready");
while !CommandBuilder::docker()
.args(&format!(
"exec {db_container} pg_isready -h postgresdb -p 5432 -U db",
))
.spawn()?
.wait()?
.success()
{
thread::sleep(Duration::from_secs(1));
CommandBuilder::docker().args(&command).exec()?;
}
}
println!("restarting containers");