refactor: one helper each for the repeated blocks
This commit is contained in:
@@ -16,7 +16,7 @@ use crate::cmd::{
|
||||
};
|
||||
use crate::ctx::Ctx;
|
||||
use crate::fsops::{create_private_new, suffixed};
|
||||
use crate::output::{note, warning};
|
||||
use crate::output::{note, plural, warning};
|
||||
|
||||
// unique per run: docker cp will not copy a directory over an existing path, and
|
||||
// quietly leaves whatever was there for pg_restore to read instead
|
||||
@@ -36,6 +36,13 @@ fn partial_path(file: &Path) -> PathBuf {
|
||||
suffixed(file, &format!(".{}.partial", std::process::id()))
|
||||
}
|
||||
|
||||
// the database was dropped before the restore was attempted, so a restore that
|
||||
// failed leaves nothing behind: said the same way wherever it happens, since
|
||||
// what the user has to do about it does not depend on which tool it was
|
||||
fn left_empty(tool: &str) -> anyhow::Error {
|
||||
anyhow::anyhow!("{tool} failed, the database is left empty")
|
||||
}
|
||||
|
||||
// pg_dumpall recreates roles the cluster already has, so this is the expected
|
||||
// complaint rather than a failure
|
||||
fn is_existing_role_error(line: &str) -> bool {
|
||||
@@ -75,17 +82,17 @@ fn restore_cluster(ctx: &Ctx, db: &Database, restore: &dyn Cmd, file: &Path) ->
|
||||
note!(
|
||||
ctx,
|
||||
"left {existing} existing role{} alone",
|
||||
if existing == 1 { "" } else { "s" }
|
||||
plural(existing)
|
||||
);
|
||||
}
|
||||
|
||||
if !out.status.success() {
|
||||
bail!("psql failed, the database is left empty");
|
||||
return Err(left_empty("psql"));
|
||||
}
|
||||
if failed > 0 {
|
||||
bail!(
|
||||
"psql reported {failed} error{}, so the cluster restored only in part",
|
||||
if failed == 1 { "" } else { "s" }
|
||||
plural(failed)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -99,7 +106,7 @@ fn gzip_kind(ctx: &Ctx, db: &Database, file: &Path) -> Result<Kind> {
|
||||
wait_until_ready(ctx, db)?;
|
||||
|
||||
let out = Gunzip
|
||||
.pipe(&Head::bytes(HEADER_LEN))
|
||||
.pipe(&Head::bytes(HEADER_LEN))?
|
||||
// head closes the pipe once it has its bytes, which kills gunzip
|
||||
.allow_early_close()
|
||||
.in_container(&db.container)
|
||||
@@ -257,14 +264,14 @@ fn restore_dump(
|
||||
.status(ctx)?;
|
||||
|
||||
if !status.success() {
|
||||
bail!("{tool} failed, the database is left empty");
|
||||
return Err(left_empty(tool));
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let restore: Box<dyn Cmd> = match dump {
|
||||
Dump::Gzip => Box::new(Gunzip.pipe(&*restore)),
|
||||
Dump::Gzip => Box::new(Gunzip.pipe(&*restore)?),
|
||||
_ => restore,
|
||||
};
|
||||
|
||||
@@ -280,7 +287,7 @@ fn restore_dump(
|
||||
.stdin_from(ctx, file)?;
|
||||
|
||||
if !status.success() {
|
||||
bail!("{tool} failed, the database is left empty");
|
||||
return Err(left_empty(tool));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -328,7 +335,7 @@ pub fn dump(ctx: &Ctx, args: &cli::Dump) -> Result<()> {
|
||||
|
||||
let dumped = if gzip {
|
||||
dumping
|
||||
.pipe(&Gzip)
|
||||
.pipe(&Gzip)?
|
||||
.in_container(&db.container)
|
||||
.stream_to(ctx, stdout)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user