feat: open psql in the database container

This commit is contained in:
2026-09-08 14:16:18 +00:00
parent 61f35f5ae4
commit 6389f3f1f5
6 changed files with 65 additions and 12 deletions

View File

@@ -2,7 +2,7 @@ mod server;
mod shape;
use fs_err::File;
use std::io::{self, Write};
use std::io::{self, IsTerminal, Write};
use std::path::Path;
use std::process::Stdio;
@@ -12,8 +12,8 @@ use self::server::{Database, wait_until_ready, when_ready};
use self::shape::{Dump, HEADER_LEN, Kind};
use crate::cli::Format;
use crate::cmd::{
Cmd, Cp, CreateDb, DropDb, Gunzip, Gzip, Head, PgDump, PgDumpAll, PgRestore, Rm, Start, Stop,
Up,
Cmd, Cp, CreateDb, DropDb, Gunzip, Gzip, Head, PgDump, PgDumpAll, PgRestore, Psql, Rm, Start,
Stop, Up,
};
use crate::ctx::Ctx;
use crate::fsops::{remove_file, rename, suffixed};
@@ -202,6 +202,18 @@ pub fn import(ctx: &Ctx, file: &Path) -> Result<()> {
Ok(())
}
pub fn psql(ctx: &Ctx, rest: &[String]) -> Result<()> {
let db = Database::resolve(ctx)?;
// a terminal only if this one has one, so `psql -c ... | cat` still works
Psql::new(&db.user, &db.name)
.args(rest)
.in_container(&db.container)
.interactive()
.tty(io::stdin().is_terminal())
.replace(ctx)
}
pub fn dump(ctx: &Ctx, file: &Path, format: Format, gzip: bool) -> Result<()> {
let db = Database::resolve(ctx)?;

View File

@@ -42,8 +42,8 @@ impl Database {
pub(super) fn restore_with(&self, kind: Kind) -> Box<dyn Cmd + '_> {
match kind {
Kind::Archive => Box::new(PgRestore::new(&self.user, &self.name)),
Kind::Sql => Box::new(Psql::new(&self.user, &self.name).atomic()),
Kind::Cluster => Box::new(Psql::new(&self.user, "postgres")),
Kind::Sql => Box::new(Psql::new(&self.user, &self.name).quiet().atomic()),
Kind::Cluster => Box::new(Psql::new(&self.user, "postgres").quiet()),
}
}
}