Add --destroy flag to "pg stop" CLI command

This commit is contained in:
Stas Kelvich
2021-05-20 20:37:37 +03:00
parent d534aeb9e1
commit 6ad6e5bd84
2 changed files with 24 additions and 6 deletions

View File

@@ -420,8 +420,16 @@ impl PostgresNode {
self.pg_ctl(&["restart"])
}
pub fn stop(&self) -> Result<()> {
self.pg_ctl(&["-m", "immediate", "stop"])
pub fn stop(&self, destroy: bool) -> Result<()> {
self.pg_ctl(&["-m", "immediate", "stop"])?;
if destroy {
println!(
"Destroying postgres data directory '{}'",
self.pgdata().to_str().unwrap()
);
fs::remove_dir_all(&self.pgdata())?;
}
Ok(())
}
pub fn connstr(&self) -> String {
@@ -453,7 +461,7 @@ impl Drop for PostgresNode {
// and checking it here. But let just clean datadirs on start.
fn drop(&mut self) {
if self.is_test {
let _ = self.stop();
let _ = self.stop(true);
}
}
}

View File

@@ -61,7 +61,16 @@ fn main() -> Result<()> {
.subcommand(SubCommand::with_name("list"))
.subcommand(SubCommand::with_name("create").arg(timeline_arg.clone()))
.subcommand(SubCommand::with_name("start").arg(timeline_arg.clone()))
.subcommand(SubCommand::with_name("stop").arg(timeline_arg.clone())),
.subcommand(
SubCommand::with_name("stop")
.arg(timeline_arg.clone())
.arg(
Arg::with_name("destroy")
.help("Also delete data directory (now optional, should be default in future)")
.long("destroy")
.required(false)
)
)
)
.subcommand(
SubCommand::with_name("remote")
@@ -356,12 +365,13 @@ fn handle_pg(pg_match: &ArgMatches, env: &local_env::LocalEnv) -> Result<()> {
}
("stop", Some(sub_m)) => {
let timeline_name = sub_m.value_of("timeline").unwrap_or("main");
let destroy = sub_m.is_present("destroy");
let node = cplane
.nodes
.get(timeline_name)
.ok_or_else(|| anyhow!("postgres {} is not found", timeline_name))?;
node.stop()?;
// TODO: destroy data directory here
node.stop(destroy)?;
}
_ => {}